imagemagick根据请求生成大小缩略图
来源:互联网 发布:无锡华云数据工资待遇 编辑:程序博客网 时间:2024/05/16 07:45
安装JPG,PNG开发包
apt-get install libjpeg62-dev libpng12-dev libtiff4-dev
安装ImageMagick和imagick
tar zxvf ImageMagick.tar.gz cd ImageMagick-6.5.1-2/ ./configure make make install cd ../ tar zxvf imagick-2.3.0.tgz cd imagick-2.3.0/ /usr/local/webserver/php/bin/phpize ./configure --with-php-config=/usr/local/webserver/php/bin/php-config make make install cd ../
如果:no decode delegate for this image format
安装:apt-get install libjpeg62-dev libpng12-dev libtiff4-dev后再重新安装ImageMagick和imagick
如果:libMagickCore.so.5: cannot open shared object file
执行命令:ldconfig /usr/local/lib
修改php.ini
extension = "imagick.so"
重启php-fpm
/etc/init.d/php5-fpm restart
增加rewrite
rewrite ^/uploads/(.*)\.(.*)$ /img.php last;
重启nignx
nginx -s reload
增加img.php代码处理
<?php /* A thumbnail creation. */ /* Create the Imagick object */ $img = $_SERVER['REQUEST_URI']; define('DEDEROOT', str_replace("\\", '/', dirname(__FILE__) ) );preg_match("/(.*)_(\d+)_(\d+)\.(\w+)/is",$img,$match); $getWidth = $match[2]; $getHeight = $match[3]; $normalImg = DEDEROOT. $match[1].".".$match[4]; $smallImg = DEDEROOT. $match[0]; $im = new Imagick(); if (!$match[4] || !file_exists($normalImg)) { $normalImg = DEDEROOT. $img; if (!file_exists($normalImg)) { //没有图片 header("HTTP/1.1 404 Not Found"); exit; } //使用原图 $im->readImage( $normalImg); header( "Content-Type: image/{$im->getImageFormat()}" ); echo $im->getImageBlob(); $im->destroy(); exit; } /* Read the image file */ $im->readImage( $normalImg); if ($getWidth && $getHeight) { $srcWH = $im->getImageGeometry(); $rate_w = $getWidth / $srcWH['width']; $rate_h = $getHeight / $srcWH['height']; $rate = ($rate_w > $rate_h) ? $rate_w : $rate_h; $rate = $rate > 1 ? 1 : $rate; $thumb_w = round($srcWH['width'] * $rate); $thumb_h = round($srcWH['height'] * $rate); //缩略 $im->thumbnailImage( $thumb_w, $thumb_h ); if ($thumb_w>$getWidth || $thumb_h>$getHeight) { $fromX = ($thumb_w-$getWidth)/2; //剪切 $im->cropImage($getWidth, $getHeight,$fromX,0); } } elseif ($getWidth && !$getHeight) { $im->thumbnailImage( $getWidth, null); } elseif (!$getWidth && $getHeight) { $im->thumbnailImage(null, $getHeight ); } /* Write the thumbail to disk */ $im->writeImage($smallImg); header( "Content-Type: image/{$im->getImageFormat()}" ); echo $im->getImageBlob(); /* Free resources associated to the Imagick object */ $im->destroy(); ?>
优化:
nginx的rewrite时可以做个判断,如果已经有生成的图片则直接显示。不用再到php里处理。
if (!-f $request_filename) {
rewrite ^/uploads/(.*)\.(.*)$ /img.php last;
}
参考:
http://www.digitalsanctum.com/2009/03/18/installing-imagemagick-from-source-on-ubuntu-804/
http://bootpolish.net/home_howto_installimagemagick
http://wenku.baidu.com/view/2e93f278168884868762d689.html
http://apps.hi.baidu.com/share/detail/37348887
http://hi.baidu.com/houwenbin1986/blog/item/a0afa1fa3adcad294e4aea48.html
http://www.zhouchen33.com/index.php/archives/284
- imagemagick根据请求生成大小缩略图
- 按照指定大小,生成缩略图
- C# 生成指定大小缩略图
- php 缩略图生成类,支持imagemagick及gd库两种处理
- C#根据原图生成缩略图介绍
- java根据url生成网页截图,缩略图
- php 根据url自动生成缩略图
- asp.net 根据图片路径生成缩略图
- net上传图片生成大小缩略图
- asp.net生成指定大小缩略图
- .net生成固定大小缩略图,不变形
- C#生成缩略图,指定像素大小
- 根据指定的图像路径和大小来获取缩略图
- php实现的支持imagemagick及GD库两种处理的缩略图生成类
- 生成规定大小的图片(缩略图生成)
- 生成规定大小的图片(缩略图生成)
- 根据图片URL裁切并生成缩略图
- C# 根据图片生成缩略图.直接输出到客户端.
- 利用Android系统的传感器开发水平仪应用的全过程介绍
- oracle自动备份
- linux zip unzip指令使用方法
- BI学习计划
- CF 567B Berland National Library
- imagemagick根据请求生成大小缩略图
- 9-2
- C#操作Cookie
- 268. Missing Number
- 第四节 Set、Map、Tuple、队列操作实战
- H5 localstorage存储文件
- Maven - 父工程的使用
- BI导论——数据仓库概论
- centos6.5安装python-MySQLdb