可定制的PHP缩略图生成程式(需要GD库支持)
2015-01-24信息快讯网
经典的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 裁剪图片成固定大小代码方法
PHP 获取MSN好友列表的代码(2009-05-14测试通过)
PHP 危险函数全解析
php 获取远程网页内容的函数
自动把纯文本转换成Web页面的php代码
谈PHP生成静态页面分析 模板+缓存+写文件
最新的php 文件上传模型,支持多文件上传
php 生成WML页面方法详解
PHP 生成的XML以FLASH获取为乱码终极解决
UTF8编码内的繁简转换的PHP类
PHP n个不重复的随机数生成代码
两个强悍的php 图像处理类1
一个很不错的PHP翻页类
简单的php 验证图片生成函数
php不用GD库生成当前时间的PNG格式图象的程序第1/2页
15种PHP Encoder的比较
如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量的
收集的php编写大型网站问题集
用PHP实现Ftp用户的在线管理的代码
个人站长制做网页常用的php代码
给php新手谈谈我的学习心得
excellent!――ASCII Art(由目标图象生成ascii)
修改了一个很不错的php验证码(支持中文)
Discuz!5的PHP代码高亮显示插件(黑暗中的舞者更新)
获得Google PR值的PHP代码
php生成文件
PHP生成静态页面详解
PHP读写文件的方法(生成HTML)
PHP生成静态页
不用GD库生成当前时间的PNG格式图象的程序