图片处理类,用来生成缩略图
来源:互联网 发布:vscode eslint 编辑:程序博客网 时间:2024/05/16 05:08
/*
* Created on 20:31 2011-8-2
* Author : LKK , http://lianq.net
* 使用方法:
*$resizeimage = new myThumbClass($file_name,120,90,$thumb,0,0); //生成120x90大小
*$resizeimage = new myThumbClass($file_name,1,2,$thumb,1,233); //生成高,宽之中最大233
*$resizeimage = new myThumbClass($file_name,660,1,$thumb,-1,0); //生成宽度660
*$resizeimage = new myThumbClass($file_name,1,660,$thumb,-1,0); //生成高度660
*注意:新高度或新宽度都不能为0
*/
class myThumbClass{
public $sur_file; //读取的原图片
public $des_file; //生成目标图片
public $tem_file; //临时图片
public $tag; //缩略标签 0,默认,按固定的高宽生成 1,按比列或固定最大长度生成 -1,按某个宽度或某个高度缩小
public $resize_width; //$tag为0时,目标文件宽
public $resize_height; //$tag为0时,目标文件高
public $sca_max; //$tag为1时,<0$sca_max<1时为缩小比例;$sca_max>1时为最大长度(高或宽之中的最大值)
public $type; //图片类型
public $width; //原图片宽
public $height; //原图片高
//构造函数
public function __construct($surpic, $reswid, $reshei, $despic, $mark, $scamax){
$this->sur_file = $surpic;
$this->resize_width = $reswid;
$this->resize_height = $reshei;
$this->tag = $mark;
$this->sca_max = $scamax;
$this->type = strtolower(substr(strrchr($this->sur_file,"."),1)); //获取图片类型
$this->init_img(); //初始化图片
$this->des_file = $despic; //目标图片地址
$this->width = imagesx($this->tem_file);
$this->height = imagesy($this->tem_file);
$this->new_img();
imagedestroy($this->tem_file);
}
//图片初始化函数
private function init_img(){
if($this->type == 'jpeg'){
$this->tem_file = imagecreatefromjpeg($this->sur_file);
}elseif($this->type == 'jpg'){
$this->tem_file = imagecreatefromjpeg($this->sur_file);
}elseif($this->type == 'gif'){
$this->tem_file = imagecreatefromgif($this->sur_file);
}elseif($this->type == 'png'){
$this->tem_file = imagecreatefrompng($this->sur_file);
}elseif($this->type == 'bmp'){
$this->tem_file = imagecreatefrombmp($this->sur_file); //bmp.php中包含
}
}
//图片生成函数
private function new_img(){
$ratio = ($this->width)/($this->height); //原图比例
$resize_ratio = ($this->resize_width)/($this->resize_height); //缩略后比例
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);//生成新图片
if($this->tag == 0){ //按固定高宽截取缩略图
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);//生成新图片
if($ratio>=$resize_ratio){//即等比例下,缩略图的高比原图长,因此高不变
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
}elseif($ratio<$resize_ratio){//即等比例下,缩略图的宽比原图长,因此宽不变
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width,$this->resize_height, $this->width, (($this->width)/$resize_ratio));
}
}elseif($this->tag == 1){ //按固定比例或最大长度缩小
if($this->sca_max < 1){ //按比例缩小
$newimg = imagecreatetruecolor((($this->width)*($this->sca_max)),(($this->height)*($this->sca_max)));//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, (($this->width)*($this->sca_max)), (($this->height)*($this->sca_max)), $this->width, $this->height);
}elseif($this->sca_max > 1){ //按某个最大长度缩小
if($ratio>=1){ //宽比高长
$newimg = imagecreatetruecolor($this->sca_max,($this->sca_max/$ratio));//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->sca_max,($this->sca_max/$ratio), $this->width, $this->height);
}else{
$newimg = imagecreatetruecolor(($this->sca_max*$ratio),$this->sca_max);//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->sca_max*$ratio),$this->sca_max, $this->width, $this->height);
}
}
}elseif($this->tag == -1){ //按某个宽度或某个高度缩小
if($resize_ratio>=1){//新高小于新宽,则图片按新宽度缩小
$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width/$ratio));//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width,($this->resize_width/$ratio), $this->width, $this->height);
}elseif($resize_ratio<1){//新宽小于新高,则图片按新高度缩小
$newimg = imagecreatetruecolor(($this->resize_height*$ratio),$this->resize_height);//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->resize_height*$ratio),$this->resize_height, $this->width, $this->height);
}
}
//输出新图片
if($this->type == 'jpeg' || $this->type == 'jpg'){
imagejpeg($newimg,$this->des_file);
}elseif($this->type == 'gif'){
imagegif($newimg,$this->des_file);
}elseif($this->type == 'png'){
imagepng($newimg,$this->des_file);
}elseif($this->type == 'bmp'){
imagebmp($newimg,$this->des_file);//bmp.php中包含
}
}
}
/**
* 生成缩略图
* @param 输入图片路径 $img_path
* @param 输出缩略图路径 $tumb_path
* @param 缩略图宽度 $width
* @param 缩略图高度 $height
*/
function generate_thumb($img_path,&$tumb_path,$width = 201,$height = 135)
{
$baseName = ''; //文件名(不带扩展名)
$extName = ''; //文件扩展名
$saveDir = ''; //保存目录
$pt1=strrpos($img_path, "/");
$pt2=strrpos($img_path, ".");
$baseName = substr($img_path, $pt1+1,$pt2-$pt1-1);
$saveDir = substr($img_path, 0,$pt1+1);
$extend = pathinfo($img_path);
$extend = $extend["extension"];
$tumb_path = $saveDir.$baseName.'_thumb.'.$extend;
$resizeimage = new myThumbClass($img_path,$width,$height,$tumb_path,0,0);
}
* Created on 20:31 2011-8-2
* Author : LKK , http://lianq.net
* 使用方法:
*$resizeimage = new myThumbClass($file_name,120,90,$thumb,0,0); //生成120x90大小
*$resizeimage = new myThumbClass($file_name,1,2,$thumb,1,233); //生成高,宽之中最大233
*$resizeimage = new myThumbClass($file_name,660,1,$thumb,-1,0); //生成宽度660
*$resizeimage = new myThumbClass($file_name,1,660,$thumb,-1,0); //生成高度660
*注意:新高度或新宽度都不能为0
*/
class myThumbClass{
public $sur_file; //读取的原图片
public $des_file; //生成目标图片
public $tem_file; //临时图片
public $tag; //缩略标签 0,默认,按固定的高宽生成 1,按比列或固定最大长度生成 -1,按某个宽度或某个高度缩小
public $resize_width; //$tag为0时,目标文件宽
public $resize_height; //$tag为0时,目标文件高
public $sca_max; //$tag为1时,<0$sca_max<1时为缩小比例;$sca_max>1时为最大长度(高或宽之中的最大值)
public $type; //图片类型
public $width; //原图片宽
public $height; //原图片高
//构造函数
public function __construct($surpic, $reswid, $reshei, $despic, $mark, $scamax){
$this->sur_file = $surpic;
$this->resize_width = $reswid;
$this->resize_height = $reshei;
$this->tag = $mark;
$this->sca_max = $scamax;
$this->type = strtolower(substr(strrchr($this->sur_file,"."),1)); //获取图片类型
$this->init_img(); //初始化图片
$this->des_file = $despic; //目标图片地址
$this->width = imagesx($this->tem_file);
$this->height = imagesy($this->tem_file);
$this->new_img();
imagedestroy($this->tem_file);
}
//图片初始化函数
private function init_img(){
if($this->type == 'jpeg'){
$this->tem_file = imagecreatefromjpeg($this->sur_file);
}elseif($this->type == 'jpg'){
$this->tem_file = imagecreatefromjpeg($this->sur_file);
}elseif($this->type == 'gif'){
$this->tem_file = imagecreatefromgif($this->sur_file);
}elseif($this->type == 'png'){
$this->tem_file = imagecreatefrompng($this->sur_file);
}elseif($this->type == 'bmp'){
$this->tem_file = imagecreatefrombmp($this->sur_file); //bmp.php中包含
}
}
//图片生成函数
private function new_img(){
$ratio = ($this->width)/($this->height); //原图比例
$resize_ratio = ($this->resize_width)/($this->resize_height); //缩略后比例
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);//生成新图片
if($this->tag == 0){ //按固定高宽截取缩略图
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);//生成新图片
if($ratio>=$resize_ratio){//即等比例下,缩略图的高比原图长,因此高不变
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
}elseif($ratio<$resize_ratio){//即等比例下,缩略图的宽比原图长,因此宽不变
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width,$this->resize_height, $this->width, (($this->width)/$resize_ratio));
}
}elseif($this->tag == 1){ //按固定比例或最大长度缩小
if($this->sca_max < 1){ //按比例缩小
$newimg = imagecreatetruecolor((($this->width)*($this->sca_max)),(($this->height)*($this->sca_max)));//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, (($this->width)*($this->sca_max)), (($this->height)*($this->sca_max)), $this->width, $this->height);
}elseif($this->sca_max > 1){ //按某个最大长度缩小
if($ratio>=1){ //宽比高长
$newimg = imagecreatetruecolor($this->sca_max,($this->sca_max/$ratio));//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->sca_max,($this->sca_max/$ratio), $this->width, $this->height);
}else{
$newimg = imagecreatetruecolor(($this->sca_max*$ratio),$this->sca_max);//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->sca_max*$ratio),$this->sca_max, $this->width, $this->height);
}
}
}elseif($this->tag == -1){ //按某个宽度或某个高度缩小
if($resize_ratio>=1){//新高小于新宽,则图片按新宽度缩小
$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width/$ratio));//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width,($this->resize_width/$ratio), $this->width, $this->height);
}elseif($resize_ratio<1){//新宽小于新高,则图片按新高度缩小
$newimg = imagecreatetruecolor(($this->resize_height*$ratio),$this->resize_height);//生成新图片
imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->resize_height*$ratio),$this->resize_height, $this->width, $this->height);
}
}
//输出新图片
if($this->type == 'jpeg' || $this->type == 'jpg'){
imagejpeg($newimg,$this->des_file);
}elseif($this->type == 'gif'){
imagegif($newimg,$this->des_file);
}elseif($this->type == 'png'){
imagepng($newimg,$this->des_file);
}elseif($this->type == 'bmp'){
imagebmp($newimg,$this->des_file);//bmp.php中包含
}
}
}
/**
* 生成缩略图
* @param 输入图片路径 $img_path
* @param 输出缩略图路径 $tumb_path
* @param 缩略图宽度 $width
* @param 缩略图高度 $height
*/
function generate_thumb($img_path,&$tumb_path,$width = 201,$height = 135)
{
$baseName = ''; //文件名(不带扩展名)
$extName = ''; //文件扩展名
$saveDir = ''; //保存目录
$pt1=strrpos($img_path, "/");
$pt2=strrpos($img_path, ".");
$baseName = substr($img_path, $pt1+1,$pt2-$pt1-1);
$saveDir = substr($img_path, 0,$pt1+1);
$extend = pathinfo($img_path);
$extend = $extend["extension"];
$tumb_path = $saveDir.$baseName.'_thumb.'.$extend;
$resizeimage = new myThumbClass($img_path,$width,$height,$tumb_path,0,0);
}
0 0
- 图片处理类,用来生成缩略图
- ASP 图片处理工具类,生成缩略图
- 图片生成缩略图类
- 实用开发一:c#封装好的处理图片的水印和生成缩略图的类
- C#的图片处理类,生成高质量缩略图,代码已测试过,效果很好
- php类-resizeimage类-图片生成缩略图
- 简单图片文件上传并处理缩略图生成
- C#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- 常用类库-01图片:生成缩略图
- 生成缩略图的PHP上传图片类
- 生成图片缩略图
- 生成图片缩略图
- 一种高效简单的缓存机制
- matlab c++ 画图【转载】
- 域名年龄-SEO搜索引擎优化
- 【Cocos2d-x】 关于IOS设置竖屏后,运行会报错的解决方法
- android碎片Fragment简介详解
- 图片处理类,用来生成缩略图
- 黑马程序员---学生管理系统 (转载)
- iOS-UITextField类
- Lua中的IO库
- (转)linux口令相关文件(/etc/passwd和/etc/shadow)
- 游戏开发中的数学和物理算法(7):角度 vs 弧度
- Bitmap二次采样
- 系统调用与API的区别
- DIV+CSS命名规范