[PHP5]T.T.R 缩略图/截图 生成类!
时间:2007-09-24
来源:互联网
准备是继承上传类,实现一个子类,然后实例化缩略图类,
在开发中明显感觉到PHP OOP的不足,无多继承,无重载
希望能在PHP6中得到解决。
PHP代码:
<?php
/**
*TTR缩略图\截图类
*2007-09-23
*[url]http://www.Gx3.cn[/url]
*QQ:252319874
*类实例:$a = ResizeImage::getInstance();
*创建方法:$a->create(string imagename,int resizewidth,int resizeheight,[boolen cutmode],[int cutX],[int cutY])
*可选参数说明:
*cutmode,默认false:创建缩略图,true:剪裁图片
*cutX,cutY:要截取原图片的,xy坐标以左上角为原点
**/
class ResizeImage
{
private $type=null;
private $width=0;
private $height=0;
private $resize_width=0;
private $resize_height=0;
private $cutX=0;
private $cutY=0;
private $cutmode=true;
private $imagepath=null;
private $savepath=null;
private $uploadpath="uploadfile/";
private $im;
static $instance=null;
private $resizeimge=null;
private function __construct()
{
}
public function create($imagepath,$resizewidth,$resizeheight,$cutmode=false,$x=0,$y=0)
{
$this->imagepath=$imagepath;
$this->resize_width=$resizewidth;
$this->resize_height=$resizeheight;
$this->cutmode=$cutmode;
$this->cutX=$x;
$this->cutY=$y;
$this->type=$this->getImageType();
$this->initImage();
$this->getImageDstPath();
$this->width=$this->getImageX();
$this->height=$this->getImageY();
$this->createResizeImage();
}
public static function getInstance()
{
if(self::$instance==null)
{
self::$instance = new ResizeImage();
}
return self::$instance;
}
private function initImage()
{
switch($this->type)
{
case "jpg":
$this->im=imagecreatefromjpeg($this->imagepath);
break;
case "gif":
$this->im=imagecreatefromgif($this->imagepath);
break;
case "png":
$this->im=imagecreatefrompng($this->imagepath);
break;
}
}
private function getNewImageName()
{
$arr=explode("/",$this->imagepath);
if(count($arr)==0)
{
$arr=explode(".",$this->imagepath);
}else{
$arr=explode(".",$arr[count($arr)-1]);
}
return $arr[0]."_s.jpg";
}
private function createResizeImage()
{
$resizerito=($this->resize_width/$this->resize_height);
$rito=($this->width/$this->height);
if($this->cutmode)
{
$reim=imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($reim,$this->im,0,0,$this->cutX,$this->cutY,$this->width,$this->height,$this->width,$this->height);
imagejpeg($reim,$this->savepath.$this->getNewImageName());
}else{
if($this->rito >= $this->resizerito)
{
$reim=imagecreatetruecolor($this->resize_width,$this->resize_width/$rito);
imagecopyresampled($reim,$this->im,0,0,0,0,$this->resize_width,($this->resize_width/$rito),$this->width,$this->height);
imagejpeg($reim,$this->savepath.$this->getNewImageName());
}else{
$reim=imagecreatetruecolor($this->resize_height*$rito,$this->resize_height);
imagecopyresampled($reim,$this->im,0,0,0,0,($this->resize_height*$rito),$this->resize_height,$this->width,$this->height);
imagejpeg($reim,$this->savepath.$this->getNewImageName());
}
}
}
private function showErrorMessage($str)
{
echo "<script>alert('$str')</script>";
}
private function getImageX()
{
return imagesx($this->im);
}
private function getImageY()
{
return imagesy($this->im);
}
private function getImageType()
{
return strtolower(substr(strrchr($this->imagepath,"."),1));
}
private function getImageDstPath()
{
if(substr($this->uploadpath,-1,1)!="/")
{
$this->savepath=$this->uploadpath."/".date("Ym");
}else{
$this->savepath=$this->uploadpath.date("Ym");
}
$this->savepath=$this->getFolder($this->savepath);
}
private function getFolder($folder)
{
if(!is_dir($folder))
{
mkdir($folder);
}
return $folder."/";
}
public function __destruct()
{
$this->im=null;
$this->width=0;
$this->height=0;
$this->imagepath=null;
$this->savepath=null;
$this->resize_width=0;
$this->resize_height0;
}
}
?>
作者: T.T.R 发布时间: 2007-09-24
作者: niohe 发布时间: 2007-09-24
作者: sanler 发布时间: 2007-09-25
作者: cator 发布时间: 2007-09-25
佩服
作者: sanler 发布时间: 2007-11-12
作者: adleyliu 发布时间: 2007-11-16
重载的确是不支持,这个有待改进,不过基本上影响也不是太大,没有也讲究了...有了更好= =
据说5.3 将会支持namespace,这倒也算是个不小的进步了
[ 本帖最后由 flyinghail 于 2007-11-16 18:54 编辑 ]
作者: flyinghail 发布时间: 2007-11-16
作者: sanler 发布时间: 2007-11-23



作者: strongability 发布时间: 2008-02-03
作者: T.T.R 发布时间: 2008-03-27

作者: luzhou 发布时间: 2008-03-27
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28