PHP生成图片缩略图
2015-12-23 10:46
537 查看
原文地址:PHP生成图片缩略图作者:邓强要使用PHP生成图片缩略图,要保证你的PHP服务器安装了GD2图形库[/b]。
使用一个类生成图片的缩略图,类的源码达内php培训老师为大家分享到下面。
调用此类的方法:
$resizeimage=newresizeimage("图片源文件地址","200","100","0","缩略图地址");
//就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高
//使用如下类就可以生成图片缩略图,
[php代码]
classresizeimage
{
//图片类型
var$type;
//实际宽度
var$width;
//实际高度
var$height;
//改变后的宽度
var$resize_width;
//改变后的高度
var$resize_height;
//是否裁图
var$cut;
//源图象
var$srcimg;
//目标图象地址
var$dstimg;
//临时创建的图象
var$im;
functionresizeimage($img,$wid,$hei,$c,$dstpath)
{
$this->srcimg=$img;
$this->resize_width=$wid;
$this->resize_height=$hei;
$this->cut=$c;
//图片的类型
$this->type=strtolower(substr(strrchr($this->srcimg,"."),1));
//初始化图象
$this->initi_img();
//目标图象地址
$this->dst_img($dstpath);
//--
$this->width=imagesx($this->im);
$this->height=imagesy($this->im);
//生成图象
$this->newimg();
ImageDestroy($this->im);
}
functionnewimg()
{
//改变后的图象的比例
$resize_ratio=($this->resize_width)/($this->resize_height);
//实际图象的比例
$ratio=($this->width)/($this->height);
if(($this->cut)=="1")
//裁图
{
if($ratio>=$resize_ratio)
//高度优先
{
$newimg=imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,(($this->height)*$resize_ratio),$this->height);
ImageJpeg($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
//宽度优先
{
$newimg=imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,$this->width,(($this->width)/$resize_ratio));
ImageJpeg($newimg,$this->dstimg);
}
}
else
//不裁图
{
if($ratio>=$resize_ratio)
{
$newimg=imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,($this->resize_width)/$ratio,$this->width,$this->height);
ImageJpeg($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
{
$newimg=imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
imagecopyresampled($newimg,$this->im,0,0,0,0,($this->resize_height)*$ratio,$this->resize_height,$this->width,$this->height);
ImageJpeg($newimg,$this->dstimg);
}
}
}
//初始化图象
functioniniti_img()
{
if($this->type=="jpg")
{
$this->im=imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif")
{
$this->im=imagecreatefromgif($this->srcimg);
}
if($this->type=="png")
{
$this->im=imagecreatefrompng($this->srcimg);
}
}
//图象目标地址
functiondst_img($dstpath)
{
$full_length =strlen($this->srcimg);
$type_length =strlen($this->type);
$name_length =$full_length-$type_length;
$name =substr($this->srcimg,0,$name_length-1);
$this->dstimg=$dstpath;
//echo$this->dstimg;
}
}
使用一个类生成图片的缩略图,类的源码达内php培训老师为大家分享到下面。
调用此类的方法:
$resizeimage=newresizeimage("图片源文件地址","200","100","0","缩略图地址");
//就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高
//使用如下类就可以生成图片缩略图,
[php代码]
classresizeimage
{
//图片类型
var$type;
//实际宽度
var$width;
//实际高度
var$height;
//改变后的宽度
var$resize_width;
//改变后的高度
var$resize_height;
//是否裁图
var$cut;
//源图象
var$srcimg;
//目标图象地址
var$dstimg;
//临时创建的图象
var$im;
functionresizeimage($img,$wid,$hei,$c,$dstpath)
{
$this->srcimg=$img;
$this->resize_width=$wid;
$this->resize_height=$hei;
$this->cut=$c;
//图片的类型
$this->type=strtolower(substr(strrchr($this->srcimg,"."),1));
//初始化图象
$this->initi_img();
//目标图象地址
$this->dst_img($dstpath);
//--
$this->width=imagesx($this->im);
$this->height=imagesy($this->im);
//生成图象
$this->newimg();
ImageDestroy($this->im);
}
functionnewimg()
{
//改变后的图象的比例
$resize_ratio=($this->resize_width)/($this->resize_height);
//实际图象的比例
$ratio=($this->width)/($this->height);
if(($this->cut)=="1")
//裁图
{
if($ratio>=$resize_ratio)
//高度优先
{
$newimg=imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,(($this->height)*$resize_ratio),$this->height);
ImageJpeg($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
//宽度优先
{
$newimg=imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,$this->width,(($this->width)/$resize_ratio));
ImageJpeg($newimg,$this->dstimg);
}
}
else
//不裁图
{
if($ratio>=$resize_ratio)
{
$newimg=imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,($this->resize_width)/$ratio,$this->width,$this->height);
ImageJpeg($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
{
$newimg=imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
imagecopyresampled($newimg,$this->im,0,0,0,0,($this->resize_height)*$ratio,$this->resize_height,$this->width,$this->height);
ImageJpeg($newimg,$this->dstimg);
}
}
}
//初始化图象
functioniniti_img()
{
if($this->type=="jpg")
{
$this->im=imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif")
{
$this->im=imagecreatefromgif($this->srcimg);
}
if($this->type=="png")
{
$this->im=imagecreatefrompng($this->srcimg);
}
}
//图象目标地址
functiondst_img($dstpath)
{
$full_length =strlen($this->srcimg);
$type_length =strlen($this->type);
$name_length =$full_length-$type_length;
$name =substr($this->srcimg,0,$name_length-1);
$this->dstimg=$dstpath;
//echo$this->dstimg;
}
}
相关文章推荐
- PHP框架(codeigniter):数据…
- PHP框架(codeigniter):数据…
- php简单笔记
- Yii中创建Model时出现的错误…
- Windows10搭建PHP7开发环境
- php中file_get_contents与curl性能比较分析
- Yii2-GridView
- ububtu server 14.04上,PHP5-FPM服务的启动方式变更
- 开源PHP框架Kohana的介绍
- 关于phpcms内容修改中文不显示问题
- php 5.6 安装opcache 支持
- Yii2的深入学习--事件Event
- php 输出 csv 单元格内换行
- PHP多维数组转一维数组的简单实现方法
- PHP+MySQL实现无极限分类栏目的方法
- php获得客户端浏览器名称及版本的方法(基于ECShop函数)
- 分享PHP计算两个日期相差天数的代码
- 10个必须掌握的PHP关联数组使用技巧
- yii2 pjax使用
- PHP闭包(Closure)初探