Recently, I am working on thumbnails. I will share with you two methods $ source_path: Path of the source image $ NewImagePath: generating the thumbnail path $ target_width: width of the thumbnail $ target_height: the height of the thumbnail is currently working on the thumbnail. I 'd like to share with you two methods.
$ Source_path: source image path
$ NewImagePath: generate the thumbnail path
$ Target_width: thumbnail width
$ Target_height: Thumbnail height
$ Target_ratio) {$ cropped_width = $ source_width; $ cropped_height = $ source_width * $ target_ratio; $ source_x = 0; $ source_y = ($ source_height-$ cropped_height)/2 ;} // elseif ($ response <$ target_ratio) {$ cropped_width = $ source_height/$ target_ratio; $ cropped_height = $ source_height; $ source_x = ($ source_width-$ response) /2; $ source_y = 0;} // else {$ cropped_width = $ source_width; $ cropped_height = $ source_height; $ source_x = 0; $ source_y = 0 ;} switch ($ source_mime) {case 'image/gif ': $ source_image = imagecreatefromgif ($ source_path); break; case 'image/jpeg': $ source_image = imagecreatefromjpeg ($ source_path ); break; case 'image/png ': $ source_image = imagecreatefrompng ($ source_path); break; default: return false; break;} $ target_image = imagecreatetruecolor ($ target_width, $ target_height ); $ cropped_image = crop ($ cropped_width, $ cropped_height); // Image cropping imagecopy ($ cropped_image, $ source_image, 0, 0, $ source_x, $ source_y, $ cropped_width, $ cropped_height); // image scaling imagecopyresampled ($ target_image, $ cropped_image, 0, 0, 0, 0, $ target_width, $ target_height, $ cropped_width, $ cropped_height ); header ('content-Type: image/jpeg '); imagejpeg ($ target_image, $ NewImagePath, 100); imagedestroy ($ source_image); imagedestroy ($ target_image ); imagedestroy ($ cropped_image );}
The following method is used to generate a thumbnail and fill white edges.
= $ Width/$ height) {$ tmp_image_width = $ width; $ tmp_image_height = round ($ tmp_image_width * $ src_height/$ src_width);} else {$ tmp_image_height = $ height; $ tmp_image_width = round ($ tmp_image_height * $ src_width/$ src_height);} $ tmpImage = round ($ tmp_image_width, $ tmp_image_height); round ($ tmpImage, $ src_image, 0, 0, 0, 0, $ tmp_image_width, $ tmp_image_height, $ src_width, $ src_height); // add a white edge $ final_image = imagecreatetruecolor ($ width, $ height ); $ color = imagecolorallocate ($ final_image, 255,255,255); imagefill ($ final_image, 0, 0, $ color); $ x = round ($ width-$ tmp_image_width)/2 ); $ y = round ($ height-$ tmp_image_height)/2); imagecopy ($ final_image, $ tmpImage, $ x, $ y, 0, 0, $ tmp_image_width, $ tmp_image_height); // output image header ('content-Type: image/jpeg '); imagejpeg ($ final_image, $ NewImagePath, 100); imagedestroy ($ src_image ); imagedestroy ($ final_image );}
The above is (practical) the content of the method instance for generating thumbnails in PHP. For more information, see PHP Chinese network (www.php1.cn )!