可定制的PHP缩略图生成程式(需要GD库支持)


Posted in PHP onMarch 06, 2007

经典的PHP缩略图生成程式,基于GD库,可指定生成路径及生成目标的宽高细节 使用方法: 在支持GD库的PHP环境中,将以下代码另存为resize.php测试 

经典的PHP缩略图生成程式,基于GD库,可指定生成路径及生成目标的宽高细节 

使用方法: 在支持GD库的PHP环境中,将以下代码另存为resize.php测试  

<?  $FILENAME="image_name";  
// 生成图片的宽度  
$RESIZEWIDTH=400;  
// 生成图片的高度  
$RESIZEHEIGHT=400;  
//生成图片的路径  
$uploaddir="c:/winnt/temp";  
function ResizeImage($im,$maxwidth,$maxheight,$name){  
global $uploaddir;  
$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,$uploaddir.$name . ".jpg");  
ImageDestroy ($newim);  
}else{  
ImageJpeg ($im,$uploaddir.$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);  
}  
}  
?>  
<img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>  
<form enctype="multipart/form-data" method="post">  
<br>  
<input type="file" name="image" size="50" value="浏览"><p>  
<input type="submit" value="上传图片">  
</form>   
</body>  
</html>
PHP 相关文章推荐
用PHP生成静态HTML速度快类库
Mar 18 PHP
php简单提示框alert封装函数
Aug 08 PHP
php Notice: Undefined index 错误提示解决方法
Aug 29 PHP
php使用ICQ网关发送手机短信
Oct 30 PHP
php实现图片缩放功能类
Dec 18 PHP
Laravel中使用自己编写类库的3种方法
Feb 10 PHP
php中 $$str 中 &quot;$$&quot; 的详解
Jul 06 PHP
如何使用微信公众平台开发模式实现多客服
Jan 06 PHP
Yii2框架dropDownList下拉菜单用法实例分析
Jul 18 PHP
PHP抽象类与接口的区别实例详解
May 09 PHP
laravel Model 执行事务的实现
Oct 10 PHP
搭建PhpStorm+PhpStudy开发环境的超详细教程
Sep 17 PHP
如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量的
Mar 06 #PHP
收集的php编写大型网站问题集
Mar 06 #PHP
用PHP实现Ftp用户的在线管理的代码
Mar 06 #PHP
个人站长制做网页常用的php代码
Mar 03 #PHP
NOD32 v2.70.32 简体中文封装版 提供下载了
Feb 27 #PHP
给php新手谈谈我的学习心得
Feb 25 #PHP
Smarty+QUICKFORM小小演示
Feb 25 #PHP
You might like
PHP实现域名whois查询的代码(数据源万网、新网)
2010/02/22 PHP
php中explode与split的区别介绍
2012/10/03 PHP
PHP人民币金额数字转中文大写的函数代码
2013/02/27 PHP
PHP autoload与spl_autoload自动加载机制的深入理解
2013/06/05 PHP
PHP5中实现多态的两种方法实例分享
2014/04/21 PHP
js+php实现静态页面实时调用用户登陆状态的方法
2015/01/04 PHP
php+ajax实现无刷新的新闻留言系统
2020/12/21 PHP
brook javascript框架介绍
2011/10/10 Javascript
Extjs 3.3切换tab隐藏相应工具栏出现空白解决
2013/04/02 Javascript
jquery的ajax和getJson跨域获取json数据的实现方法
2014/02/04 Javascript
JavaScript汉诺塔问题解决方法
2015/04/21 Javascript
简介JavaScript中strike()方法的使用
2015/06/08 Javascript
js实现当前输入框高亮显示的方法
2015/08/19 Javascript
利用vue-router实现二级菜单内容转换
2016/11/30 Javascript
Javascript 详解封装from表单数据为json串进行ajax提交
2017/03/29 Javascript
JS作用域链详解
2017/06/26 Javascript
详解创建自定义的Angular Schematics
2018/06/06 Javascript
深入理解Vue router的部分高级用法
2018/08/15 Javascript
微信小程序下拉刷新PullDownRefresh的使用方法
2018/11/29 Javascript
extract-text-webpack-plugin用法详解
2019/02/14 Javascript
js中对象与对象创建方法的各种方法
2019/02/27 Javascript
服务端预渲染之Nuxt(使用篇)
2019/04/08 Javascript
js通过循环多张图片实现动画效果
2019/12/19 Javascript
快速解决Vue、element-ui的resetFields()方法重置表单无效的问题
2020/08/12 Javascript
vue实现简易的双向数据绑定
2020/12/29 Vue.js
python条件和循环的使用方法
2013/11/01 Python
python文件和目录操作方法大全(含实例)
2014/03/12 Python
六个窍门助你提高Python运行效率
2015/06/09 Python
举例讲解Python中的Null模式与桥接模式编程
2016/02/02 Python
python实现定时自动备份文件到其他主机的实例代码
2018/02/23 Python
详解python实现小波变换的一个简单例子
2019/07/18 Python
Python: 传递列表副本方式
2019/12/19 Python
国外平面设计素材网站:The Hungry JPEG
2017/03/28 全球购物
工程师岗位职责
2013/11/08 职场文书
2014最新实习证明模板
2014/10/02 职场文书
django 认证类配置实现
2021/11/11 Python