Home  >  Article  >  Backend Development  >  Example analysis of how to change image size in php

Example analysis of how to change image size in php

黄舟
黄舟Original
2017-07-26 13:43:181648browse

Changing the size of pictures is a very common functional requirement. Let’s start studying the method of changing picture size in PHP.

First introduce a function written by myself.

<?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格式图像路径 $imgdst jpg格式图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度 
//取得图片的宽度,高度值 
$arr = getimagesize($imgsrc); 
header("Content-type: image/jpg"); 
$imgWidth = $imgwidth; 
$imgHeight = $imgheight; 
// Create image and define colors 
$imgsrc = imagecreatefromjpeg($imgsrc); 
$image = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图 
imagecopyresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]); 
imagepng($image); 
imagedestroy($image); 
} 
?>

imagecopyresampled
imagecopyresampled -- Resample and resize part of the image.
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() Copy 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:

The code is as follows:

<?php 
// The file 
$filename = &#39;test.jpg&#39;; 
$percent = 0.5; 
// Content type 
header(&#39;Content-Type: image/jpeg&#39;); 
// 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:

<?php 
// The file 
$filename = &#39;test.jpg&#39;; 
// Set a maximum height and width 
$width = 200; 
$height = 200; 
// Content type 
header(&#39;Content-Type: image/jpeg&#39;); 
// 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 changes Image size method:
ImageCopyResized() function is valid in all GD versions, but its algorithm for scaling images is relatively 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:

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(&#39;php.jpg&#39;); 
$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(&#39;Content-Type : image/png&#39;); 
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 = getimagesize($img); 
if ($h = "auto") $h = $w/($imagedata[0]/$imagedata[1]);//根据原图的纵横比得出高度! 
$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 
/* 
* 图片缩略图 
* $srcfile 来源图片, 
* $rate 缩放比,默认为缩小一半,或者具体宽度象素值 
* $filename 输出图片文件名jpg 
* 例如: resizeimage("zt32.gif",0.1); 
* 例如: resizeimage("zt32.gif",250 ); 
* 说明:调用时直接把函数的结果放在HTML文件IMG标签中的SRC属性里 
*/ 
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; 
} 
//源图片的宽度和高度 
$srcw=imagesx($img); 
$srch=imagesy($img); 
//目的图片的宽度和高度 
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 "; 
//新建一个真彩色图像 
$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); 
// 以 JPEG 格式将图像输出到浏览器或文件 
if( $filename ) { 
//图片保存输出 
imagejpeg($im, $filename ); 
}else { 
//图片输出到浏览器 
imagejpeg($im); 
} 
//释放图片 
imagedestroy($im); 
imagedestroy($img); 
} 
?>


The above is the detailed content of Example analysis of how to change image size in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn