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

Php realizes the function of generating thumbnails and filling white edges.

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "php to achieve the function of generating thumbnails to fill white edges". In daily operation, I believe that many people have doubts in php to achieve the function of generating thumbnails to fill white edges. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for everyone to answer the questions of "php to achieve the function of generating thumbnails to fill white edges." Next, please follow the editor to study!

It should be a very common function to generate thumbnails after uploading pictures on the website. generally speaking, in order to show beauty on the website, thumbnails will be of the same size. for example, in a recent site made by the author, the specification requirements for thumbnails are 160 × 120. However, if the proportion of the uploaded picture is not consistent with the thumbnail, direct zooming will cause the image to deform, which must not be a good experience. So the author thought of a compromise, that is, the method of reducing the size and adding white edges.

Source drawing, size is 600 × 366:

The resulting effect image:

The code is relatively long, so here are some simple ideas:

First, the source image is scaled to generate a thumbnail with a width of no more than 160 and a height of no more than 120. For example, the picture above will turn it into a 160 × 98 thumbnail.

Create a new 160x120 white background image and center the thumbnail generated in the previous step on this image and OK it.

The final code is as follows:

The copy code is as follows:

/ / the path of the source image, which can be a local file or a remote picture

$src_path = '1.jpg'

/ / finally save the width of the picture

$width = 160

/ / the height of the final saved picture

$height = 120

/ / Source image object

$src_image = imagecreatefromstring (file_get_contents ($src_path))

$src_width = imagesx ($src_image)

$src_height = imagesy ($src_image)

/ / generate proportional thumbnails

$tmp_image_width = 0

$tmp_image_height = 0

If ($src_width / $src_height > = $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 = imagecreatetruecolor ($tmp_image_width, $tmp_image_height)

Imagecopyresampled ($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 pictures

Header ('Content-Type: image/jpeg')

Imagejpeg ($final_image)

At this point, the study on "php realizes the function of generating thumbnails to fill white edges" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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