PHP imagick扩展使用心得
2014-03-24 15:20
316 查看
/** * 图片缩放和裁剪,并进行存储 * @param string $srcPath 图片源地址 * @param string $dstPath 目标存储地址 * @param int $thumbWidth 缩略图宽度 * @param array $crop 裁剪尺寸 * @return string $err 错误信息 */ function create_thumb($srcPath, $dstPath, $thumbWidth, $crop = array()){ $err = ''; try{ $im = new Imagick($srcPath); $im->setimageformat('png'); $im->stripimage(); $oriWidth = $im->getimagewidth(); $oriHeight = $im->getimageheight(); $quotient = $oriWidth / $oriHeight; $thumbHeight = $thumbWidth / $quotient; if ($oriWidth > $thumbWidth) { $im->resizeimage($thumbWidth, $thumbHeight, Imagick::FILTER_LANCZOS, 1, false); } if (!empty($crop)) { $currentWidth = $im->getimagewidth(); $currentHeight = $im->getimageheight(); $scale = $crop['width'] / $crop['height']; if ($quotient === $scale) { $im->resizeimage($crop['width'], $crop['height'], Imagick::FILTER_LANCZOS, 1, false); } elseif ($quotient > $scale) { $cutWidth = $currentHeight * $scale; $im->cropimage($cutWidth, $currentHeight, $currentWidth/4, 0); $im->resizeimage($crop['width'], $crop['height'], Imagick::FILTER_LANCZOS, 1, false); } elseif ($quotient < $scale) { $cutHeight = $currentWidth / $scale; $im->cropimage($currentWidth, $cutHeight, 0, 0); $im->resizeimage($crop['width'], $crop['height'], Imagick::FILTER_LANCZOS, 1, false); } } $im->writeimage($dstPath); $im->destroy(); }catch (ImagickException $e){ $err = $e->getMessage(); } return $err; }
例如你想把一张图片缩放到480宽,并裁剪成一个192*192的正方形,可以这样调用:
create_thumb('c:/test.png', 'd:/test.png', 480, array('width'=>192, 'height'=>192));
相关文章推荐
- ZH奶酪:PHP图片压缩(TinyPNG在线API)和(使用Imagick扩展)
- PHP使用imagick扩展来合并图像
- PHP扩展插件imagick使用笔记
- PHP使用imagick扩展实现合并图像的方法
- 使用PHP扩展php_imagick对图片改变大小、旋转、锐化、减色或增加特效
- imagick php的扩展的安装和使用
- php5.3.29使用imagick扩展
- PHP使用imagick扩展来合并图像
- JMS(Jboss Messaging)的一点使用心得(四)Spring扩展应用-可修改的JMS ConnectionFactory
- CentOS6.5 使用phpize 配置mysql和pdo扩展
- 使用php_imagick对图片改变大小、旋转、锐化、减色或是增加特效
- 使用php的mysqli扩展库操作mysql数据库
- centos系统上为php安装imagick扩展以及magickwand扩展的方法
- PHP使用mysqli扩展连接MySQL数据库
- 使用Windows缓存进行PHP的扩展
- 【原创】在Windows系统中使用VC9、VC11编译32位、64位PHP及其扩展
- 搭建memcached使用:/usr/bin/phpize 安装memcached扩展的时候报错
- php学习笔记(二十四)php使用memcache扩展库的使用和安装
- 使用 Rust 创建 PHP 扩展
- CentOS下安装ImageMagick和PHP Imagick扩展