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文档的代码
Apr 12 PHP
Sorting Array Values in PHP(数组排序)
Sep 15 PHP
需要注意的几个PHP漏洞小结
Feb 05 PHP
深入array multisort排序原理的详解
Jun 18 PHP
实现获取http内容的php函数分享
Feb 16 PHP
thinkphp使用literal防止模板标签被解析的方法
Nov 22 PHP
PHP实现阿里大鱼短信验证的实例代码
Jul 10 PHP
php实现socket推送技术的示例
Dec 20 PHP
PHP中“=&gt;
Mar 01 PHP
Laravel 中使用简单的方法跟踪用户是否在线(推荐)
Oct 30 PHP
Laravel等框架模型关联的可用性浅析
Dec 15 PHP
php操作redis常见方法示例【key与value操作】
Apr 14 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
phpinfo 系统查看参数函数代码
2009/06/05 PHP
Yii2使用$this-&gt;context获取当前的Module、Controller(控制器)、Action等
2017/03/29 PHP
PHP优化之批量操作MySQL实例分析
2020/04/23 PHP
php7 参数、整形及字符串处理机制修改实例分析
2020/05/25 PHP
javascript 导出数据到Excel(处理table中的元素)
2009/12/18 Javascript
iframe 父窗口和子窗口相互的调用方法集锦
2010/12/15 Javascript
js限制textarea每行输入字符串长度的代码
2012/10/31 Javascript
Extjs优化(二)Form表单提交通用实现
2013/04/15 Javascript
全面解析Bootstrap表单使用方法(表单控件状态)
2015/11/24 Javascript
非常实用的js验证框架实现源码 附原理方法
2016/06/08 Javascript
微信小程序与php 实现微信支付的简单实例
2017/06/23 Javascript
vue实现表格增删改查效果的实例代码
2017/07/18 Javascript
微信小程序图表插件wx-charts用法实例详解
2019/05/20 Javascript
JavaScript相等运算符的九条规则示例详解
2019/10/20 Javascript
基于Electron实现桌面应用开发代码实例
2020/07/07 Javascript
vue中使用echarts的示例
2021/01/03 Vue.js
[01:54]TI珍贵瞬间系列(三):翻盘
2020/08/28 DOTA
Python中的defaultdict与__missing__()使用介绍
2018/02/03 Python
使用Django启动命令行及执行脚本的方法
2018/05/29 Python
Python实现九宫格式的朋友圈功能内附“马云”朋友圈
2019/05/07 Python
Python任意字符串转16, 32, 64进制的方法
2019/06/12 Python
Django组件content-type使用方法详解
2019/07/19 Python
python并发编程 Process对象的其他属性方法join方法详解
2019/08/20 Python
解决Keras使用GPU资源耗尽的问题
2020/06/22 Python
python中的错误如何查看
2020/07/08 Python
详解python 内存优化
2020/08/17 Python
python用Configobj模块读取配置文件
2020/09/26 Python
Daisy London官网:英国最大的首饰集团IBB旗下
2019/02/28 全球购物
意大利领先的奢侈品在线时装零售商:MCLABELS
2020/10/13 全球购物
高二化学教学反思
2014/01/30 职场文书
2015公务员试用期工作总结
2014/12/12 职场文书
初三语文教学计划
2015/01/22 职场文书
2015年社区综治工作总结
2015/04/21 职场文书
音乐剧猫观后感
2015/06/04 职场文书
《哪吒之魔童降世》观后感:世上哪有随随便便的成功
2019/11/08 职场文书
CSS3 菱形拼图实现只旋转div 背景图片不旋转功能
2021/03/30 HTML / CSS