How to generate thumbnails based on PHP server Images

Source: Internet
Author: User

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 ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.