php类-resizeimage类-图片生成缩略图
2008-03-20 19:32
387 查看
<?
class resizeimage
{
//图片类型
var $type;
//实际宽度
var $width;
//实际高度
var $height;
//改变后的宽度
var $resize_width;
//改变后的高度
var $resize_height;
//是否裁图
var $cut;
//源图象
var $srcimg;
//目标图象地址[separator]
var $dstimg;
//临????建的图象
var $im;
function resizeimage($img, $wid, $hei,$c)
{
$this->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
//图片的类型
$this->type = substr(strrchr($this->srcimg,"."),1);
//初始化图象
$this->initi_img();
//目标图象地址
$this -> dst_img();
//--
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成图象
$this->newimg();
ImageDestroy ($this->im);
}
function newimg()
{
//改变后的图象的比例
$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);
}
}
}
//初始化图象
function initi_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);
}
}
//图象目标地址
function dst_img()
{
$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 = $name."_small.".$this->type;
}
}
$resizeimage = new resizeimage("test.jpg", "200", "200", "1");
?>
相关文章推荐
- 在ASP.NET中上传图片并生成缩略图
- c# 添加图片水印,可以指定水印位置+生成缩略图
- Asp.Net上传图片同时生成高清晰缩略图
- java 上传图片 生成缩略图
- C# 图片生成缩略图
- 利用ASPUPLOAD,ASPJPEG实现图片上传自动生成缩略图及加上水印
- android为视频、音乐、图片等生成缩略图
- Net 上传图片并生成高清晰缩略图(网摘)
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- 使用PHP 给网站 生成快照图片和缩略图
- .net mvc中上传图片生成缩略图
- 生成规定大小的图片(缩略图生成)
- ASP.NET 上传图片并生成缩略图
- 上传图片时生成缩略图,可以自定义图片尺寸
- PHP 生成图片缩略图
- Android 中为图片生成卷角+圆角缩略图的方法
- PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转
- phpcms v9图片生成缩略图变成黑色解决方法
- ASP.Net 上传图片并生成高清晰缩略图
- asp.net(c#)上传图片生成缩略图