PHP图片操作类
1,生成缩图
代码
<?php
///生成縮圖
///$resizeimage=new resizeimage($filesaved,$dstimg,250,250);
class resizeimage
{
var $type;
var $width;
var $height;
var $resize_width;
var $resize_height;
var $srcimg;
var $dstimg;
var $im;
var $desiredRatio;
function resizeimage($srcimg,$dstimg, $wid, $hei)
{
$this->srcimg = $srcimg;
$this->dstimg = $dstimg;
//圖片的類型
$this->type = substr(strrchr($this->srcimg,"."),1);
//初始化圖像
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);
//目標圖像寬和高
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//計算縮圖的寬和高
//若需要生產的縮圖的寬和高都大於源圖的寬高時都不需要縮小
if( ($this->width<=$wid) && ($this->height<=$hei) ){
$this->resize_width=$this->width;
$this->resize_height=$this->height;
}else{
//若寬的縮小比例小於高的縮小比例時,則成生的縮圖的寬為$wid
if( ($wid/$this->width) < ($hei/$this->height) ){
$this->desiredRatio=$wid/$this->width;
$this->resize_width=$wid;
$this->resize_height=(int)($this->height*$this->desiredRatio);
//若寬的縮小比例大於高的縮小比例時,則生成的縮圖的高為$hei
}else{
$this->desiredRatio=$hei/$this->height;
$this->resize_height=$hei;
$this->resize_width=(int)($this->width*$this->desiredRatio);
}
}
//生成圖像
$this->newimg();
//销毁一图像
ImageDestroy ($this->im);
}
function newimg()
{
$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->height) ;
ImageJpeg ($newimg,$this->dstimg);
}
}
?>
///生成縮圖
///$resizeimage=new resizeimage($filesaved,$dstimg,250,250);
class resizeimage
{
var $type;
var $width;
var $height;
var $resize_width;
var $resize_height;
var $srcimg;
var $dstimg;
var $im;
var $desiredRatio;
function resizeimage($srcimg,$dstimg, $wid, $hei)
{
$this->srcimg = $srcimg;
$this->dstimg = $dstimg;
//圖片的類型
$this->type = substr(strrchr($this->srcimg,"."),1);
//初始化圖像
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);
//目標圖像寬和高
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//計算縮圖的寬和高
//若需要生產的縮圖的寬和高都大於源圖的寬高時都不需要縮小
if( ($this->width<=$wid) && ($this->height<=$hei) ){
$this->resize_width=$this->width;
$this->resize_height=$this->height;
}else{
//若寬的縮小比例小於高的縮小比例時,則成生的縮圖的寬為$wid
if( ($wid/$this->width) < ($hei/$this->height) ){
$this->desiredRatio=$wid/$this->width;
$this->resize_width=$wid;
$this->resize_height=(int)($this->height*$this->desiredRatio);
//若寬的縮小比例大於高的縮小比例時,則生成的縮圖的高為$hei
}else{
$this->desiredRatio=$hei/$this->height;
$this->resize_height=$hei;
$this->resize_width=(int)($this->width*$this->desiredRatio);
}
}
//生成圖像
$this->newimg();
//销毁一图像
ImageDestroy ($this->im);
}
function newimg()
{
$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->height) ;
ImageJpeg ($newimg,$this->dstimg);
}
}
?>
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。