PHP intercepts a specified Image size

Source: Internet
Author: User
This article mainly introduces the PHP method for intercepting a specified image size, which can be used to scale and intercept a specified image. it is a very practical technique, for more information about how to use PHP to intercept a specified image size, see the following example. Share it with you for your reference. The specific analysis is as follows:

Imagecopyresampled ($ newim, $ im, 0, 0, 7,174,120, 42, $ new_img_width, $ new_img_height); // The original size is 120x42

Imagecopyresampled ($ newim, $ im, 0, 0,100, 30,500,500, $ new_img_width, $ new_img_height); // place the image after it is captured to 500x500

Imagecopyresampled ($ newim, $ im, 0, 0,100, 30, 10, 10, $ new_img_width, $ new_img_height); // The image is truncated to 10x10.

This is probably the operation of this function. it is only good to check the parameter of the first original size. The following are the parameters of imagecopyresampled. the comparison is as follows:

$ Newim: imagecreate ($ new_img_width, $ new_img_height); blank layer created

$ Im: imagecreatefromjpeg ($ filename); read the original image

0: The x point starting from which the output graph starts. (the output vertex of the image can be specified. if it is not specified, it will be the layer color or graph you created)

0: start y of the output graph (same as above)

7: Start x of the original graph (specify the x point from which the image is to be captured)

174: start y of the original graph (specify the starting vertex of the source image, and set the block size based on the following two parameters)

120: how large the width is to be truncated. from the specified position above, the 120px width is intercepted. (if you want to zoom in and out, this is also the decision)

42: the size of the height to be captured. the height of 42px is intercepted from the position specified above (this is also determined if you want to zoom in and out)

$ New_img_width: this is a new graph. from the second set of parameters 0 and 0 written above, draw multiple widths (the specified size is used in this program currently)

$ New_img_height: this is a new graph. from the above second set of parameters 0, 0, how high is it to be drawn (the current program is the specified size)

The PHP instance code is as follows:

The code is as follows:

Header ("Content-type: image/jpeg ");

$ Filename = 'book_rabbit_rule.jpg ';
/* Read the image file */
$ Im = imagecreatefromjpeg ($ filename );
/* How much image is to be cut, length/width */
$ New_img_width = 120;
$ New_img_height = 42;
/* Create a new blank image first */
$ Newim = imagecreate ($ new_img_width, $ new_img_height );
// The output graph starts from x and y. the original graph starts from x and y. The size of x, y (resize), and the size of x and y.
Imagecopyresampled ($ newim, $ im, 0, 0, 7,174,120, 42, $ new_img_width, $ new_img_height );
/* Display a 500x500 chart */
// Imagecopyresampled ($ newim, $ im, 0, 0,100, 30,500,500, $ new_img_width, $ new_img_height );
/* Print the image */
Imagejpeg ($ newim );
/* Recycle Resources */
Imagedestroy ($ newim );
Imagedestroy ($ im );

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.