How to implement a scaled image in PHP

Source: Internet
Author: User
This paper mainly introduces how to use PHP and other scales to scale the image, and can propose the width and length of scaling required for the original image.

This article introduced the PHP implementation picture compression method, the reader may according to the specific application reference or to improve, directly on the code, Imgzip ($SRC, $newwid, $newhei) This function takes in the original picture, the scale request width, the scale length.

<?php//Compress picture thumbnail $src= "xiezheng.jpg"; $newwid =640; $newhei = 480;function Imgzip ($src, $newwid, $newhei) {$imgInfo = GetImageSize ($SRC); $imgType = Image_type_to_extension ($imgInfo [2], false); $fun = "imagecreatefrom{$imgType}";//Declare picture open picture in memory $image = $fun ($SRC);//Convenient configuration length width, height, set box is variable wid, height is hei $wid = $imgInfo [0 ]; $hei = $imgInfo [1];//to determine length and width to facilitate equal-to-scale scaling, specifications according to the above, the ($wid > $hei) {$wid = $newwid; $hei = $newwid/($wid/$hei);} else {$wid = $newhei * ($wid/$hei); $hei = $newhei;} Create an image in memory $images 2 = Imagecreatetruecolor ($newwid, $newhei); Create a 500*320 image//Copy the original image to the new picture//imagecopyresampled ($dst _image, $src _image, $dst _x, $dst _y, $src _x, $src _y, $dst _w, $ Dst_h, $src _w, $src _h) imagecopyresampled ($images 2, $image, 0, 0, 0, 0, $wid, $hei, $imgInfo [0], $imgInfo [1]);//Destroy original picture Imag Edestroy ($image);//Direct Output image file header ("Content-type:". $imgInfo [' MIME ']); Imagejpeg ($images 2);//Save the picture to the new file imagejpeg ($images 2, ' new.jpg ', 100); 10 code output picture quality 0-100 100 quality highest//Destroy Imagedestroy ($images 2);} Imgzip ($SRC, $newwid, $newhei);?> 

Below to share is the use of PHP implementation of the picture and other proportional scaling code, the small partners can integrate it into the image upload

New file index.php, need to have a picture in the statistics directory is q.jpg (can change the name of the picture according to the source code)

The source code is as follows:

<?php$filename= "Q.jpg", $per =0.3;list ($width, $height) =getimagesize ($filename); $n _w= $width * $per; $n _h= $height * $per, $new =imagecreatetruecolor ($n _w, $n _h), $img =imagecreatefromjpeg ($filename);//Copy part of the image and adjust imagecopyresized ($new , $img, 0, 0,0, 0, $n _w, $n _h, $width, $height);//image output new picture, Save as imagejpeg ($new, "q1.jpg"); Imagedestroy ($new); Imagedestroy ( $IMG);? >

After using the browser to run, in the index.php sibling directory will have a q1.jpg, this picture is the scale of the image, the path can be changed in the source code itself, put in their own projects or write a method also line

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.