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 complete image zooming and cropping in PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to complete image scaling and cropping in PHP". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to zoom and crop images in PHP.

In the previous we know how to draw the graphic CAPTCHA, then let's take a look at how to achieve image scaling and cropping in PHP. But before we learn about image scaling and cropping, let's take a look at the following two functions:

Imagecopyresampled: used to resample and resize part of the copied image

Imagecopyresized: used to copy and resize part of the image

These two functions copy the image and can be used to complete the scaling or cropping of the image. Their syntax format parameters are the same, except that the picture they copy is partial or whole. Their grammatical format is as follows:

Imagecopyresampled ($target map, $source map, x position of $target start, y position of $target start, x position of $source start, y position of $source start, width of $target picture, height of $source picture, width of $source picture, height of $source picture)

The specified size of the width and height of the picture, starting from the starting point of the source picture (xrecoery y). Put it into the starting point of the target picture (xrecoery y) and specify the width and height size of the picture.

Zoom the picture

First of all, let's prepare a picture called dog.png:

Our code needs to do the following steps to complete the zoom of the drawing:

Open the source picture

Set the zoom percentage of the picture (zoom)

Get the source picture and resize it according to the ratio

Create a new picture of a specified size as the target image

In the future, the resized source image will be put into the target.

Destroy resources

Examples are as follows:

Output result:

From the above results, the scaling of the image is completed through the imagecopyresampledh function.

Crop a picture

To crop a picture, you need to use the imagecopyresized function. Next, let's explain it with an example:

I have two pictures named dog.png and logo.png.

Now you want to cut some logo pictures and put them on dog pictures, how should you achieve that? Here are the main steps:

Open source picture and target picture

Intercept the points in the source picture and set the width and height. Put it in the target picture. (cut)

Save picture input

Destroy resources

The actual operation is as follows:

In the logo picture, I want to cut out the text part and keep only the image part. We can know that the coordinate of the beginning of the image part we want to keep is (0re0) and the coordinate of the end is (52).

Then we put the cropped image in the upper-left corner of the target image, and the coordinates are exactly from (0d0) to (52recovery59). Now that the idea has been sorted out, the code is as follows:

Output result:

From the above results, we have completed the image clipping through the imagecopyresizedh function.

At this point, I believe you have a deeper understanding of "how to complete image zooming and cropping in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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