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 sprintf()函数让你的sql操作更安全
Jul 23 PHP
php ftp文件上传函数(基础版)
Jun 03 PHP
php中require和require_once的区别说明
Feb 27 PHP
php判断字符串在另一个字符串位置的方法
Feb 27 PHP
codeigniter框架The URI you submitted has disallowed characters错误解决方法
May 06 PHP
PHP+Memcache实现wordpress访问总数统计(非插件)
Jul 04 PHP
PHP中empty和isset对于参数结构的判断及empty()和isset()的区别
Nov 15 PHP
Yii视图CGridView实现操作按钮定义地址示例
Jul 14 PHP
PHP验证码类ValidateCode解析
Jan 07 PHP
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
Apr 27 PHP
PHP简单实现二维数组的矩阵转置操作示例
Nov 24 PHP
PHP基于双向链表与排序操作实现的会员排名功能示例
Dec 26 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
smarty中先strip_tags过滤html标签后truncate截取文章运用
2010/10/25 PHP
PHP版 汉字转码的实现详解
2013/06/09 PHP
PHP 开发者该知道的 5 个 Composer 小技巧
2016/02/03 PHP
yii2使用ajax返回json的实现方法
2016/05/14 PHP
PHP导出带样式的Excel示例代码
2016/08/28 PHP
thinkphp3.2实现在线留言提交验证码功能
2017/07/19 PHP
PHP面向对象五大原则之接口隔离原则(ISP)详解
2018/04/04 PHP
php 可变函数使用小结
2018/06/12 PHP
javascript闭包的理解和实例
2010/08/12 Javascript
window.open以post方式将内容提交到新窗口
2012/12/26 Javascript
javascript使用prototype完成单继承
2014/12/24 Javascript
微信小程序 wxapp内容组件 icon详细介绍
2016/10/31 Javascript
nodejs基础应用
2017/02/03 NodeJs
基于vue.js快速搭建图书管理平台
2017/10/29 Javascript
页面点击小红心js实现代码
2018/05/26 Javascript
Vue.js 2.x之组件的定义和注册图文详解
2018/06/19 Javascript
JS使用队列对数组排列,基数排序算法示例
2019/03/02 Javascript
js实现图片3D轮播效果
2019/09/21 Javascript
vue路由跳转传递参数的方式总结
2020/05/10 Javascript
2分钟实现一个Vue实时直播系统的示例代码
2020/06/05 Javascript
[00:12]2018DOTA2亚洲邀请赛 Sccc亮相SOLO赛,今年他又会有什么样的战绩?
2018/04/06 DOTA
Python random模块(获取随机数)常用方法和使用例子
2014/05/13 Python
Python中多线程thread与threading的实现方法
2014/08/18 Python
Python中的index()方法使用教程
2015/05/18 Python
Python正则表达式非贪婪、多行匹配功能示例
2017/08/08 Python
python文件拆分与重组实例
2018/12/10 Python
Python可视化mhd格式和raw格式的医学图像并保存的方法
2019/01/24 Python
Python3使用PySynth制作音乐的方法
2019/09/09 Python
乐观自信演讲稿范文
2014/05/21 职场文书
2014标准社保办理委托书
2014/10/06 职场文书
党员转正大会主持词
2015/07/02 职场文书
学者《孟子》名人名言
2019/08/09 职场文书
成人成长感言如何写?
2019/08/16 职场文书
利用html+css实现菜单栏缓慢下拉效果的示例代码
2021/03/30 HTML / CSS
基于Python的EasyGUI学习实践
2021/05/07 Python
分享Python异步爬取知乎热榜
2022/04/12 Python