For websites with fixed width and high requirements, direct Thumbnails cannot solve the problem, because proportional scaling width or height is not fixed.
For websites with fixed width and high requirements, direct Thumbnails cannot solve the problem, because proportional scaling width or height is not fixed, however, the website still requires that the image width and height be fixed. what should we do? there is only one way to first scale down the image proportion. the scaling ratio depends on the width and height of the image, then crop the middle image from the thumbnail, and the cropped image is the fixed image required by the website. The following code is used:
Need php to enable GD2
Echo thumb_dblxr ('./2.png', '144', '94', 'test1.png ');
// Proportional scaling
Function thumb_dblxr ($ backimg, $ width, $ height, $ newsfile ){
List ($ s_w, $ s_h, $ exten) = getimagesize ($ backimg );
// Proportional fixed algorithm
// $ Test = $ s_w/144> $ s_h/94? 144: 94;
// Echo $ test; die;
If ($ width & ($ s_w/$ width)> ($ s_h/$ height )){
$ Width = ($ height/$ s_h) * $ s_w;
} Else {
$ Height = ($ width/$ s_w) * $ s_h;
}
Echo $ width ."
";
Echo $ height; die;
$ New = imagecreatetruecolor ($ width, $ height); // create a true color.
// Process the GD library functions that are not needed based on the obtained extension.
If ($ exten = 1 ){
$ Old = imagecreatefromgif ($ backimg );
} Elseif ($ exten = 2 ){
$ Old = imagecreatefromjpeg ($ backimg );
} Elseif ($ exten = 3 ){
$ Old = imagecreatefrompng ($ backimg );
}
// Processing of transparent gif background
$ Otcs = imagecolortransparent ($ old );
If ($ otcs> = 0 & $ otcs <imagecolorstotal ($ old )){
$ Tran = imagecolorsforindex ($ old, $ otcs );
Print_r ($ tran );
$ Newtran = imagecolorallocate ($ new, $ tran ["red"], $ tran ["green"], $ tran ["blue"]);
Imagefill ($ new, 0, 0, $ newtran );
Imagecolortransparent ($ new, $ newtran );
}
Imagecopyresampled ($ new, $ old, 0, 0, 0, $ width, $ height, $ s_w, $ s_h );
// Process the GD library functions that are not needed based on the obtained extension.
If ($ exten = 1 ){
Imagegif ($ new, $ newsfile );
} Elseif ($ exten = 2 ){
Imagejpeg ($ new, $ newsfile );
} Elseif ($ exten = 3 ){
Imagepng ($ new, $ newsfile );
}
Imagedestroy ($ new );
Imagedestroy ($ old );
}
// Crop images
// $ Backimg = './test1.png ';
List ($ w, $ h, $ exten) = getimagesize ($ backimg );
$ X = ($ WS-144)/2;
$ Y = ($ h-99)/2;
Echo cut_thumb ('./test1.png', XX, policy,'144', '94', 'tt.png ');
Function cut_thumb ($ backimg, $ cut_x, $ cut_y, $ cut_width, $ cut_height, $ newfile ){
List ($ c_w, $ c_h, $ c_exten) = getimagesize ($ backimg );
If ($ c_exten = 1 ){
$ Back = imagecreatefromgif ($ backimg );
} Elseif ($ c_exten = 2 ){
$ Back = imagecreatefromjpeg ($ backimg );
} Elseif ($ c_exten = 3 ){
$ Back = imagecreatefrompng ($ backimg );
}
$ C_new = imagecreatetruecolor ($ cut_width, $ cut_height );
Imagecopyresampled ($ c_new, $ back, 0, 0, $ cut_x, $ cut_y, $ cut_width, $ cut_height, $ cut_width, $ cut_height );
If ($ c_exten = 1 ){
Imagegif ($ c_new, $ newfile );
} Elseif ($ c_exten = 2 ){
Imagejpeg ($ c_new, $ newfile );
} Elseif ($ c_exten = 3 ){
Imagepng ($ c_new, $ newfile );
}
Imagedestroy ($ back );
Imagedestroy ($ c_new );
}
?>