php nginx 动态产生缩略图

<?php
$replace_from   = array( ' ', '(', ')' );
$replace_out    = array( '\ ', '\(', '\)' );

$str            =  str_replace( $replace_from  , $replace_out , trim( urldecode(                                           $_SERVER['REQUEST_URI'] ), '/' ) );
$str_arr        =   explode( '/',  $str );
$from_base      =  __DIR__ . '/../photo_source/';
$convert_exe = '/usr/bin/convert';
$filename       = array_shift( $str_arr );

$water_file     = 'water.3.png';
#$water_file    = '0930.water.png';

if ( $filename ==  'xt_' )
{
        $from_file      = $from_base . implode( '/', $str_arr );
        $command        = "$convert_exe $from_file -resize 236  $str";

} else if ( $filename == 'xf_' )
{
        $from_file      = $from_base . implode( '/', $str_arr );
        $command        = "$convert_exe $from_file $water_file  -gravity southea                                          st -geometry +10+10 -composite $str";

} else
{
        $from_file      = $from_base . $filename . '/' . implode( '/', $str_arr                                           );
        $command        = "$convert_exe $from_file -resize 550  $water_file -gra                                          vity southeast -geometry +10+10 -composite $str";
}
$out_dir        = str_replace( end( $str_arr ), '', $str );
$from_file      = str_replace( $replace_out , $replace_from , $from_file );

if ( ! file_exists( $from_file ) OR empty( $str )  )
        exit( header( 'Status: 404 Not Found' ) );

exec( "test -d $out_dir || mkdir -p $out_dir");
exec( $command );

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file_type =  finfo_file($finfo, $str );
finfo_close($finfo);

$str    = str_replace( $replace_out , $replace_from , $str );
header('content-type:' . $file_type );
header('X-Accel-Redirect: /img/'.$str);