Php Image Resize图片大小调整的函数代码
作者:bea
代码如下: function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) { if($dst_width <1 || $dst_height <1) { echo "params width or height error !"; exit(); } if(!file_exists($src_file)) { echo $src_file . " is
代码如下:
function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) {
if($dst_width <1 || $dst_height <1) {
echo "params width or height error !";
exit();
}
if(!file_exists($src_file)) {
echo $src_file . " is not exists !";
exit();
}
$type=exif_imagetype($src_file);
$support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);
if(!in_array($type, $support_type,true)) {
echo "this type of image does not support! only support jpg , gif or png";
exit();
}
switch($type) {
case IMAGETYPE_JPEG :
$src_img=imagecreatefromjpeg($src_file);
break;
case IMAGETYPE_PNG :
$src_img=imagecreatefrompng($src_file);
break;
case IMAGETYPE_GIF :
$src_img=imagecreatefromgif($src_file);
break;
default:
echo "Load image error!";
exit();
}
$src_w=imagesx($src_img);
$src_h=imagesy($src_img);
$ratio_w=1.0 * $dst_width/$src_w;
$ratio_h=1.0 * $dst_height/$src_h;
if ($src_w<=$dst_width && $src_h<=$dst_height) {
$x = ($dst_width-$src_w)/2;
$y = ($dst_height-$src_h)/2;
$new_img=imagecreatetruecolor($dst_width,$dst_height);
imagecopy($new_img,$src_img,$x,$y,0,0,$dst_width,$dst_height);
switch($type) {
case IMAGETYPE_JPEG :
imagejpeg($new_img,$dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($new_img,$dst_file);
break;
case IMAGETYPE_GIF :
imagegif($new_img,$dst_file);
break;
default:
break;
}
} else {
$dstwh = $dst_width/$dst_height;
$srcwh = $src_w/$src_h;
if ($ratio_w <= $ratio_h) {
$zoom_w = $dst_width;
$zoom_h = $zoom_w*($src_h/$src_w);
} else {
$zoom_h = $dst_height;
$zoom_w = $zoom_h*($src_w/$src_h);
}
$zoom_img=imagecreatetruecolor($zoom_w, $zoom_h);
imagecopyresampled($zoom_img,$src_img,0,0,0,0,$zoom_w,$zoom_h,$src_w,$src_h);
$new_img=imagecreatetruecolor($dst_width,$dst_height);
$x = ($dst_width-$zoom_w)/2;
$y = ($dst_height-$zoom_h)/2+1;
imagecopy($new_img,$zoom_img,$x,$y,0,0,$dst_width,$dst_height);
switch($type) {
case IMAGETYPE_JPEG :
imagejpeg($new_img,$dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($new_img,$dst_file);
break;
case IMAGETYPE_GIF :
imagegif($new_img,$dst_file);
break;
default:
break;
}
}
}
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- javascript,php获取函数参数对象的代码
- php下将多个数组合并成一个数组的方法与实例代码
- PHP操作数组相关函数
- php5 apache 2.2 webservice 创建与配置(java)
- 使用XDebug调试及单元测试覆盖率分析
- 仿AS3实现PHP 事件机制实现代码
- PHP 命名空间实例说明
- PHP中利用substr_replace将指定两位置之间的字符替换为*号
- PHP Session_Regenerate_ID函数双释放内存破坏漏洞
- PHP中通过加号合并数组的一个简单方法分享
- PHP中删除变量时unset()和null的区别分析
- PHP企业级应用之常见缓存技术篇
- 兼容性最强的PHP生成缩略图的函数代码(修改版)
- PHP统计目录下的文件总数及代码行数(去除注释及空行)
- php短域名转换为实际域名函数
- PHP学习笔记之三 数据库基本操作
- PHP学习笔记之二
- PHP学习笔记之一
- php下连接mssql2005的代码