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 use gd Library to realize the function of picture clipping and thumbnail image generation on php server

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

Share

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

This article mainly introduces how to use gd library to achieve php server picture clipping and thumbnail generation function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Crop example:

The final cut into a picture:

Among them, the dotted frame is the picture to be cut out, and finally saved into a 100-wide picture. The code is as follows:

$src_path = '1.jpg'

/ / create an instance of the source diagram

$src = imagecreatefromstring (file_get_contents ($src_path))

/ / coordinates of the point in the upper left corner of the cropping area

$x = 100

$y = 12

/ / width and height of the crop region

$width = 200

$height = 200

/ / finally save the width and height of the image in proportion to the source, otherwise it will be deformed

$final_width = 100

$final_height = round ($final_width * $height / $width)

/ / copy the crop region to the new picture and scale or pull it up according to the width and height of the source and target

$new_image = imagecreatetruecolor ($final_width, $final_height)

Imagecopyresampled ($new_image, $src, 0,0, $x, $y, $final_width, $final_height, $width, $height)

/ / output pictures

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

Imagejpeg ($new_image)

Imagedestroy ($src)

Imagedestroy ($new_image)

In fact, if the coordinate is (0d0) and the width and height of the crop area is the same as that of the source image, then it is the function of generating thumbnails.

Thank you for reading this article carefully. I hope the article "how to use gd library to achieve picture clipping and thumbnail generation on php server" shared by the editor is helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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