非常好用的PHP图片处理函数

/*
* $o_photo 原图路径
* $d_photo 处理后图片路径
* $width 定义宽
* $height 定义高
* 调用方法 cutphoto("test.jpg","temp.jpg",256,146);
*/

function cutphoto($o_photo,$d_photo,$width,$height){
$o_width = imagesx($temp_img); //取得原图宽
$o_height = imagesy($temp_img); //取得原图高
//判断处理方法
if($width>$o_width || $height>$o_height){ //原图宽或高比规定的尺寸小,进行压缩
$newwidth=$o_width;
$newheight=$o_height;
if($o_width>$width){
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
}
if($newheight>$height){
$newwidth=$newwidth*$height/$newheight;
$newheight=$height;
}
//缩略图片
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
if($lsstr == '.jpg'){
imagejpeg($new_img , $d_photo);

}elseif($lsstr == '.gif'){
imagegif($new_img,$d_photo);
}elseif($lsstr=='.png'){
imagepng($new_img,$d_photo);
}else{
return false;
}
imagedestroy($new_img);

}else{ //原图宽与高都比规定尺寸大,进行压缩后裁剪
if($o_height*$width/$o_width>$height){ //先确定width与规定相同,如果height比规定大,则ok
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
$x=0;
$y=($newheight-$height)/2;
}else{ //否则确定height与规定相同,width自适应
$newwidth=$o_width*$height/$o_height;
$newheight=$height;
$x=($newwidth-$width)/2;
$y=0;
}
//缩略图片
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
if($lsstr == '.jpg'){
imagejpeg($new_img , $d_photo);
}elseif($lsstr == '.gif'){
imagegif($new_img,$d_photo);
}elseif($lsstr=='.png'){
imagepng($new_img,$d_photo);
}else{
return false;
}
imagedestroy($new_img);


}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值