php image scali...LOGIN

php image scaling and cropping technology

In the last chapter, we learned the watermarking technology of images. The watermarking technology is basically the same as the scaling and cropping technology we will learn in this chapter, except that the functions used have changed slightly.

The two commonly used functions are as follows:

Function nameFunction description
imagecopyresampledResample copy part of the image and resize
imagecopyresizedCopy part of the image and resize adjust size

Let’s take a look at these two functions. These two functions are not difficult to use. It's just that the parameters are a little complicated. And to complete the scaling or cropping of the image:

bool imagecopyresampled ( resource $ target image, resource $ source image, int $ x position where the target starts, int $ y position where the target starts, int $ source Starting x position, int $ source starting y position, int $ width of target image, int $ height of target image, int $ width of source image, int $ height of source image)

Please note that the parameters of the upper and lower images are the same.

bool imagecopyresized ( resource $ target image, resource $ source image, int $ target start x position, int $ target start y position, int $ source start x position, int $ source start y position, int $width of target image, int $height of target image, int $width of source image, int $height of source image)

The methods of image scaling and cropping are the same Yes, the difference is whether the entire picture or part of the picture is copied when copying.

Operation method description:
Start from the starting point (x, y) of the source image, and specify the width and height of the image. Place it to the starting point (x, y) of the target image and specify the width and height of the image.

1. Zoom in and out of the image, we will step by step


1. Open the source image

2. Set the image scaling percentage (zoom)

3. Get the source image and adjust the size accordingly

4. Create a new image of a specified size as the target image

5 .Put the adjusted size of the source image into the target

6.Destroy the resource

We will scale Fan Bingbing:

20161114147910942958296b354976a.png

<?php

//打开来源图片
$image = imagecreatefrompng('fbb.png');


//定义百分比,缩放到0.1大小
$percent = 0.1;


// 将图片宽高获取到
list($width, $height) = getimagesize('fbb.png');

//设置新的缩放的宽高
$new_width = $width * $percent;
$new_height = $height * $percent;

//创建新图片
$new_image = imagecreatetruecolor($new_width, $new_height);

//将原图$image按照指定的宽高,复制到$new_image指定的宽高大小中
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

header('content-type:image/jpeg');
imagejpeg($new_image);
?>

999.png

2. Crop the picture, we will divide the operation into steps:

1. Open the source picture and the target picture

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

3. Save the image and input it

4. Destroy the resource

We will cut out the word "degree" in the picture below and put it on the face of the husky:

Baidu logo:

document_2015-09-22_56010df4559d3.png

Husky picture:

10.png

Let’s sort out our ideas:

The starting coordinates of degree are x and y axes: 407, 154

The word degree starts from x, The width and height starting from the y coordinate are: 80, 89

The x and y axis positions of the husky’s face in the picture are: 281, 71

The x and y coordinates of the husky’s face in the picture Width and height are: 132, 160

We all know the coordinates and width and height. We start to follow the usage of the function and use the code to operate the picture:

<?php
 $dst = imagecreatefrompng('hsq.png');
 $src = imagecreatefrompng('du.png');
 imagecopyresized($dst, $src, 281, 71, 407, 154, 132, 160, 80, 90);
 header('content-type:image/jpeg'); 
imagejpeg($dst); 
imagedestroy($dst);
 imagedestroy($src);
 ?>

Let’s take a look at the experimental effect:

document_2015-09-22_56011105316ee.png

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