Copy codeThe Code is as follows: <? Php
// Define the size of the thumbnail.
$ PicSize = array (
'1970 _ 100' => 1,
'2017 _ 000000' => 1
);
$ ImagePath = "../image /";
Function parseUrl ($ url ){
Preg_match ("/(? P <name> [\ w \ d] +) _ w (? P <width> \ d +) _ h (? P 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 ('this size picture 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
*/
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 the 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 records logs
Exit (0 );
}
If (! @ Imagecopyresampled ($ new_img, $ orgImg, ($ picInfo ['width']-$ tW)/2, ($ picInfo ['height']-$ tH)/2, 0, 0, $ tW, $ tH, $ owidth, $ oheight )){
Echo "failed to generate image ";
Exit (0 );
}
// Generate an image
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; charsets = UTF-8 ");
Imagejpeg ($ new_img );
?>
Bind the handler of container enterror 404 of apache conf to this file ..