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 相关文章推荐
dedecms 制作模板中使用的全局标记图文教程
Mar 11 PHP
php生成xml简单实例代码
Dec 16 PHP
PHP 第二节 数据类型之字符串类型
Apr 28 PHP
深入理解require与require_once与include以及include_once的区别
Jun 05 PHP
不使用php api函数实现数组的交换排序示例
Apr 13 PHP
php使用指定编码导出mysql数据到csv文件的方法
Mar 31 PHP
PHP实现即时输出、实时输出内容方法
May 27 PHP
如何实现php图片等比例缩放
Jul 28 PHP
详解PHP的Yii框架中的Controller控制器
Mar 29 PHP
Yii调试查看执行SQL语句的方法
Jul 15 PHP
PHP入门教程之图像处理技巧分析
Sep 11 PHP
基于PHP实现短信验证码发送次数限制
Jul 11 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
substr()函数中文版
2006/10/09 PHP
Smarty安装配置方法
2008/04/10 PHP
PHP session会话的安全性分析
2011/09/08 PHP
php实现将数据做成json的格式给前端使用
2018/08/21 PHP
使用laravel和ECharts实现折线图效果的例子
2019/10/09 PHP
详解CSS样式中的 !important * _ 符号
2021/03/09 HTML / CSS
js 浮动层菜单收藏
2009/01/16 Javascript
学习ExtJS TextField常用方法
2009/10/07 Javascript
IE6,IE7,IE8下使用Javascript记录光标选中范围(已补全)
2011/08/28 Javascript
Prototype源码浅析 Number部分
2012/01/16 Javascript
JavaScript中的对象继承关系
2016/08/01 Javascript
Es6 Generator函数详细解析
2018/02/24 Javascript
浅谈mvvm-simple双向绑定简单实现
2018/04/18 Javascript
在pycharm中开发vue的方法步骤
2020/03/04 Javascript
[01:04:20]完美世界DOTA2联赛PWL S2 LBZS vs Forest 第一场 11.29
2020/12/02 DOTA
python解析xml文件操作实例
2014/10/05 Python
python简单文本处理的方法
2015/07/10 Python
Python中的time模块与datetime模块用法总结
2016/06/30 Python
python字典DICT类型合并详解
2017/08/17 Python
对Python3中的print函数以及与python2的对比分析
2018/05/02 Python
对numpy.append()里的axis的用法详解
2018/06/28 Python
PyTorch实现ResNet50、ResNet101和ResNet152示例
2020/01/14 Python
Python实现不规则图形填充的思路
2020/02/02 Python
Python 格式化输出_String Formatting_控制小数点位数的实例详解
2020/02/04 Python
python框架flask入门之路由及简单实现方法
2020/06/07 Python
Django基于Models定制Admin后台实现过程解析
2020/11/11 Python
洛杉矶健身中心女性专用运动服饰品牌:Marika
2018/05/09 全球购物
Nike瑞典官方网站:Nike.com (SE)
2018/11/26 全球购物
搬家公司的创业计划书
2014/01/01 职场文书
安全生产管理合理化建议书
2014/03/12 职场文书
环保建议书500字
2014/05/14 职场文书
纪念9.18事变演讲稿
2014/09/14 职场文书
2014县政府领导班子对照检查材料思想汇报
2014/09/25 职场文书
领导班子对照检查剖析材料
2014/10/13 职场文书
2016年师德学习心得体会
2016/01/12 职场文书
2019年让高校“心动”的自荐信
2019/03/25 职场文书