PHP 裁剪保留原透明度、原尺寸函数
代码介绍
PHP 裁剪保留原透明度、原尺寸函数
代码:
/**
* 重置图片文件大小
* @param string $filePath 文件路径
* @param int $xmax 最大宽度
* @param int $ymax 最大高度
* @return boolean true/false
*/
ResetImageFileSize('C:UsersAdministratorDesktop .46574.png',100,100);
function ResetImageFileSize($filePath, $xmax, $ymax)
{
if(filesize($filePath) == 0) return 'error_file';
//$extension = pathinfo($filePath)['extension'];
$extension = explode('/',getimagesize($filePath)['mime'])[1];
$img = null;
switch ($extension) {
case 'jpg':
case 'jpeg':
$img = imagecreatefromjpeg($filePath);
break;
case 'png':
$img = imagecreatefrompng($filePath);
break;
case 'gif':
$img = imagecreatefromgif($filePath);
break;
case 'webp':
$img = imagecreatefromwebp($filePath);
break;
}
if(is_null($img)) return false;
list($x,$y) = getimagesize($filePath);
if($x <= $xmax && $y <= $ymax){
return true;
}
/*保留原宽高比率*/
if($x >= $y) {
$newX = ($x > $xmax) ? $xmax : $x;
$newY = $newX * ($y / $x);
}else{
$newY = ($y > $ymax) ? $ymax : $y;
$newX = ($x / $y) * $newY;
}
$img2 = imagecreatetruecolor($newX, $newY);
imageantialias($img2,true);//使用抗锯齿
if($extension == 'png'){
$Color = imagecolorallocatealpha($img2, 0, 0, 0, 127);//设置透明
}else{
$Color = imagecolorallocate($img2,255,255,255);
}
imagecolortransparent($img2,$Color);
imagefill($img2,0,0,$Color);
if(function_exists('imagecopyresampled')){
/*生成图像质量较好,但速度相比较慢*/
imagecopyresampled($img2, $img, 0, 0, 0, 0, floor($newX), floor($newY), $x, $y);
}else{
/*生成图像质量较差,但速度相比较快*/
imagecopyresized($img2, $img, 0, 0, 0, 0, floor($newX), floor($newY), $x, $y);
}
switch ($extension) {
case 'jpg':
case 'jpeg':
imagejpeg($img2,$filePath,100);
break;
case 'png':
imagesavealpha($img2,true);
imagepng($img2,$filePath);
//imagepng($img2,$filePath,9);
break;
case 'gif':
imagegif($img2,$filePath);
break;
default:
imagejpeg($img2,$filePath,100);
break;
}
imagedestroy($img2);
return true;
}
特效介绍 jQuery图片列表和分类菜单ui布局,鼠标悬停窗口图片全图上下滚动预览效果代码。 演示截图 相关推荐: 网页歌词搜索音乐代码代码介绍 利用网易云音乐api实现的可搜索歌词与歌曲列表的歌词搜索器代码。 演示截图 相关推荐: 在线查询QQ等级信息和测凶…
关注微信公众号『OOINK』
第一时间了解最新资源动态关注OO.INK不迷路~
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 下载资源需要解压密码,解压密码是什么?
1、本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
OO.INK资源网 » PHP 裁剪保留原透明度、原尺寸函数
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
OO.INK资源网 » PHP 裁剪保留原透明度、原尺寸函数