The question of generating a thumbnail in PHP is as follows .... I set the size of the thumbnail to be generated to 100*50 my original image is 1000*600 I want to scale the original image proportionally, horizontally or vertically centered. other places are filled in white. I don't know what I mean. I don't know if the expression is clear. The new w1h1 figure w2h2ifw1/h1 & gt; w2/h2 // The problem that PHP will generate a thumbnail after zooming.
I think so ....
I set the size of the thumbnail to be generated to 100*50
My original image is 1000*600
I want to scale the original image proportionally, horizontally or vertically in the center. Other places are filled in white. I don't know what I mean.
------ Solution --------------------
SOURCE image w1 h1
Figure w2 h2
If w1/h1> w2/h2 // The scaled height is insufficient.
New figure height h = h2/w2 * w1
Offset y = (h2-h)/2 in vertical residence
X = 0
If w1/h1 <w2/h2 // The scaled width is insufficient.
New image width w = w2/h2 * h1
Offset x = (x2-x)/2 in horizontal residence
Y = 0
------ Solution --------------------
Image processing of a cms
PHP code
_ Construct ($ targetfile, $ region, $ pai_watermarktext, $ photo_waterpos, $ region, $ photo_wheight, $ photo_wwidth, $ pai_watermarktype, $ photo_marktrans, $ trueMarkimg, $ attach );} // destructor function _ construct ($ targetfile, $ response, $ pai_watermarktext, $ photo_waterpos, $ response, $ photo_wheight, $ photo_wwidth, $ pai_watermarktype, $ photo_marktrans, $ trueMarkimg, $ attach = array ()){ $ This-> thumbstatus = $ pai_thumb; $ this-> watermarktext = $ pai_watermarktext; $ this-> watermarkstatus = $ photo_waterpos; $ this-> watermarkquality = $ photo_marktrans; $ this-> watermarkminwidth = $ photo_wwidth; $ this-> watermarkminheight = $ photo_wheight; $ this-> watermarktype = $ cfg_watermarktype; $ this-> watermarktrans = $ photo_diaphaneity; $ this-> animatedgif = 0; $ this-> targetfile = $ targetfile; $ this-> Attachinfo = @ getimagesize ($ targetfile); $ this-> attach = $ attach; switch ($ this-> attachinfo ['Mime ']) {case 'image/jpeg ': $ this-> imagecreatefromfunc = function_exists ('imagecreatefromjpeg ')? 'Imagecreatefromjpeg ': ''; $ this-> imagefunc = function_exists ('imagejpeg ')? 'Imagejpeg ': ''; break; case 'image/GIF': $ this-> imagecreatefromfunc = function_exists ('imagecreatefromgif ')? 'Imagecreatefromgif ': ''; $ this-> imagefunc = function_exists ('imagegif ')? 'Imagegif': ''; break; case 'image/PNG': $ this-> imagecreatefromfunc = function_exists ('imagecreatefrompng ')? 'Imagecreatefrompng ': ''; $ this-> imagefunc = function_exists ('imagepng ')? 'Imagepng ': ''; break ;} // if it is null, the function of the matching type does not exist. $ this-> attach ['size'] = empty ($ this-> attach ['size'])? @ Filesize ($ targetfile): $ this-> attach ['size']; if ($ this-> attachinfo ['Mime '] = 'image/GIF ') {$ fp = fopen ($ targetfile, 'RB'); $ targetfilecontent = fread ($ fp, $ this-> attach ['size']); fclose ($ fp ); $ this-> animatedgif = strpos ($ targetfilecontent, 'netscape2. 0') = false? 0: 1 ;}} /*** generate a thumbnail ** @ access public * @ param int $ thumbwidth image width * @ param int $ thumbheight Image height * @ param int $ preview * @ return void * /function thumb ($ thumbwidth, $ thumbheight, $ preview = 0) {$ this-> thumb_gd ($ thumbwidth, $ thumbheight, $ preview ); if ($ this-> thumbstatus = 2 & $ this-> watermarkstatus) {$ this-> image ($ this-> targetfile, $ this-> attach ); $ this-> attach ['size'] = filesize ($ th Is-> targetfile );}} /*** use gd to generate a thumbnail ** @ access public * @ param int $ thumbwidth image width * @ param int $ thumbheight Image height * @ param int $ preview * @ return void */function thumb_gd ($ thumbwidth, $ thumbheight, $ preview = 0) {if ($ this-> thumbstatus & function_exists ('imagecreatetruecolor') & function_exists ('imagecopyresampled ') & amp; function_exists ('imagejpeg ') {$ imagecreatefromfunc = $ this-> imagec Reatefromfunc; $ imagefunc = $ this-> thumbstatus = 1? 'Imagejpeg ': $ this-> imagefunc; list ($ imagewidth, $ imageheight) = $ this-> attachinfo; if (! $ This-> animatedgif & ($ imagewidth >=$ thumbwidth | $ imageheight >=$ thumbheight) {$ attach_photo = $ imagecreatefromfunc ($ this-> targetfile ); $ x_ratio = $ thumbwidth/$ imagewidth; $ y_ratio = $ thumbheight/$ imageheight; if ($ x_ratio * $ imageheight) <$ thumbheight) {$ thumb ['height'] = ceil ($ x_ratio * $ imageheight); $ thumb ['width'] = $ thumbwidth ;} else {$ thumb ['width'] = ceil ($ y_ratio * $ imagewid Th); $ thumb ['height'] = $ thumbheight;} $ targetfile =! $ Preview? ($ This-> thumbstatus = 1? $ This-> targetfile.'.thumb.jpg ': $ this-> targetfile ):'. /watermark_tmp.jpg '; $ thumb_photo = second ($ thumb ['width'], $ thumb ['height']); imagecopyresampled ($ thumb_photo, $ attach_photo, 0, 0, 0, 0, $ thumb ['width'], $ thumb ['height'], $ imagewidth, $ imageheight ); if ($ this-> attachinfo ['Mime '] = 'image/jpeg') {$ imagefunc ($ thumb_photo, $ targetfile, 100 );} else {$ imagefunc ($ thumb_photo, $ t Argetfile) ;}$ this-> attach ['thumb'] = $ this-> thumbstatus = 1? 1: 0 ;}}}// End Class