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脚本的10个技巧(2)
Oct 09 PHP
Linux下 php5 MySQL5 Apache2 phpMyAdmin ZendOptimizer安装与配置[图文]
Nov 18 PHP
php disk_free_space 返回目录可用空间
May 10 PHP
PHP __autoload()方法真的影响性能吗?
Mar 30 PHP
基于MySQL体系结构的分析
May 02 PHP
使用php+swoole对client数据实时更新(一)
Jan 07 PHP
PHP基于单例模式实现的mysql类
Jan 09 PHP
PHP MySql增删改查的简单实例
Jun 21 PHP
php获取目录中所有文件名及判断文件与目录的简单方法
Mar 04 PHP
PHP convert_uudecode()函数讲解
Feb 14 PHP
Laravel 实现数据软删除功能
Aug 21 PHP
浅谈laravel-admin form中的数据,在提交后,保存前,获取并进行编辑
Oct 21 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 Socket配置以及实例的详细介绍
2013/06/13 PHP
php用正则表达式匹配URL的简单方法
2013/11/12 PHP
PHP性能分析工具XHProf安装使用教程
2015/05/13 PHP
分享ThinkPHP3.2中关联查询解决思路
2015/09/20 PHP
php 下 html5 XHR2 + FormData + File API 上传文件操作实例分析
2020/02/28 PHP
分享别人写的一个小型js框架
2007/08/13 Javascript
ExtJs之带图片的下拉列表框插件
2010/03/04 Javascript
jQuery中$.click()无效问题分析
2015/01/29 Javascript
js与jQuery实现checkbox复选框全选/全不选的方法
2016/01/05 Javascript
在JavaScript中模拟类(class)及类的继承关系
2016/05/20 Javascript
JavaScript实现点击文本自动定位到下拉框选中操作
2016/06/15 Javascript
JavaScript 深层克隆对象详解及实例
2016/11/03 Javascript
web打印小结
2017/01/11 Javascript
JavaScript中的toString()和toLocaleString()方法的区别
2017/02/15 Javascript
jQuery插件FusionCharts绘制2D双折线图效果示例【附demo源码】
2017/04/14 jQuery
自定义类似于jQuery UI Selectable 的Vue指令v-selectable
2017/08/23 jQuery
通过vue-cli3构建一个SSR应用程序的方法
2018/09/13 Javascript
利用JavaScript将Excel转换为JSON示例代码
2019/06/14 Javascript
使用 Jest 和 Supertest 进行接口端点测试实例详解
2020/04/25 Javascript
解决echarts 一条柱状图显示两个值,类似进度条的问题
2020/07/20 Javascript
python k-近邻算法实例分享
2014/06/11 Python
Django 路由系统URLconf的使用
2018/10/11 Python
使用Python快速制作可视化报表的方法
2019/02/03 Python
详解Python基础random模块随机数的生成
2019/03/23 Python
基于Python新建用户并产生随机密码过程解析
2019/10/08 Python
python 串口读取+存储+输出处理实例
2019/12/26 Python
Python读写操作csv和excle文件代码实例
2020/03/16 Python
亚历山大·王官网:Alexander Wang
2017/06/23 全球购物
毕业生找工作的自我评价
2013/10/18 职场文书
人事专员岗位职责
2013/11/20 职场文书
一年级学生期末评语
2014/04/21 职场文书
高二学生评语大全
2014/04/25 职场文书
教师先进工作者事迹材料
2014/05/01 职场文书
工作推荐信模板
2015/03/25 职场文书
运动会闭幕式主持词
2015/07/01 职场文书
pandas中关于apply+lambda的应用
2022/02/28 Python