ImageMagick库使用

ImageMagick库使用
ImageMagick相对于以前的GD自己感觉使用更加方便,图片处理的清晰度要高。
以前也用过,都是使用命令的方式,现在使用PHP扩展支持更加方便

安装:
#wgethttp://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
#wgethttp://pecl.php.net/get/imagick-2.2.2.tgz

#tar zxvf ImageMagick.tar.gz
#cd ImageMagick-6.5.1-2/
#./configure
#make
#make install
#cd ../

#tar zxvf imagick-2.2.2.tgz
#cd imagick-2.2.2/
#/usr/local/webserver/php/bin/phpize
#./configure --with-php-config=/usr/local/webserver/php/bin/php-config
#make
#make install
#cd ../

将extension = "imagick.so"写入了php.ini的扩展中

根据日常用到的自己封装了一个类
<?php
/**
* 使用ImageMagick进行图片处理
* @filename class.imagic.php
* @author: lnnujxxy
* @date: 2009/12/18
*/
class Imagic {

/**
* @var 上传路径
*/
public $uploadPath;
/**
* @var 上传保存是否重命名
*/
public $isRename = true;
/**
* @var 保存路径
*/
public $savePath;
/**
* @var 图片处理保存后缀(jpg,jpeg,gif,png)
*/
public $saveExt = 'jpg';
/**
* @var 图片质量 0-100
*/
public $quality = 100;
/**
* @var 生成缩图后缀 $filename_thumb;
*/
public $thumbSuffix = '_thumb';
/**
* @var 生成裁切图后缀 $filename_crop;
*/
public $cropSuffix = '_crop';
/**
* @var 生成水印图后缀 $filename_water;
*/
public $waterSuffix = '_water';

public function __construct($uploadPath, $savePath, $isRename=true, $saveExt='jpg', $quality=100) {
if (extension_loaded('imagick') !== true) {
throw new Exception('Imagick extension is not loaded.');
}
$this->uploadPath = $uploadPath;
$this->savePath = $savePath;
$this->isRename = $isRename;
$this->saveExt = $saveExt;
$this->quality = $quality;
}

/**
* 上传图片
* @param $filename 文件名
* @return String
*/
public function uploadImage($filename) {
$img = !empty($_FILES[$filename]) ? $_FILES[$filename] : null;

if($img==Null) return;
if ($this->isRename) {
$pic = strrpos($img['name'], '.');
$ext = substr($img['name'], $pic+1);
$newName = md5(time().mt_rand(1000, 999)) . '.' . $ext;
} else {
$newName = $img['name'];
}

$imgPath = $this->uploadPath.$newName;
try {
if (move_uploaded_file($img['tmp_name'], $imgPath))
return $this->savePath.$newName;
} catch(Exception $e) {
throw new Exception('上传图片失败!');
}
}

/**
* 等比缩放
* @param $file String 待处理图片路径
* @param $width Int 缩图最大宽
* @param $height Int 缩图最大高
* @return String 保存图片路径
*/
public function thumb($file, $width, $height) {
$pathFile = $this->uploadPath.$file;

$newName = $this->rename($file, $this->thumbSuffix, $this->saveExt);
$image = new Imagick($pathFile);

list($nWidth,$nHeight) = $this->scaleImage(
$image->getImageWidth(),
$image->getImageHeight(),
$width,
$height);
try {
$image->thumbnailImage($nWidth,$nHeight);
$image->setCompression($this->quality);
$image->writeImage($this->savePath.$newName);

return $this->savePath.$newName;
} catch(Exception $e) {
throw new Exception('缩图失败!');
}
}

/**
* 缩裁到指定大小
* @param $file String 待处理图片路径
* @param $cWidth Int 最终宽
* @param $cHeight Int 最终高
* @return String 保存图片路径
*/
public function crop($file, $cWidth, $cHeight) {
$pathFile = $this->uploadPath.$file;
$newName = $this->rename($file, $this->cropSuffix, $this->saveExt);

$image = new Imagick($pathFile);
try {
$image->cropThumbnailImage($cWidth, $cHeight);
$image->setCompression($this->quality);
$image->writeImage($this->savePath.$newName);

return $this->savePath.$newName;
} catch(Exception $e) {
throw new Exception('剪切图失败!');
}
}

/**
* 生成图片水印
* @param $file String 待处理图片路径
* @param $water String 水印路径
* @param $pos 水印生成的位置 1-9
* @return String 保存图片路径
*/
public function water($file, $water, $pos = 9) {
$pathFile = $this->uploadPath.$file;
$newName = $this->rename($file, $this->waterSuffix, $this->saveExt);

$image = new Imagick($pathFile);
$water = new Imagick($water);
list($fileWidth, $fileHeight) = array_values($image->getImageGeometry());
list($waterWidth, $waterHeight) = array_values($water->getImageGeometry());

if($fileWidth < $waterWidth || $fileHeight < $waterHeight) {
return $newName;
}

switch($pos) {
case 1:
$x = +5;
$y = +5;
break;
case 2:
$x = ($fileWidth - $waterWidth) / 2;
$y = +5;
break;
case 3:
$x = $fileWidth - $waterWidth - 5;
$y = +5;
break;
case 4:
$x = +5;
$y = ($fileHeight - $waterHeight) / 2;
break;
case 5:
$x = ($fileWidth - $waterWidth) / 2;
$y = ($fileHeight - $waterHeight) / 2;
break;
case 6:
$x = $fileWidth - $waterWidth;
$y = ($fileHeight - $waterHeight) / 2;
break;
case 7:
$x = +5;
$y = $fileHeight - $waterHeight - 5;
break;
case 8:
$x = ($fileWidth - $waterWidth) / 2;
$y = $fileHeight - $waterHeight - 5;
break;
case 9:
$x = $fileWidth - $waterWidth - 5;
$y = $fileHeight - $waterHeight - 5;
break;
}
try {
$image->compositeImage($water, Imagick::COMPOSITE_DEFAULT, $x, $y);
$image->setCompression($this->quality);
$image->writeImage($this->savePath.$newName);

return $this->savePath.$newName;
} catch(Exception $e) {
throw new Exception('生成水印图失败!');
}
}

private function rename($file, $suffix, $saveExt) {
$ext = trim(substr(strrchr($file, '.'), 0, 10));
return str_replace($ext, $suffix.'.'.$saveExt, $file);
}

/**
* 获得等比缩放图片宽高
* @param $x Int
* @param $y Int
* @param $cx Int
* @param $cy Int
* @return Array
*/
private function scaleImage($x,$y,$cx,$cy) {
//Set the default NEW values to be the old, in case it doesn't even need scaling
list($nx,$ny)=array($x,$y);

//If image is generally smaller, don't even bother
if ($x>=$cx || $y>=$cy) {

//Work out ratios
if ($x>0) $rx=$cx/$x;
if ($y>0) $ry=$cy/$y;

//Use the lowest ratio, to ensure we don't go over the wanted image size
if ($rx>$ry) {
$r=$ry;
} else {
$r=$rx;
}
//Calculate the new size based on the chosen ratio
$nx=intval($x*$r);
$ny=intval($y*$r);
}
//Return the results
return array($nx,$ny);
}
}
/*
$imagic = new Imagic('./', './save/');
var_dump($imagic->thumb('image.jpg', 100, 100));
var_dump($imagic->crop('image.jpg', 100, 100));
var_dump($imagic->water('image.jpg', 'logo_cn.gif', 9));
*/
?>

参考资料:
http://cn.php.net/manual/en/book.imagick.php
http://blog.s135.com/post/366/

转:http://hi.baidu.com/lnnujxxy/blog/item/e50b56a16c047c8047106448.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值