php函数缩放图片大小,PHP图片重新采样及大小缩放函数imagecopyresampled用法实例

(PHP 4 >= 4.0.6, PHP 5)

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

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

函数参数如下:

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

今天根据这个函数写了个方法,能够等比缩放图片的大小,并且可以设置宽度固定高度等比缩放,或者高度固定宽度等比缩放,或者设置最大宽和最大高。具体代码如下:

/**

* 图片缩放函数(可设置高度固定,宽度固定或者最大宽高,支持gif/jpg/png三种类型)

* Author : Specs

* Homepage: https://9iphp.com

*

* @param string $source_path 源图片

* @param int $target_width 目标宽度

* @param int $target_height 目标高度

* @param string $fixed_orig 锁定宽高(可选参数 width、height或者空值)

* @return string

*/

function myImageResize($source_path, $target_width = 200, $target_height = 200, $fixed_orig = ''){

$source_info = getimagesize($source_path);

$source_width = $source_info[0];

$source_height = $source_info[1];

$source_mime = $source_info['mime'];

$ratio_orig = $source_width / $source_height;

if ($fixed_orig == 'width'){

//宽度固定

$target_height = $target_width / $ratio_orig;

}elseif ($fixed_orig == 'height'){

//高度固定

$target_width = $target_height * $ratio_orig;

}else{

//最大宽或最大高

if ($target_width / $target_height > $ratio_orig){

$target_width = $target_height * $ratio_orig;

}else{

$target_height = $target_width / $ratio_orig;

}

}

switch ($source_mime){

case 'image/gif':

$source_image = imagecreatefromgif($source_path);

break;

case 'image/jpeg':

$source_image = imagecreatefromjpeg($source_path);

break;

case 'image/png':

$source_image = imagecreatefrompng($source_path);

break;

default:

return false;

break;

}

$target_image = imagecreatetruecolor($target_width, $target_height);

imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $target_width, $target_height, $source_width, $source_height);

//header('Content-type: image/jpeg');

$imgArr = explode('.', $source_path);

$target_path = $imgArr[0] . '_new.' . $imgArr[1];

imagejpeg($target_image, $target_path, 100);

}

用法如下:

myImageResize($filename, 200, 200); //最大宽高

myImageResize($filename, 200, 200, 'width'); //宽度固定

myImageResize($filename, 200, 200, 'height'); //高度固定

宽图结果如下:

7ca939922c78bec830cb34afc6dbff4b.png(原图)

242ec73034f6f2ca1687de19e0f14fc7.png

最大宽高

6113a4389f2ad19ba6ea372cc9ed7f5f.png 固定高

d806282cd16589d88fc7453ecf705d32.png

固定宽

高图结果如下:

8c9b32919c575f9dc26c9288c97d9b28.png

原图

f03ff05ace6c9a673356a5cefaa34bc7.png

最大宽高

038a201d9a501c5488a805374420c233.png

固定高

b14045d2c7b15622cb2cf666b900e077.png

固定宽

附:把图片剪切为固定大小的函数:

function imagecropper($source_path, $target_width, $target_height){

$source_info = getimagesize($source_path);

$source_width = $source_info[0];

$source_height = $source_info[1];

$source_mime = $source_info['mime'];

$source_ratio = $source_height / $source_width;

$target_ratio = $target_height / $target_width;

// 源图过高

if ($source_ratio > $target_ratio){

$cropped_width = $source_width;

$cropped_height = $source_width * $target_ratio;

$source_x = 0;

$source_y = ($source_height - $cropped_height) / 2;

}elseif ($source_ratio < $target_ratio){ // 源图过宽

$cropped_width = $source_height / $target_ratio;

$cropped_height = $source_height;

$source_x = ($source_width - $cropped_width) / 2;

$source_y = 0;

}else{ // 源图适中

$cropped_width = $source_width;

$cropped_height = $source_height;

$source_x = 0;

$source_y = 0;

}

switch ($source_mime){

case 'image/gif':

$source_image = imagecreatefromgif($source_path);

break;

case 'image/jpeg':

$source_image = imagecreatefromjpeg($source_path);

break;

case 'image/png':

$source_image = imagecreatefrompng($source_path);

break;

default:

return false;

break;

}

$target_image = imagecreatetruecolor($target_width, $target_height);

$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

// 裁剪

imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);

// 缩放

imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

$dotpos = strrpos($source_path, '.');

$imgName = substr($source_path, 0, $dotpos);

$suffix = substr($source_path, $dotpos);

$imgNew = $imgName . '_small' . $suffix;

imagejpeg($target_image, $imgNew, 100);

imagedestroy($source_image);

imagedestroy($target_image);

imagedestroy($cropped_image);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值