Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use gd Library to realize the function of picture clipping and thumbnail image generation on php server

Shulou Source: shulou.com Published: 2022-06-03 08:39:55 10月02日 Update

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!

Tags: Pictures areas articles functions generation services coordinates that is source drawings code values interests at the same time examples upper-left corner more friends goals knowledge examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL Huawei NVidia macOS