CakePhp phpThumb Compenet

参照了一下官网的国际友人的一篇[url=http://bakery.cakephp.org/articles/view/phpthumb-component]帖子[/url]修改的

请先将phpthumb的所有文件放在 app/vendors下,如 phpthumb.class.php 文件的路径是
[quote]app/vendors/phpThumb/phpthumb.class.php[/quote]

修改 index.php 加上 ROOT的定义后面加上 ,默认图片上传的路径


if (!defined('PIC_UPLOAD_DIR')) {
define('PIC_UPLOAD_DIR', ROOT.DS.APP_DIR.DS."upload");
}

然后就可在 controller中调用

var $components = array('phpThumb');

function someFunction(){

//设置参数
$this->phpThumb->setParams($params);

//生成略缩图
$this->phpThumb->generateThumb($path);

//调试信息
$this->phpThumb->debugMsg;

}






<?php

/**
* phpThumb 组件 生成略缩图
*
* @author rikugun
*/

App::import('vendor','phpThumb',array('file' =>'phpThumb'.DS.'phpthumb.class.php'));
class phpThumbComponent extends Object {


/**
* 水印字符
* @var <string>
*/
var $wmtStr = 'www.uCoulor.com';
/**
* 水印字体大小 1-5
* @var <int>
*/
var $wmtFontSize = 5;

/**
* 允许生成的源图形mime类型
* @var <array>
*/
var $allowed_mime_types = array('image/jpeg','image/pjpeg','image/gif','image/png');
/**
* 略缩图生成路径 **必须可写
* @var <string>
*/
var $image_location = 'thumb';

/**
* 错误输出数组
* @var <array>
*/
var $errors = array();
/**
* phpThumb 调试信息
* @var <array>
*/
var $debugMsg = array();
/**
* 图像默认宽 px
* @var <int>
*/
var $width = 300;

/**
* 图像默认高 px
* @var <int>
*/
var $height = 200;

/**
* 图像缩放
* @var <float>
*/
var $zoom_crop = 0;
/**
* jpg 输出质量
* @var <int>
*/
var $q = 95;

var $phpThumb;

function __construct() {
$this->phpThumb = new phpthumb();
}


/**
* 生成略缩图
* @param <string> $filename 格式: 20090505/1211412xxx.jpg
* @return <boolean>
*/
function generateThumb($filename) {

//判断源文件是否存在
if (!file_exists(PIC_UPLOAD_DIR.DS.$filename)) {
$this->addError('源文件: '.PIC_UPLOAD_DIR.DS.$filename.' 不存在.');
return false;
}
//确认输出目录可写
if(!is_writable(WWW_ROOT.$this->image_location)) {
$this->addError('目录: '.WWW_ROOT.$this->image_location.' 不可写.');
return false;
}
$thumbDayDir = basename(dirname($filename));

//确认日期目录
if(!is_dir(WWW_ROOT.$this->image_location.DS.$thumbDayDir)) {
mkdir(WWW_ROOT.$this->image_location.DS.$thumbDayDir,0777,true);
}

//转移水印字符
$sUcoulor = htmlentities(urlencode($this->wmtStr));

// $phpThumb = new phpThumb();
$this->phpThumb->setSourceFilename(PIC_UPLOAD_DIR.DS.$filename);
$this->phpThumb->setParameter('config_allow_src_above_docroot', true);
$this->phpThumb->setParameter('w',$this->width);
$this->phpThumb->setParameter('h',$this->height);
$this->phpThumb->setParameter('zc',$this->zoom_crop);
$this->phpThumb->setParameter('q',$this->q);
//设置水印
$this->phpThumb->setParameter('fltr', "wmt|www.ucolor.com|{$this->wmtFontSize}|BR|EE3322");


if($this->phpThumb->GenerateThumbnail()) {
if(!$this->phpThumb->RenderToFile(WWW_ROOT.$this->image_location.DS.$filename)) {
$this->addError('无法保存生成图片为: '.$this->image_location.DS.$filename);
}
} else {
$this->addError('无法生成略缩图,请查看debug信息!');
}
if(count($this->errors)>0) {
if(file_exists(WWW_ROOT.$this->image_location.DS.$filename)) {
unlink(WWW_ROOT.$this->image_location.DS.$filename);
}
$this->debugMsg = $this->phpThumb->debugmessages;
return false;
} else return true;
}
/**
* 设置phpThumb 参数
* @param <array> $params 参数
*/
function setParams($params) {
foreach ($params as $k=>$v) {
$this->phpThumb->setParameter($k, $v);
}
}
function addError($msg) {
$this->errors[] = $msg;
}
}
?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值