How to complete image scaling and cropping in PHP? (detailed examples)

WBOY
Release: 2023-04-10 17:54:02
Original
3433 people have browsed it

In the previous article, I brought you "How to draw a graphical verification code through PHP? ", which introduces how to draw text in PHP and how to draw graphic verification codes based on previous knowledge. In this article, let's take a look at how to scale and crop graphics. I hope it can help everyone. !

How to complete image scaling and cropping in PHP? (detailed examples)

We learned previously how to draw a graphical verification code, then let’s take a look at how to achieve image scaling and cropping in PHP operate. But before understanding the scaling and cropping of images, let’s first get to know the following two functions:

  • <strong>imagecopyresampled</strong><strong> </strong>: Used to resample copy part of the image and resize it

  • ##imagecopyresized<strong></strong> <strong></strong>: Used to copy part of the image and resize it

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, the difference is whether they copy part of the picture or the whole picture. Their syntax format is as follows:


imagecopyresampled ($目标图 ,$来源图,$目标开始的x位置,$目标开始的y位置,$来源开始的x位置,$来源开始的y位置,$目标图片的宽 ,$目标图片的高,$来源图片的宽 ,$来源图片的高 )
Copy after login

The specified width and height of the image starting from the starting point (x, y) of the source image. Place it to the starting point (x, y) of the target image and specify the width and height of the image.

Zoom picture

Zoom picture First we prepare a picture named dog.png:


How to complete image scaling and cropping in PHP? (detailed examples)

Our code needs to do the following steps to complete the scaling of the graphics:


  • Open the source image


  • Set the image scaling percentage (zoom)

  • Get the source image and adjust the size according to the ratio

  • Create a new specified The size of the image is the target image

  • Put the adjusted size of the source image into the target

  • Destroy the resource

The example is as follows:


<?php
//打开来源图片
$a = imagecreatefrompng(&#39;dog.png&#39;);
//定义百分比,缩放到0.1大小
$percent = 0.1;
// 将图片宽高获取到
list($width, $height) = getimagesize(&#39;dog.png&#39;);
//设置新的缩放的宽高
$new_width = $width * $percent;
$new_height = $height * $percent;
//创建新图片
$new_image = imagecreatetruecolor($new_width, $new_height);
//将原图$image按照指定的宽高,复制到$new_image指定的宽高大小中
imagecopyresampled($new_image, $a, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
header(&#39;content-type:image/jpeg&#39;);
imagejpeg($new_image);
?>
Copy after login

Output result:

How to complete image scaling and cropping in PHP? (detailed examples)

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


Crop the image

Crop the image, you need to use the

imagecopyresized function to achieve it. Next, let’s explain it through an example:

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

How to complete image scaling and cropping in PHP? (detailed examples)

Want it now Crop the logo image and place it on top of the dog image. How should this be achieved? The following are the main steps:

  • Open the source image and the target image

  • Intercept the points in the source image and set the width and height. into the target image. (Crop)

  • Save image input

  • Destroy resources

The actual operation is as follows:


In the logo picture, I want to crop out the text part and keep only the image part. We can know that the starting coordinates of the image part we want to keep are (0,0) and the ending coordinates are (52 , 59).

How to complete image scaling and cropping in PHP? (detailed examples)How to complete image scaling and cropping in PHP? (detailed examples)

Then we put the cropped picture in the upper left corner of the target picture, and the exact coordinates are from (0, 0) to (52, 59) , now that we have sorted out our ideas, the code is as follows:


<?php
 $dst = imagecreatefrompng(&#39;dog.png&#39;);
 $src = imagecreatefrompng(&#39;logo.png&#39;);
 imagecopyresized($dst, $src, 0, 0, 0, 0, 52, 59, 52, 59);
 header(&#39;content-type:image/jpeg&#39;); 
imagejpeg($dst); 
imagedestroy($dst);
 imagedestroy($src);
 ?>
Copy after login

Output result:

How to complete image scaling and cropping in PHP? (detailed examples)

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

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to complete image scaling and cropping in PHP? (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!