用PHP改变图片的尺寸大小

改变图片的尺寸是一个很常见的功能需求,下面开始研究下关于PHP改变图片尺寸的方法。先介绍一个自己写的函数。

01 <?php
02  
04 $width = 780;
05 $height = 420;
06  
07 resizejpg($imgsrc,$imgdst,$width,$height);
08  
09 function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight)
10 {
11     //$imgsrc jpg格式图像路径 $imgdst jpg格式图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度
12     //取得图片的宽度,高度值
13     $arr getimagesize($imgsrc);                    
14     header("Content-type: image/jpg");
15      
16     $imgWidth $imgwidth;
17     $imgHeight $imgheight;
18     // Create image and define colors
19     $imgsrc = imagecreatefromjpeg($imgsrc);
20     $image = imagecreatetruecolor($imgWidth$imgHeight);  //创建一个彩色的底图
21     imagecopyresampled($image$imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]);
22     imagepng($image);
23     imagedestroy($image);
24 }
25  
26 ?>
imagecopyresampled

imagecopyresampled -- 重采样拷贝部分图像并调整大小。

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() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。dst_im 和 src_im 分别是目标图像和源图像的标识符。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果 dst_im 和 src_im 相同的话)区域,但如果区域交迭的话则结果不可预知。

注: 因为调色板图像限制(255+1 种颜色)有个问题。重采样或过滤图像通常需要多于 255 种颜色,计算新的被重采样的像素及其颜色时采用了一种近似值。对调色板图像尝试分配一个新颜色时,如果失败我们选择了计算结果最接近(理论上)的颜色。这并不总是视觉上最接近的颜色。这可能会产生怪异的结果,例如空白(或者视觉上是空白)的图像。要跳过这个问题,请使用真彩色图像作为目标图像,例如用 imagecreatetruecolor() 创建的。

注: imagecopyresampled() 需要 GD 2.0.l 或更高版本。

一个简单的示例:

01 <?php
02 // The file
03 $filename 'test.jpg';
04 $percent = 0.5;
05  
06 // Content type
07 header('Content-Type: image/jpeg');
08  
09 // Get new dimensions
10 list($width$height) = getimagesize($filename);
11 $new_width $width $percent;
12 $new_height $height $percent;
13  
14 // Resample
15 $image_p = imagecreatetruecolor($new_width$new_height);
16 $image = imagecreatefromjpeg($filename);
17 imagecopyresampled($image_p$image, 0, 0, 0, 0, $new_width$new_height$width$height);
18  
19 // Output
20 imagejpeg($image_p, null, 100);
21 ?>

示例2:

01 <?php
02 // The file
03 $filename 'test.jpg';
04  
05 // Set a maximum height and width
06 $width = 200;
07 $height = 200;
08  
09 // Content type
10 header('Content-Type: image/jpeg');
11  
12 // Get new dimensions
13 list($width_orig$height_orig) = getimagesize($filename);
14  
15 $ratio_orig $width_orig/$height_orig;
16  
17 if ($width/$height $ratio_orig) {
18    $width $height*$ratio_orig;
19 else {
20    $height $width/$ratio_orig;
21 }
22  
23 // Resample
24 $image_p = imagecreatetruecolor($width$height);
25 $image = imagecreatefromjpeg($filename);
26 imagecopyresampled($image_p$image, 0, 0, 0, 0, $width$height$width_orig$height_orig);
27  
28 // Output
29 imagejpeg($image_p, null, 100);
30 ?>

有两种改变图像大小的方法:

  • ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。
  • ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑。(但该函数的速度比 ImageCopyResized() 慢)。

两个函数的参数是一样的,如下:

1 imageCopyResampled(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);
2 imageCopyResized(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);

例子:

01 <?PHP
02 $src = ImageCreateFromJPEG('php.jpg');
03 $width = ImageSx($src);
04 $height = ImageSy($src);
05 $x $widht/2;
06 $y $height/2;
07 $dst = ImageCreateTrueColor($x,$y);
08 ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$widht,$height);
09 header('Content-Type : image/png');
10 ImagePNG($det);
11 ?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值