Let me first introduce a function I wrote myself.
Copy code The 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 format image path $imgdst jpg format image save file name $imgwidth width to be changed $imgheight height to be changed
//Get the width and height of the image
$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 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() takes a square in an image Regions are copied into another image, smoothly interpolating pixel values and thus, inter alia, reducing the size of the image while still maintaining great sharpness. dst_im and src_im are the identifiers of the target image and source image respectively. If the source and destination have different widths and heights, the image shrinks and stretches accordingly. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if dst_im and src_im are the same), but the results are unpredictable if the regions overlap.
Note: There is a problem because of the palette image limit (255+1 colors). Resampling or filtering an image often requires more than 255 colors, and an approximation is used in calculating the new resampled pixels and their colors. When trying to assign a new color to a palette image, if that fails we choose the closest (theoretically) calculated color. This is not always the visually closest color. This can produce weird results, such as blank (or visually blank) images. To bypass this problem, use a truecolor image as the target image, such as one created with imagecreatetruecolor().
Note: imagecopyresampled() requires GD 2.0.l or higher.
A simple example:
Copy the code The 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 code The 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 is valid in all GD versions, but its algorithm for scaling images is rough.
ImageCopyResamples(), the image edges obtained by its pixel interpolation algorithm are relatively smooth. (But this function is slower than ImageCopyResized()).
The parameters of the two functions are the same, as follows:
Copy code 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:
Copy code The 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 jpg image file in php The size of
Copy code The 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]);//Get the height based on 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 The code is as follows:
/*
* Image thumbnail
* $srcfile source image,
* $rate scaling ratio , the default is to reduce it by half, or the specific width pixel value
* $filename Output image file name jpg
* For example: resizeimage("zt32.gif",0.1);
* For example: resizeimage("zt32 .gif",250 );
* Note: When calling, place the result of the function directly in the SRC attribute in the IMG tag of the HTML file
*/
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 image
$srcw=imagesx($img);
$srch=imagesy($img);
//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 the image to the browser or file in JPEG format
if( $filename ) {
// Image saving and output
imagejpeg($im, $filename);
}else {
//Output the image to the browser
imagejpeg($im);
}
//Release Image
imagedestroy($im);
imagedestroy($img);
}
?>
http://www.bkjia.com/PHPjc/324010.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324010.htmlTechArticleFirst introduce a function written by myself. Copy the code as follows: ?php $imgsrc = "http://www.nowamagic.net/images/3.jpg"; $width = 780; $height = 420; resizejpg($imgsrc,$imgdst,$width.. .