Example analysis of how to change image size in PHP

Source: Internet
Author: User
Tags imagejpeg save file
Changing the size of a picture is a common feature requirement, and the following is a look at how PHP changes the size of a picture.

Let's start by describing a function that you write yourself.

<?php $imgsrc = "Http://www.nowamagic.net/images/3.jpg"; $width = 780; $height = 420; Resizejpg ($IMGSRC, $IMGDST, $width, $height); function Resizejpg ($IMGSRC, $IMGDST, $imgwidth, $imgheight) {//$imgsrc jpg image path $imgdst jpg format image save file name $imgwidth the width you want to change ImgHeight to change the height//Get the width of the picture, the height value $arr = getimagesize ($IMGSRC); Header ("content-type:image/jpg"); $imgWidth = $imgwidth; $imgHeight = $imgheight; Create image and define colors $IMGSRC = Imagecreatefromjpeg ($IMGSRC); $image = Imagecreatetruecolor ($imgWidth, $imgHeight); Create a color Basemap imagecopyresampled ($image, $imgsrc, 0, 0, 0, 0, $imgWidth, $imgHeight, $arr [0], $arr [1]); Imagepng ($image); Imagedestroy ($image); }?>

Imagecopyresampled
Imagecopyresampled-Resample The copied portion of the image and resize it.
int imagecopyresampled (Resource Dst_im, resource src_im, int dstX, int dsty, int srcx, int srcy, int dstw, int dsth, int SRCW, int SrcH)
Imagecopyresampled () copies a square area of an image into another image, inserting the pixel values smoothly, thus, in particular, reducing the size of the image and still maintaining great clarity. Dst_im and Src_im are identifiers for the target image and the source image, respectively. If the width and height of the source and destination are different, the image is shrunk and stretched accordingly. The coordinates refer to the upper-left corner. This function can be used to copy the area within the same image (if Dst_im and Src_im are identical), but the result is unpredictable if the area overlaps.
Note: There is a problem with palette image limitations (255+1 colors). Resampling or filtering images typically require more than 255 colors, and an approximation is used to calculate the new resampled pixels and their colors. When trying to assign a new color to a palette image, if it fails we select the closest (theoretical) color to the computed result. This is not always the most visually close color. This can produce bizarre results, such as blank (or visually blank) images. To skip this problem, use a true color image as the target image, for example, created with Imagecreatetruecolor ().
Note: imagecopyresampled () requires GD 2.0.l or higher.
A simple example:

The code is as follows:

<?php//The file $filename = ' test.jpg '; $percent = 0.5; Content Type header (' Content-type:image/jpeg '); Get New Dimensions list ($width, $height) = getimagesize ($filename); $new _width = $width * $percent; $new _height = $height * $percent; Resample $image _p = Imagecreatetruecolor ($new _width, $new _height); $image = Imagecreatefromjpeg ($filename); Imagecopyresampled ($image _p, $image, 0, 0, 0, 0, $new _width, $new _height, $width, $height); Output imagejpeg ($image _p, NULL,?>);


Example 2:

<?php//The file $filename = ' test.jpg '; Set a maximum height and width $width = 200; $height = 200; Content Type header (' Content-type:image/jpeg '); Get New Dimensions list ($width _orig, $height _orig) = getimagesize ($filename); $ratio _orig = $width _orig/$height _orig; if ($width/$height > $ratio _orig) {$width = $height * $ratio _orig;} else {$height = $width/$ratio _orig;}//Resample $image _p = Imagecreatetruecolor ($width, $height); $image = Imagecreatefromjpeg ($filename); Imagecopyresampled ($image _p, $image, 0, 0, 0, 0, $width, $height, $width _orig, $height _orig); Output imagejpeg ($image _p, NULL,?>);


There are two ways to change the image size:
The imagecopyresized () function works in all GD versions, but its algorithm for scaling images is coarser.
Imagecopyresamples (), its pixel interpolation algorithm obtains the image edge to be smooth. (But the function is slower than imagecopyresized ().)
The parameters of the two functions are the same, as follows:

The code is as follows:

Imagecopyresampled (DEST,SRC,DY,DX,SX,SY,DW,DH,SW,SH); Imagecopyresized (DEST,SRC,DY,DX,SX,SY,DW,DH,SW,SH);


Example:

<? PHP $src = imagecreatefromjpeg (' php.jpg '); $width = Imagesx ($SRC); $height = Imagesy ($SRC); $x = $WIDHT/2; $y = $height/2; $DST = Imagecreatetruecolor ($x, $y); Imagecopyresampled ($DST, $SRC, 0,0,0,0, $x, $y, $WIDHT, $height); Header (' content-type:image/png '); Imagepng ($det);?>


How to change the size of JPG image files in PHP

The code is as follows:

<? function Resize_jpg ($img, $w, $h) {//$thumb = Imagecreate ($w, $h); $image = Imagecreatefromjpeg ($img); $imagedata = Getima Gesize ($IMG); if ($h = "Auto") $h = $w/($imagedata [0]/$imagedata [1]);//height according to the aspect ratio of the original image! $thumb = Imagecreatetruecolor ($w, $h); Imagecopyresized ($thumb, $image, 0, 0, 0, 0, $w, $h, $imagedata [0], $imagedata [1]); Imagejpeg ($THUMB); }//resize_jpg ($file, $w, $h); Resize_jpg ("Images/dsc01244.jpg", 100,100); Imagedestory ($THUMB); Imagedestory ($image);?>


Function code:

<?php/* * Picture thumbnail * $srcfile source image, * $rate zoom ratio, default to half, or specific width pixel value * $filename output picture filename jpg * For example: Resizeimage ("Zt32.gif", 0.1 ); * For example: Resizeimage ("Zt32.gif", 250); * Description: The result of the function is placed directly in the SRC attribute of the HTML file img tag ($srcfile, $rate =.5, $filename = "") {$size =getimagesize ($ resizeimage) Srcfile); Switch ($size [2]) {Case 1: $img =imagecreatefromgif ($srcfile), break, Case 2: $img =imagecreatefromjpeg ($srcfile); Case 3: $img =imagecreatefrompng ($srcfile); Break Default:exit; }//The width and height of the source picture $SRCW =imagesx ($img); $srch =imagesy ($IMG); The width and height of the destination picture if ($size [0] <= $rate | | $size [1] <= $rate) {$DSTW = $SRCW; $dsth = $srch;} else{if ($rate <= 1) {$DSTW =floor ($SRCW * $rate); $dsth =floor ($srch * $rate);} else {$DSTW = $rate; $rate = $rate/$SRCW; $dsth =floor ($srch * $rate);}} echo "$DSTW, $dsth, $SRCW, $srch"; Create a new True color image $im =imagecreatetruecolor ($DSTW, $dsth); $black =imagecolorallocate ($im, 255,255,255); Imagefilledrectangle ($im, 0,0, $DSTW, $dsth, $black); Imagecopyresized ($im, $img, 0,0,0,0, $DSTW, $dsth, $SRCW, $srch); Output the image in JPEG format to the browser or file if ($filename) {//Picture save output imagejpeg ($im, $filename);} else {//Picture output to Browser imagejpeg ($IM);}//Release picture Imagedestroy ($im); Imagedestroy ($IMG); }?>

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.