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 相关文章推荐
VML绘图板②脚本--VMLgraph.js、XMLtool.js
Oct 09 PHP
基于PHP遍历数组的方法汇总分析
Jun 08 PHP
PHP系统命令函数使用分析
Jul 05 PHP
从PHP的源码中深入了解stdClass类
Apr 18 PHP
ThinkPHP框架实现session跨域问题的解决方法
Jul 01 PHP
PHP开发框架kohana3 自定义路由设置示例
Jul 14 PHP
PHP生成静态HTML页面最简单方法示例
Apr 09 PHP
php实现异步数据调用的方法
Dec 24 PHP
Docker 如何布置PHP开发环境
Jun 21 PHP
PHP进程通信基础之信号
Feb 19 PHP
PHP实现通过strace定位故障原因的方法
Apr 29 PHP
Yii支持多域名cors原理的实现
Dec 05 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 购物车完整实现代码
2014/06/05 PHP
完整删除ecshop中获取店铺信息的API
2014/12/24 PHP
php无法连接mysql数据库的正确解决方法
2016/07/01 PHP
Thinkphp5框架实现图片、音频和视频文件的上传功能详解
2019/08/27 PHP
PHP中迭代器的简单实现及Yii框架中的迭代器实现方法示例
2020/04/26 PHP
js no-repeat写法 背景不重复
2009/03/18 Javascript
javascript Keycode对照表
2009/10/24 Javascript
js格式化货币数据实现代码
2013/09/04 Javascript
jQuery移动web开发中的页面初始化与加载事件
2015/12/03 Javascript
jQuery绑定事件监听bind和移除事件监听unbind用法实例详解
2016/01/19 Javascript
微信小程序 引用其他js文件实现代码
2017/02/22 Javascript
vue服务端渲染的实例代码
2017/08/28 Javascript
浅谈Vue初学之props的驼峰命名
2018/07/19 Javascript
angular学习之动态创建表单的方法
2018/12/07 Javascript
Vuex 单状态库与多模块状态库详解
2018/12/11 Javascript
python中import学习备忘笔记
2017/01/24 Python
使用Python脚本和ADB命令实现卸载App
2017/02/10 Python
pygame实现简易飞机大战
2018/09/11 Python
python单例模式获取IP代理的方法详解
2018/09/13 Python
Python正则表达式和元字符详解
2018/11/29 Python
python-序列解包(对可迭代元素的快速取值方法)
2019/08/24 Python
CSS3圆角边框和边界图片效果实例
2016/07/01 HTML / CSS
加拿大拼图大师:Puzzle Master
2020/12/28 全球购物
杭州联环马网络笔试题面试题
2013/08/04 面试题
面临毕业的毕业生自荐书范文
2014/02/05 职场文书
房产代理公证处委托书
2014/04/04 职场文书
2014第二批党的群众路线教育实践活动对照检查材料思想汇报
2014/09/18 职场文书
高中校园广播稿
2014/10/21 职场文书
2014业务员年终工作总结
2014/12/09 职场文书
志愿者个人总结
2015/03/03 职场文书
2016元旦晚会主持词
2015/07/01 职场文书
老乡会致辞
2015/07/28 职场文书
小学班主任心得体会
2016/01/07 职场文书
我对PyTorch dataloader里的shuffle=True的理解
2021/05/20 Python
Python scrapy爬取起点中文网小说榜单
2021/06/13 Python
适合后台管理系统开发的12个前端框架(小结)
2021/06/29 Javascript