Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate thumbnails in php without distortion

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report