PHP用GD库生成高质量的缩略图片


Posted in PHP onMarch 09, 2011

以下是PHP源代码(ResizeImage.php)。

<?php 
$FILENAME="image.thumb"; 
// 生成图片的宽度 
$RESIZEWIDTH=400; 
// 生成图片的高度 
$RESIZEHEIGHT=400; function ResizeImage($im,$maxwidth,$maxheight,$name){ 
$width = imagesx($im); 
$height = imagesy($im); 
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){ 
if($maxwidth && $width > $maxwidth){ 
$widthratio = $maxwidth/$width; 
$RESIZEWIDTH=true; 
} 
if($maxheight && $height > $maxheight){ 
$heightratio = $maxheight/$height; 
$RESIZEHEIGHT=true; 
} 
if($RESIZEWIDTH && $RESIZEHEIGHT){ 
if($widthratio < $heightratio){ 
$ratio = $widthratio; 
}else{ 
$ratio = $heightratio; 
} 
}elseif($RESIZEWIDTH){ 
$ratio = $widthratio; 
}elseif($RESIZEHEIGHT){ 
$ratio = $heightratio; 
} 
$newwidth = $width * $ratio; 
$newheight = $height * $ratio; 
if(function_exists("imagecopyresampled")){ 
$newim = imagecreatetruecolor($newwidth, $newheight); 
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
}else{ 
$newim = imagecreate($newwidth, $newheight); 
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
} 
ImageJpeg ($newim,$name . ".jpg"); 
ImageDestroy ($newim); 
}else{ 
ImageJpeg ($im,$name . ".jpg"); 
} 
} 
if($_FILES['image']['size']){ 
if($_FILES['image']['type'] == "image/pjpeg"){ 
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']); 
}elseif($_FILES['image']['type'] == "image/x-png"){ 
$im = imagecreatefrompng($_FILES['image']['tmp_name']); 
}elseif($_FILES['image']['type'] == "image/gif"){ 
$im = imagecreatefromgif($_FILES['image']['tmp_name']); 
} 
if($im){ 
if(file_exists("$FILENAME.jpg")){ 
unlink("$FILENAME.jpg"); 
} 
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME); 
ImageDestroy ($im); 
} 
} 
?>

以下是测试代码(demo.php)
<?php 
include('ResizeImage.php'); 
if(!empty($_POST)){ 
echo($FILENAME.".jpg?cache=".rand(0,999999)); 
} 
?> 
<form name="test" action="?submit=true" enctype="multipart/form-data" method="post" > 
<input type="file" name="image" size="50" value="浏览"><p> 
<input type="submit" value="上传图片"> 
</form>
PHP 相关文章推荐
PHP伪静态页面函数附使用方法
Jun 20 PHP
PHP+ACCESS 文章管理程序代码
Jun 21 PHP
用PHP读取和编写XML DOM的实现代码
Feb 03 PHP
PHP的error_reporting错误级别变量对照表
Jul 08 PHP
php中把美国时间转为北京时间的自定义函数分享
Jul 28 PHP
php中session定期自动清理的方法
Nov 12 PHP
PHP 用session与gd库实现简单验证码生成与验证的类方法
Nov 15 PHP
微信公众平台开发-微信服务器IP接口实例(含源码)
Mar 05 PHP
YII框架批量插入数据的方法
Mar 18 PHP
如何修改yii2.0自带的user表为其它的表
Aug 01 PHP
Yii框架日志操作图文与实例详解
Sep 09 PHP
PHP 对接美团大众点评团购券(门票)的开发步骤
Apr 03 PHP
php GeoIP的使用教程
Mar 09 #PHP
让PHP COOKIE立即生效,不用刷新就可以使用
Mar 09 #PHP
通过JavaScript或PHP检测Android设备的代码
Mar 09 #PHP
PHP中uploaded_files函数使用方法详解
Mar 09 #PHP
PHP $_FILES函数详解
Mar 09 #PHP
PHP中for与foreach的区别分析
Mar 09 #PHP
php模板中出现空行解决方法
Mar 08 #PHP
You might like
使用php+swoole对client数据实时更新(一)
2016/01/07 PHP
学习thinkphp5.0验证类使用方法
2017/11/16 PHP
PHP实现数据库的增删查改功能及完整代码
2018/04/18 PHP
php swoft框架实例用法
2020/12/22 PHP
JQuery模板插件 jquery.tmpl 动态ajax扩展
2011/11/10 Javascript
jquery $(&quot;#variable&quot;) 循环改变variable的值示例
2014/02/23 Javascript
JS+CSS实现仿新浪微博搜索框的方法
2015/02/24 Javascript
jQuery+jRange实现滑动选取数值范围特效
2015/03/14 Javascript
js实现随屏幕滚动的带缓冲效果的右下角广告代码
2015/09/04 Javascript
JS 日期与时间戮相互转化的简单实例
2016/06/22 Javascript
JS中split()用法(将字符串按指定符号分割成数组)
2016/10/24 Javascript
vue-router 中router-view不能渲染的解决方法
2017/05/23 Javascript
JS倒计时实例_天时分秒
2017/08/22 Javascript
jQuery中extend函数简单用法示例
2017/10/11 jQuery
微信小程序实现跳转的几种方式总结(推荐)
2019/04/24 Javascript
提升Python程序运行效率的6个方法
2015/03/31 Python
理解Python中的With语句
2016/03/18 Python
python用opencv批量截取图像指定区域的方法
2019/01/24 Python
Python使用os.listdir和os.walk获取文件路径
2020/05/21 Python
Python如何实现线程间通信
2020/07/30 Python
Python连接mysql数据库及简单增删改查操作示例代码
2020/08/03 Python
Python创建文件夹与文件的快捷方法
2020/12/08 Python
HTML5+WebSocket实现多文件同时上传的实例
2016/12/29 HTML / CSS
Notino芬兰:购买香水和化妆品
2019/04/15 全球购物
软件测试工程师结构化面试题库
2016/11/23 面试题
采购内勤岗位职责
2013/12/10 职场文书
爱情保证书范文
2014/02/01 职场文书
优秀党员获奖感言
2014/02/18 职场文书
企业安全生产月活动总结
2014/07/05 职场文书
高校自主招生校长推荐信
2015/03/23 职场文书
村党组织公开承诺书
2015/04/30 职场文书
教师调动申请报告
2015/05/18 职场文书
本科毕业答辩开场白
2015/05/27 职场文书
初中班长竞选稿
2015/11/20 职场文书
学习党章心得体会2016
2016/01/15 职场文书
解决MySQL报“too many connections“错误
2022/04/19 MySQL