Get the App
SLTechnology News&Howtos  ›  Development  › 

How to generate thumbnails in php without distortion

Shulou Source: shulou.com Published: 2022-06-01 19:51:20 10月03日 Update

Today I will show you how php generates thumbnails without distortion. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How does php generate thumbnails without distortion?

Use the php function to generate undistorted code for picture thumbnails

/ / generate thumbnails without distortion / * generate thumbnails * $imgSrc picture source path * $resize_width picture width * $resize_height picture height * $dstimg thumbnail path * $isCut whether to cut the picture * / function reSizeImg ($imgSrc, $resize_width, $resize_height, $dstimg $isCut = false) {/ / the type of picture $type = substr (strrchr ($imgSrc, "."), 1) / / initialize the image if ($type = = "jpg" | | $type = = "jpeg") {$im = imagecreatefromjpeg ($imgSrc);} if ($type = = "gif") {$im = imagecreatefromgif ($imgSrc);} if ($type = "png") {$im = imagecreatefrompng ($imgSrc);} $width = imagesx ($im) $height = imagesy ($im); / / generate image / / the ratio of the changed image $resize_ratio = ($resize_width) / ($resize_height); / / the ratio of the actual image $ratio = ($width) / ($height) If (($isCut) = = 1) {if ($ratio > = $resize_ratio) {/ / High priority $newimg = imagecreatetruecolor ($resize_width, $resize_height); imagecopyresampled ($newimg, $im, 0,0,0,0, $resize_width, $resize_height, (($height) * $resize_ratio), $height); ImageJpeg ($newimg, $dstimg) } if ($ratio

< $resize_ratio) { //宽度优先 $newimg = imagecreatetruecolor($resize_width, $resize_height); imagecopyresampled($newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, $width, (($width) / $resize_ratio)); ImageJpeg($newimg, $dstimg); } } else { if ($ratio >

= $resize_ratio) {$newimg = imagecreatetruecolor ($resize_width, ($resize_width) / $ratio); imagecopyresampled ($newimg, $im, 0,0,0,0, $resize_width, ($resize_width) / $ratio, $width, $height); ImageJpeg ($newimg, $dstimg) } if ($ratio < $resize_ratio) {$newimg = imagecreatetruecolor (($resize_height) * $ratio, $resize_height); imagecopyresampled ($newimg, $im, 0,0,0,0, ($resize_height) * $ratio, $resize_height, $width, $height); ImageJpeg ($newimg, $dstimg);} ImageDestroy ($im) } $result = reSizeImg ('images/15M.jpg', 1280, 1280 camera thumbpin Xiao15.jpg'); this is how php generates thumbnails without distortion, and more content related to how php generates thumbnails without distortion can search the previous articles or browse the following articles to learn! I believe the editor will add more knowledge to you. I hope you can support it!

Tags: Generation picture distortion image content article more scale path height good code function actual width that is ideas friends environment computer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Xiaomi Redmi Huawei