Code to change the size of a picture in PHP _php tutorial

Source: Internet
Author: User
Tags imagejpeg
Let's start by describing a function that you write yourself.
Copy CodeThe code is as follows:
$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 to change $imgheight the height to change
Gets 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:
Copy CodeThe code is as follows:
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, 100);
?>

Example 2:
Copy CodeThe code is as follows:
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, 100);
?>

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:
Copy CodeThe 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:
Copy CodeThe code is as follows:
$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
Copy CodeThe code is as follows:
function Resize_jpg ($img, $w, $h) {
$thumb = Imagecreate ($w, $h);
$image = Imagecreatefromjpeg ($img);
$imagedata = getimagesize ($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:
Copy CodeThe code is as follows:
/*
* Thumbnail image
* $srcfile source picture,
* $rate zoom ratio, the default is half, or the specific width pixel value
* $filename output image 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.
*/
function Resizeimage ($srcfile, $rate =.5, $filename = "") {
$size =getimagesize ($srcfile);
Switch ($size [2]) {
Case 1:
$img =imagecreatefromgif ($srcfile);
Break
Case 2:
$img =imagecreatefromjpeg ($srcfile);
Break
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 image
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 images to a browser or file in JPEG format
if ($filename) {
Picture Save output
Imagejpeg ($im, $filename);
}else {
Picture output to Browser
Imagejpeg ($im);
}
Release picture
Imagedestroy ($im);
Imagedestroy ($IMG);
}
?>

http://www.bkjia.com/PHPjc/324010.html www.bkjia.com true http://www.bkjia.com/PHPjc/324010.html techarticle let's start by describing a function that you write yourself. Copy the code as follows: PHP $imgsrc = "http://www.nowamagic.net/images/3.jpg"; $width = 780; $height = 420; Resizejpg ($IMGSRC, $IMGDST, $width ...

  • 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.