Simple php image proportional scaling code

Source: Internet
Author: User
Tags image identifier

I used getimagesize to get the size of the original image, and then x0.5 is to set the image/5.

Array getimagesize (string $ filename [, array & $ imageinfo])
The getimagesize () function will determine any gif, jpg, png, swf, swc, pstutorial d, tiff, bmp, iff, jp2, jpx, jb2, jpc, the size of the xbm or wbmp image file, the size of the returned image, the file type, and a height/width text string that can be used in the mark of a common html file.
If you cannot access the image specified by filename or the image is not valid, getimagesize () returns false and generates an e_warning error.

*/
// Define a file
Using filename='1.jpg ';
$ Percent = 0.5;
// Send the header file
Header ('content-type: image/jpeg ');
// Obtain the image size
List ($ width, $ height) = getimagesize ($ filename );
// Define the new size
$ New_width = $ width * $ percent;
$ New_height = $ height * $ percent;
$ Image_p = imagecreatetruecolor ($ new_width, $ new_height );
/*
: Int imagecreate (int x_size, int y_size );

Return Value: integer

Description


This function is used to create an empty graph. The x_size and y_size parameters are the image sizes in pixels ).

*/
$ Image = imagecreatefromjpeg ($ filename );

/*
Resource imagecreatefromjpeg (string filename) imagecreatefromjpeg () returns an image identifier, representing the image obtained from the given file name. Imagecreatefromjpeg () returns an empty string upon failure and outputs an error message. Unfortunately, it is displayed as a broken link in the browser. The following example generates an error jpeg to reduce debugging:
*/
Imagecopyresampled ($ image_p, $ image, 0, 0, 0, $ new_width, $ new_height, $ width, $ height );

/*
): Imagecopyresamples (), the image edge obtained by its pixel interpolation algorithm is relatively smooth. good quality (but this function is slower than imagecopyresized ). the parameters of the two functions are the same. imagecopyresampled (dest, src, dx, dy, sx, sy, dw, dh, sw, sh );
*/
// Output image
Imagejpeg ($ image_p, null, 100 );
/*
Run the code to scale the original image by 50% and output the new image.
*/

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.