Example _php instance of image scaling in PHP

Source: Internet
Author: User

Copy Code code as follows:

<?php
Picture's equal scaling

Because PHP can only operate on resources, copy the picture that needs to be scaled to create a new resource
$src =imagecreatefromjpeg (' a.jpg ');

Get the width and height of the source picture
$size _src=getimagesize (' a.jpg ');
$w = $size _src[' 0 '];
$h = $size _src[' 1 '];

Specifies the maximum width to zoom out (and possibly the height)
$max = 300;

Calculates the length of the other side based on the maximum value of 300, and gets the scaled picture width and height
if ($w > $h) {
$w = $max;
$h = $h * ($max/$size _src[' 0 ']);
}else{
$h = $max;
$w = $w * ($max/$size _src[' 1 ']);
}


Declare a $w wide, $h high true Color picture resources
$image =imagecreatetruecolor ($w, $h);


Key functions, parameters (target resource, source, target resource start coordinate x,y, source resource start coordinate x,y, target resource's width high w,h, source resource's width high w,h)
Imagecopyresampled ($image, $src, 0, 0, 0, 0, $w, $h, $size _src[' 0 '], $size _src[' 1 ']);

Tell the browser to parse it in the form of a picture
Header (' content-type:image/png ');
Imagepng ($image);

Destroying resources
Imagedestroy ($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.