header("Content-type: image/jpeg");
$filename = 'book_rabbit_rule.jpg';
/* Read image file */
$im = imagecreatefromjpeg($filename) ;
/* How much should the image be cut, length/width */
$new_img_width = 120;
$new_img_height = 42;
/* First create a new blank image file */
$newim = imagecreate($new_img_width, $new_img_height);
// From which side should the output image start x, y , where should the original image start x, y , how big should the drawing be x, y (resize) , Capture how big x, y
imagecopyresampled($newim, $im, 0, 0, 7, 174, 120, 42, $new_img_width, $new_img_height);
/* Enlarge to 500 x 500 image */
// imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width, $new_img_height);
/* Print the image */
imagejpeg($newim) ;
/* Resource recovery */
imagedestroy($newim);
imagedestroy($im);
?>
All the comments that should be included in the file have been written (Source code is placed at the bottom of this article), just explain the main parameters:
imagecopyresampled($newim, $im, 0, 0, 7, 174, 120, 42, $new_img_width, $new_img_height ); // Original size 120 x 42
imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width, $new_img_height); // Cut out the image and place it at 500
imagecopyresampled($newim, $im, 0, 0, 100, 30, 10, 10, $new_img_width, $new_img_height); // After the image is cut out, it is reduced to 10 x 10
Probably mainly The operation of this function is just a comparison of the parameters of the first original size. The following are the parameters of imagecopyresampled. Compare them one by one as follows:
$newim: imagecreate($new_img_width, $new_img_height); Create Blank layer
$im: imagecreatefromjpeg($filename); Read in the original image
0: The x point from which side the output image should start (the output point of the image can be specified, but it is not specified) The position will be the layer color or image you create)
0: The y point from which side the output image should start (same as above)
7: The y point from which the original image should start (specify which side Start intercepting at point x)
174: From which side of the original image y (specify which point to start intercepting, the interception block size is determined by the following two parameters)
120: How wide the width of the screenshot should be, From the specified position above, start to intercept 120px wide (if you want to zoom in and out, it is also determined here)
42: How high the height of the screenshot should be, start from the specified position above, start to intercept 42px in height (if you want to zoom in and out, it is also determined here) Determined here)
$new_img_width: This is a new picture. From the second set of parameters 0, 0 written above, how wide should you start drawing (in this program, the size of the screenshot is currently specified)
$new_img_height : This is a new picture. From the second set of parameters 0, 0 above, how high should you start drawing (in this program, the size of the screenshot is currently specified)