Php Image Resize图片大小调整的函数代码


Posted in PHP onJanuary 17, 2011
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; 
} 
} 
}
PHP 相关文章推荐
php添加文章时生成静态HTML文章的实现代码
Feb 17 PHP
PHP重定向的3种方式
Mar 07 PHP
php把session写入数据库示例
Feb 26 PHP
php实现批量压缩图片文件大小的脚本
Jul 04 PHP
PHP防盗链代码实例
Aug 27 PHP
php实现的一个简单json rpc框架实例
Mar 30 PHP
CI框架出现mysql数据库连接资源无法释放的解决方法
May 17 PHP
php操纵mysqli数据库的实现方法
Sep 18 PHP
完美解决thinkphp唯一索引重复时出错的问题
Mar 31 PHP
深入理解PHP的远程多会话调试
Sep 21 PHP
ThinkPHP框架实现定时执行任务的两种方法分析
Sep 04 PHP
PHP 计算两个时间段之间交集的天数示例
Oct 24 PHP
php生成随机密码的几种方法
Jan 17 #PHP
PHP校验ISBN码的函数代码
Jan 17 #PHP
PHP中用正则表达式清除字符串的空白
Jan 17 #PHP
php开发环境配置记录
Jan 14 #PHP
PHP文件读写操作之文件写入代码
Jan 13 #PHP
PHP文件读写操作之文件读取方法详解
Jan 13 #PHP
PHP目录函数实现创建、读取目录教程实例
Jan 13 #PHP
You might like
php采集时被封ip的解决方法
2010/08/29 PHP
php 注释规范
2012/03/29 PHP
php中3种方法统计字符串中每种字符的个数并排序
2012/08/27 PHP
php 删除cookie方法详解
2014/12/01 PHP
php实现字符串反转输出的方法
2015/03/14 PHP
php实现生成验证码实例分享
2016/04/10 PHP
thinkphp3.2.0 setInc方法 源码全面解析
2018/01/29 PHP
javascript学习笔记(十九) 节点的操作实现代码
2012/06/20 Javascript
JS控制按钮10秒钟后可用的方法
2015/12/22 Javascript
D3.js实现文本的换行详解
2016/10/14 Javascript
轻松理解JavaScript闭包
2017/03/14 Javascript
React router动态加载组件之适配器模式的应用详解
2018/09/12 Javascript
20道JS原理题助你面试一臂之力(必看)
2019/07/22 Javascript
Layui Table js 模拟选中checkbox的例子
2019/09/03 Javascript
Layui数据表格判断编辑输入的值,是否为我需要的类型详解
2019/10/26 Javascript
js回到页面指定位置的三种方式
2020/12/17 Javascript
基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件功能
2021/02/23 Vue.js
vue实现桌面向网页拖动文件的示例代码(可显示图片/音频/视频)
2021/03/01 Vue.js
[56:56]VG vs LGD 2019国际邀请赛淘汰赛 胜者组 BO3 第一场 8.22
2019/09/05 DOTA
python网络编程学习笔记(四):域名系统
2014/06/09 Python
Python字典,函数,全局变量代码解析
2017/12/18 Python
pandas 将list切分后存入DataFrame中的实例
2018/07/03 Python
Python之使用adb shell命令启动应用的方法详解
2019/01/07 Python
python 下 CMake 安装配置 OPENCV 4.1.1的方法
2019/09/30 Python
python安装gdal的两种方法
2019/10/29 Python
Centos7下源码安装Python3 及shell 脚本自动安装Python3的教程
2020/03/07 Python
django 读取图片到页面实例
2020/03/27 Python
HTML5 Video/Audio播放本地文件示例介绍
2013/11/18 HTML / CSS
HTML5添加鼠标悬浮音响效果不使用FLASH
2014/04/23 HTML / CSS
高级人员简历的自我评价分享
2013/11/03 职场文书
教师网络培训感言
2014/03/09 职场文书
2014年小学英语教师工作总
2014/12/03 职场文书
2014年大学宣传部工作总结
2014/12/19 职场文书
男方婚前保证书
2015/02/28 职场文书
2019下半年英语教师的教学工作计划(3篇)
2019/09/25 职场文书
MongoDB orm框架的注意事项及简单使用
2021/06/20 MongoDB