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. !
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
imagecopyresampled ($目标图 ,$来源图,$目标开始的x位置,$目标开始的y位置,$来源开始的x位置,$来源开始的y位置,$目标图片的宽 ,$目标图片的高,$来源图片的宽 ,$来源图片的高 )
Zoom picture
Zoom picture First we prepare a picture named dog.png:<?php //打开来源图片 $a = imagecreatefrompng('dog.png'); //定义百分比,缩放到0.1大小 $percent = 0.1; // 将图片宽高获取到 list($width, $height) = getimagesize('dog.png'); //设置新的缩放的宽高 $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('content-type:image/jpeg'); imagejpeg($new_image); ?>
Crop the image
imagecopyresized function to achieve it. Next, let’s explain it through an example:
<?php $dst = imagecreatefrompng('dog.png'); $src = imagecreatefrompng('logo.png'); imagecopyresized($dst, $src, 0, 0, 0, 0, 52, 59, 52, 59); header('content-type:image/jpeg'); imagejpeg($dst); imagedestroy($dst); imagedestroy($src); ?>
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!