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 相关文章推荐
层叠菜单的动态生成
Oct 09 PHP
PHP+DBM的同学录程序(5)
Oct 09 PHP
mysql 的 like 问题,超强毕杀记!!!
Jan 18 PHP
php网页后退不再出现过期
Mar 08 PHP
php之Memcache学习笔记
Jun 17 PHP
php实现源代码加密的方法
Jul 11 PHP
php开发工具有哪五款
Nov 09 PHP
PHP的Yii框架中使用数据库的配置和SQL操作实例教程
Mar 17 PHP
ThinkPHP3.2.1图片验证码实现方法
Aug 19 PHP
Yii2中datetime类的使用
Dec 17 PHP
PHP基于堆栈实现的高级计算器功能示例
Sep 15 PHP
PHP按一定比例压缩图片的方法
Oct 12 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
ThinkPHP3.1新特性之内容解析输出详解
2014/06/19 PHP
PHP中设置一个严格30分钟过期Session面试题的4种答案
2014/07/30 PHP
PHP中的日期时间处理利器实例(Carbon)
2017/06/09 PHP
High Performance JavaScript(高性能JavaScript)读书笔记分析
2011/05/05 Javascript
用Javascript评估用户输入密码的强度(Knockout版)
2011/11/30 Javascript
jquery 插件学习(二)
2012/08/06 Javascript
js实现数组去重、判断数组以及对象中的内容是否相同
2013/11/29 Javascript
jQuery异步验证用户名是否存在示例代码
2014/05/21 Javascript
js图片模糊切换显示特效的方法
2015/02/17 Javascript
详解Angular.js指令中scope类型的几种特殊情况
2017/02/21 Javascript
js弹出窗口简单实现代码
2017/03/22 Javascript
ES6新特性之Symbol类型用法分析
2017/03/31 Javascript
nodejs入门教程六:express模块用法示例
2017/04/24 NodeJs
Vue.js结合Ueditor富文本编辑器的实例代码
2017/07/11 Javascript
jQuery事件对象的属性和方法详解
2017/09/09 jQuery
JS实现小星星特效
2019/12/24 Javascript
vue radio单选框,获取当前项(每一项)的value值操作
2020/09/10 Javascript
[02:15]2014DOTA2国际邀请赛 赛后退役选手回顾
2014/08/01 DOTA
Python读取Excel表格,并同时画折线图和柱状图的方法
2018/10/14 Python
Python实现繁?转为简体的方法示例
2018/12/18 Python
python 根据字典的键值进行排序的方法
2019/07/24 Python
Django框架序列化与反序列化操作详解
2019/11/01 Python
Python实现进度条和时间预估的示例代码
2020/06/02 Python
canvas实现圆绘制的示例代码
2019/09/11 HTML / CSS
Boda Skins皮衣官网:奢侈皮夹克,全球配送
2016/12/15 全球购物
新西兰最大的天然保健及护肤品网站:HealthPost(直邮中国)
2021/02/13 全球购物
Linux文件操作命令都有哪些
2016/07/23 面试题
水利公司纪检监察自我鉴定
2014/02/25 职场文书
党员岗位承诺口号大全
2014/03/28 职场文书
企业家王石演讲稿:坚持与放下
2014/04/27 职场文书
病人慰问信范文
2015/02/15 职场文书
CSS中em的正确打开方式详解
2021/04/08 HTML / CSS
浅谈pytorch中的dropout的概率p
2021/05/27 Python
Javascript设计模式之原型模式详细
2021/10/05 Javascript
Python集合set()使用的方法详解
2022/03/18 Python
nginx共享内存的机制详解
2022/03/21 Servers