PHP image proportional Scaling

Source: Internet
Author: User
1 <? Php
2 // proportional scaling of the image
3
4 // because PHP can only operate on resources, you need to copy the image to be scaled and create a new resource.
5. src?imagecreatefrom=('a.jpg ');
6
7 // obtain the width and height of the source Image
8 $size_src1_getimagesize('a.jpg ');
9 $ w = $ size_src ['0'];
10 $ h = $ size_src ['1'];
11
12 // specify the maximum scaled width (or the height)
13 $ max = 300;
14
15 // calculate the length of the other side based on the maximum value of 300 to obtain the scaled image width and height.
16 if ($ w> $ h ){
17 $ w = $ max;
18 $ h = $ h * ($ max/$ size_src ['0']);
19} else {
20 $ h = $ max;
21 $ w = $ w * ($ max/$ size_src ['1']);
22}
23
24
25 // declare a $ w width, $ h high real color image resource
26 $ image = imagecreatetruecolor ($ W, $ H );
27
28
29 // key functions, parameters (Starting coordinates x and y of the target resource, source resource, and target resource, starting coordinates x and y of the source resource, width and height W, H, source resource width/height W, H)
30 imagecopyresampled ($ image, $ SRC, 0, 0, 0, $ W, $ H, $ size_src ['0'], $ size_src ['1']);
31
32 // tell the browser to parse the image
33 header ('content-type: image/PNG ');
34 imagepng ($ image );
35
36 // destroy resources
37 imagedestroy ($ image );
38
39?>

Take a note of the course and use a blog to record it.

I have been in touch with PHP for a short time. I have made many suggestions during the writing process. However, I am not very easy to learn. It is inevitable that there are mistakes. If there are mistakes, I would like to ask you to correct them. I am very grateful!

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.