<? PHP /* * @ Author: Xiaojiang * @ time: 2013-04-25 */ // Define the size of a thumbnail. $ Picsize = Array ( '1970 _ 000000' => 1, '1970 _ 000000' => 1 ); $ ImagePath = "../Image /" ; Function Parseurl ( $ URL ){ Preg_match ("/(? P <Name> [\ W \ D] +) _ w (? P <width> \ D +) _ H (? P $ URL , $ Match ); Return $ Match ;} $ Urlarr = Explode ("/", $ _ Server ['Request _ URI' ]); $ Imgname =$ Urlarr [ Count ( $ Urlarr )-1 ]; $ Picinfo = Parseurl ( $ Imgname ); // Error size If ( Empty ( $ Picinfo ['Width']) | Empty ( $ Picinfo ['Height']) |!Array_key_exists ( $ Picinfo ['Width']. '_'. $ Picinfo ['Height'], $ Picsize )) Die ('The image of this dimension does not exist' ); $ Originalpic = $ ImagePath . $ Picinfo ['Name']. '/'. $ Picinfo ['Name']. '.'. $ Picinfo ['Ext' ]; // The original image does not exist. If (! File_exists ( $ Originalpic )) Die ("The image does not exist! " ); /* ** Proportional compression of images */ Switch ( $ Picinfo ['Ext' ]) { Case 'Jpg ': $ Orgimg = Imagecreatefromjpeg ($ Originalpic ); Break ; Default : Break ;} $ Owidth = Imagesx ( $ Orgimg ); // Original size $ Oheight = Imagesy ( $ Orgimg ); $ TW =$ Picinfo ['Width' ]; $ Th = $ Picinfo ['Height' ]; // Obtain the thumbnail size If ( $ Owidth / $ Oheight > $ TW / $ Th ){ $ Th = Intval ( $ TW * $ Oheight / $ Owidth );} Else { $ TW = Intval ( $ Th * $ Owidth / $ Oheight );} // Generate background image $ New_img = Imagecreatetruecolor ($ Picinfo ['Width'], $ Picinfo ['Height' ]); $ Bgcolor = Imagecolorallocate ( $ New_img , 255,255,255 ); If (! @ Imagefilledrectangle ( $ New_img , 0, 0, $ Picinfo ['Width']-1, $ Picinfo ['Height']-1, $ Bgcolor )){ Echo "Cannot create background image "; // @ Todo logs Exit (0 );} If (! @ Imagecopyresampled ( $ New_img , $ Orgimg ,( $ Picinfo ['Width']- $ TW )/2 ,( $ Picinfo ['Height']- $ Th )/2, 0, 0, $ TW , $ Th ,$ Owidth , $ Oheight )){ Echo "An error occurred while generating the image" ; Exit (0 );} // Generating Images Ob_start (); Imagejpeg ( $ New_img ); $ _ Newimg = Ob_get_contents (); Ob_end_clean (); File_put_contents ( $ ImagePath . $ Picinfo ['Name']. "/". $ Imgname , $ _ Newimg ); Header ("Content-Type: image/JPEG; charset = UTF-8" ); Imagejpeg ( $ New_img ); ?>
Bind the handler of container enterror 404 of Apache conf to this file ..