Introduce a function that you wrote first.
Copy Code code as follows:
<?php
$IMGSRC = "Yun_qi_img/dontlinkme.png";
$width = 780;
$height = 420;
Resizejpg ($IMGSRC, $IMGDST, $width, $height);
function Resizejpg ($IMGSRC, $IMGDST, $imgwidth, $imgheight)
{
$IMGSRC jpg format image path $imgdst jpg image save file name $imgwidth width to change $imgheight 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 bottom chart
Imagecopyresampled ($image, $imgsrc, 0, 0, 0, 0, $imgWidth, $imgHeight, $arr [0], $arr [1]);
Imagepng ($image);
Imagedestroy ($image);
}
?>
Imagecopyresampled
imagecopyresampled-resample Copy part 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 to another image, inserts a pixel value smoothly, and therefore, in particular, reduces the size of the image and still retains great clarity. Dst_im and Src_im are the identifiers of 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 (if Dst_im and src_im the same) area within the same picture, but the result is unpredictable if the area overlaps.
Note: There is a problem with the palette image limitations (255+1 color). Resampling or filtering an image usually requires more than 255 colors, and an approximate value is used to compute the new resampling pixel and its color. When trying to assign a new color to a palette image, if it fails we choose the closest (theoretically) color to the computed result. This is not always the closest visual color. This can produce bizarre results, such as blank (or visually blank) images. To skip this issue, use a true color image as the target image, for example, with Imagecreatetruecolor ().
Note: imagecopyresampled () is required for GD 2.0.L or later.
A simple example:
Copy Code code 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, 100);
?>
Example 2:
Copy Code code as follows:
<?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, 100);
?>
There are two ways to change the size of an image:
The imagecopyresized () function is valid in all versions of GD, but the algorithm for scaling the image is rather rough.
Imagecopyresamples (), its pixel interpolation algorithm to get the image edge is relatively smooth. (But the function is slower than imagecopyresized ()).
The parameters of the two functions are the same, as follows:
Copy Code code 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 Code code as follows:
? 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 a JPG image file in PHP
Copy Code code 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 Code code as follows:
<?php
/*
* Thumbnail image
* $srcfile Source Pictures,
* $rate Scaling, the default is to shrink by half, or specific width pixel values
* $filename output picture filename jpg
* For example: Resizeimage ("Zt32.gif", 0.1);
* For example: Resizeimage ("Zt32.gif", 250);
* Description: Directly put the result of the function 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 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 an image 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);
}
?>