打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Yii的PHP缩图类
userphoto

2013.08.01

关注
(2011-04-23 16:28:22)
为Yii Framework而写
将CThumb.php文件protected/extensions/CThumb下
'components'=>array(
'thumb'=>array(
'class'=>'ext.CThumb.CThumb',
),
);
放在config/main.php中
以下为 CThumb.php 源码
<?php
class CThumb extends CApplicationComponent {
// @varmixed 要调整大小的图像文件
public$image;
// @var int缩略图宽度
public$width = 120;
// @var int缩略图高度
public$height = 120;
// @varstring 保存缩略图的文件夹
public$directory;
// @varstring 默认缩略图的名称
public$defaultName = "thumb";
// @varstring 后缀名称
public$suffix;
// @varstring 前缀名称
public$prefix;
// @var int图像质量
public$quality = 75;
// @var intPNG文件转化率
public$compression = 6;
// @varint  客户端选择区域左上角X轴坐标
public $posX= 0;
// @varint  客户端选择区域左上角Y轴坐标
public $posY= 0;
public $cutX= 0;
public $cutY= 0;
// @var int切割宽度
public $mode= 1;
// @varmixed 临时图像
private$img;
// @varmixed 输出图像
private$thumbImage;
// @var int原图宽度
private$srcWidth;
// @var int原图
private$srcHeight;
// @var int原图类型
private$srcType;
// @var int原图扩展名
private$srcExt;
publicfunction init() {
if (!function_exists("imagecreatetruecolor")) {
throw new Exception("使用这个类,需要启用GD库", 500);
}
parent::init();
}
publicfunction getImageSize() {
if (!$this->thumbImage)
$this->loadImage();
$imageSize = array('width' =>$this->srcWidth, 'height' =>$this->srcHeight, 'ext' =>$this->srcExt);
return $imageSize;
}
privatefunction loadImage() {
if (!$this->image) {
throw new Exception("必须有图片名称", 500);
}
list($this->srcWidth,$this->srcHeight, $this->srcType) =getimagesize($this->image);
switch ($this->srcType) {
case IMAGETYPE_JPEG:
$this->img =imagecreatefromjpeg($this->image);
$this->srcExt = 'jpg';
break;
case IMAGETYPE_GIF:
$this->img =imagecreatefromgif($this->image);
$this->srcExt = 'gif';
break;
case IMAGETYPE_PNG:
$this->img =imagecreatefrompng($this->image);
$this->srcExt = 'png';
break;
default:
throw new Exception("不支持的图像类型", 500);
}
}
publicfunction createThumb() {
$this->loadImage();
$this->thumbImage =imagecreatetruecolor($this->width,$this->height);
$bg = imagecolorallocatealpha($this->thumbImage,255, 255, 255, 127);
imagefill($this->thumbImage, 0, 0, $bg);
imagecolortransparent($this->thumbImage, $bg);
$ratio_w = 1.0 * $this->width /$this->srcWidth;
$ratio_h = 1.0 * $this->height /$this->srcHeight;
$ratio = 1.0;
switch ($this->mode) {
case1:       // 强制裁剪,生成图片严格按照需要,不足放大,超过裁剪,图片始终铺满
if (($ratio_w < 1 &&$ratio_h < 1) || ($ratio_w > 1&& $ratio_h > 1)){
$ratio = $ratio_w < $ratio_h ? $ratio_h :$ratio_w;
$tmp_w = (int) ($this->width / $ratio);
$tmp_h = (int) ($this->height / $ratio);
$tmp_img = imagecreatetruecolor($tmp_w, $tmp_h);
$src_x = (int) (($this->srcWidth - $tmp_w) /2);
$src_y = (int) (($this->srcHeight - $tmp_h) /2);
imagecopy($tmp_img, $this->img, 0, 0, $src_x,$src_y, $tmp_w, $tmp_h);
imagecopyresampled($this->thumbImage, $tmp_img, 0,0, 0, 0, $this->width, $this->height,$tmp_w, $tmp_h);
imagedestroy($tmp_img);
} else {
$ratio = $ratio_w < $ratio_h ? $ratio_h :$ratio_w;
$tmp_w = (int) ($this->srcWidth * $ratio);
$tmp_h = (int) ($this->srcHeight * $ratio);
$tmp_img = imagecreatetruecolor($tmp_w, $tmp_h);
imagecopyresampled($tmp_img, $this->img, 0, 0, 0, 0,$tmp_w, $tmp_h, $this->srcWidth,$this->srcHeight);
$src_x = (int) ($tmp_w - $this->width) / 2;
$src_y = (int) ($tmp_h - $this->height) / 2;
imagecopy($this->thumbImage, $tmp_img, 0, 0, $src_x,$src_y, $this->width,$this->height);
imagedestroy($tmp_img);
}
break;
case2:       // 和1类似,但不足的时候不放大 会产生补白,可以用png消除
if ($ratio_w < 1 &&$ratio_h < 1) {
$ratio = $ratio_w < $ratio_h ? $ratio_h :$ratio_w;
$tmp_w = (int) ($this->width / $ratio);
$tmp_h = (int) ($this->height / $ratio);
$tmp_img = imagecreatetruecolor($tmp_w, $tmp_h);
$src_x = (int) ($this->srcWidth - $tmp_w) / 2;
$src_y = (int) ($this->srcHeight - $tmp_h) /2;
imagecopy($tmp_img, $this->img, 0, 0, $src_x,$src_y, $tmp_w, $tmp_h);
imagecopyresampled($this->thumbImage, $tmp_img, 0,0, 0, 0, $this->width, $this->height,$tmp_w, $tmp_h);
imagedestroy($tmp_img);
} elseif ($ratio_w > 1&& $ratio_h > 1){
$dst_x = (int) abs($this->width -$this->srcWidth) / 2;
$dst_y = (int) abs($this->height -$this->srcHeight) / 2;
imagecopy($this->thumbImage,$this->img, $dst_x, $dst_y, 0, 0,$this->srcWidth,$this->srcHeight);
} else {
$src_x = 0;
$dst_x = 0;
$src_y = 0;
$dst_y = 0;
if (($this->width - $this->srcWidth)< 0) {
$src_x = (int) ($this->srcWidth -$this->width) / 2;
$dst_x = 0;
} else {
$src_x = 0;
$dst_x = (int) ($this->width -$this->srcWidth) / 2;
}
if (($this->height -$this->srcHeight) < 0) {
$src_y = (int) ($this->srcHeight -$this->height) / 2;
$dst_y = 0;
} else {
$src_y = 0;
$dst_y = (int) ($this->height -$this->srcHeight) / 2;
}
imagecopy($this->thumbImage,$this->img, $dst_x, $dst_y, $src_x, $src_y,$this->srcWidth,$this->srcHeight);
}
break;
case3:       // 只缩放,不裁剪,保留全部图片信息,会产生补白
if ($ratio_w > 1 &&$ratio_h > 1) {
$dst_x = (int) (abs($this->width -$this->srcWidth) / 2);
$dst_y = (int) (abs($this->height -$this->srcHeight) / 2);
imagecopy($this->thumbImage,$this->img, $dst_x, $dst_y, 0, 0,$this->srcWidth,$this->srcHeight);
} else {
$ratio = $ratio_w > $ratio_h ? $ratio_h :$ratio_w;
$tmp_w = (int) ($this->srcWidth * $ratio);
$tmp_h = (int) ($this->srcHeight * $ratio);
$tmp_img = imagecreatetruecolor($tmp_w, $tmp_h);
imagecopyresampled($tmp_img, $this->img, 0, 0, 0, 0,$tmp_w, $tmp_h, $this->srcWidth,$this->srcHeight);
$dst_x = (int) (abs($tmp_w - $this->width) /2);
$dst_y = (int) (abs($tmp_h - $this->height) /2);
imagecopy($this->thumbImage, $tmp_img, $dst_x,$dst_y, 0, 0, $tmp_w, $tmp_h);
imagedestroy($tmp_img);
}
break;
case4:       // 只缩放,不裁剪,保留全部图片信息,生成图片大小为最终缩放后的图片有效信息的实际大小,不产生补白
if ($ratio_w > 1 &&$ratio_h > 1) {
$this->thumbImage =imagecreatetruecolor($this->srcWidth,$this->srcHeight);
imagecopy($this->thumbImage,$this->img, 0, 0, 0, 0,$this->srcWidth,$this->srcHeight);
} else {
$ratio = $ratio_w > $ratio_h ? $ratio_h :$ratio_w;
$tmp_w = (int) ($this->srcWidth * $ratio);
$tmp_h = (int) ($this->srcHeight * $ratio);
$this->thumbImage = imagecreatetruecolor($tmp_w,$tmp_h);
imagecopyresampled($this->thumbImage,$this->img, 0, 0, 0, 0, $tmp_w, $tmp_h,$this->srcWidth,$this->srcHeight);
}
break;
case5:       // 根据posX、posY定位裁剪
$this->thumbImage =imagecreatetruecolor($this->width,$this->height);
imagecopyresampled($this->thumbImage,$this->img, 0, 0, $this->posX,$this->posY, $this->width,$this->height, $this->cutX,$this->cutY);
break;
}
}
publicfunction save() {
if (!$this->directory) {
throw new Exception("输入保存缩略图目录", 500);
}
switch ($this->srcType) {
case IMAGETYPE_JPEG:
imagejpeg($this->thumbImage,$this->directory . $this->prefix .$this->defaultName . $this->suffix ."." . $this->srcExt,$this->quality);
break;
case IMAGETYPE_GIF:
imagegif($this->thumbImage,$this->directory . $this->prefix .$this->defaultName . $this->suffix ."." . $this->srcExt,$this->quality);
break;
case IMAGETYPE_PNG:
imagepng($this->thumbImage,$this->directory . $this->prefix .$this->defaultName . $this->suffix ."." . $this->srcExt,$this->compression);
break;
}
}
publicfunction show() {
switch ($this->srcType) {
case IMAGETYPE_JPEG :
header('Content-type: image/jpeg');
imagejpeg($this->thumbImage);
break;
case IMAGETYPE_PNG :
header('Content-type: image/png');
imagepng($this->thumbImage);
break;
case IMAGETYPE_GIF :
header('Content-type: image/gif');
imagegif($this->thumbImage);
break;
}
}
function__destruct() {
imagedestroy($this->img);
imagedestroy($this->thumbImage);
}
}
?>
用法
在Controller中
$sourceFilename ='/home/caige/www/admin/application/images/source/filename.ext';
$thumbPath = dirname($request->getScriptFile()) .'/images/thumb/';
if (!is_dir($thumbPath)) {
mkdir($thumbPath, 0777);
} else {
$thumb = Yii::app()->thumb;
$thumb->image = $oldFilename;
$thumb->width = 150;
$thumb->height = 210;
$thumb->directory = $thumbPath;
$thumb->defaultName = $newFilename;
$thumb->createThumb();
$thumb->save();
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
双线性插值(Bilinear interpolation)的图像旋转在mobile上面的C++实现
C++ 从一幅图片上裁取需要的区域
“四君子”梅、兰、竹、菊,
了解代码常识的利器
健康与养生大全(一)
中药:砂仁、豆蔻、草豆蔻?、草果
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服