nginx+php 处理图片
有的时候要生成大量尺寸的缩图,事先不知道有哪些尺寸,所以可以用php动态生成;
1.在nginx中配置
location ~ .*\.(gif|jpg|jpeg|png|bmp)$
{
if ( !-f $request_filename) {
rewrite ^([^\.]*)/(.*)_(\d+)_(\d+).(gif|jpg|jpeg|png|bmp)$
/ImageTransferController.php?s=$1/$2_$3_$4.$5;
}
expires 30d;
}
2、自己编写ImageTransferController.php的代码,动态生成缩略图
ImageTransferImagick.php代码如下
<?php
/**
*图像转换类
* @author sk2ilvsh
* @email sk2ilvsh@gmail.com
*
*/
require_once APP_ROOT . 'ImageEntity.php';
require_once APP_ROOT . 'IImageTransfer.php';
class ImageTransferImagick extends ImageEntity implements IImageTransfer{
/**
* 压缩度
* @var int
*/
private $imageQuality = 0.75;
/**
* 图片资源
* @var Imagick
*/
private $image;
/**
* 文件名
* @var string
*/
private $fileName = '';
/**
* 构造函数
*/
public function __construct(){
$config = new SystemConfig();
$this->imageQuality = $config->getImgCompressionFactor();
}
/**
* 更改图片大小
* @param string $fileName
* @param int $width
* @param int $height
* @return ImageEntity 失败时返回false
*/
public function resize($fileName, $width, $height){
if (!is_file($fileName)){
return false;
}
//检测宽高参数
if ( (!is_int($width)) || (!is_int($height)) ){
return false;
}
if ( ($width < 1) || ($height < 1) ){
return false;
}
//初始化
$this->init($fileName);
if ($width >= $this->width || $height >= $this->height){
return $this;
}
$type = $this->getType();
//更改图片
switch ($type){
case 'gif':
$this->resizeGif($width, $height);
break;
default:
$this->resizeOther($width, $height);
break;
}
return $this;
}
/**
* 输出图像
*/
public function output() {
header('Content-type:image/' . $this->type);
if ($this->type == 'gif'){
echo file_get_contents($this->fileName);
}else{
$this->setCompression();
echo $this->image;
}
}
/**
* 图像保存
* @param string $dstName
*/
public function save($dstName){
//检测到有图片存在,则删除
if (is_file($dstName)){
unlink($dstName);
}
if ($this->image != null){
if ($this->type == 'gif')
$this->image->writeImages($dstName, true);
else
$this->image->writeImage($dstName);
}
}
/**
* 析构函数
*/
public function __destruct(){
if ($this->image !== null){
$this->image->clear();
$this->image->destroy();
}
}
/**
* 初始化图片信息
* @param string $fileName
*/
private function init($fileName){
$this->fileName = $fileName;
$this->image = new Imagick($fileName);
//获取图片类型
if ($this->image)
$this->type = strtolower($this->image->getImageFormat());
$size = $this->image->getImagePage();
//获取原图宽高
$this->setWidth($size['width']);
$this->setHeight($size['height']);
}
/**
* 处理jpg jpeg png
* @param int $width
* @param int $height
*/
private function resizeOther($width, $height){
$this->image->thumbnailImage($width, $height, false);
}
/**
* 输处理gif图像
* @param int $width
* @param int $height
*/
private function resizeGif($width, $height){
// $this->image = $this->image->coalesceImages();
// do {
// $this->image->resizeImage($width, $height, Imagick::FILTER_BOX, 1);
// } while($this->image->nextImage());
// $this->image = $this->image->deconstructImages();
}
/**
* 设置图像压缩输出
*/
private function setCompression(){
//压缩为jpg格式
$this->image->setImageCompression(Imagick::COMPRESSION_JPEG);
//$a 图片的压缩度
$a = $this->image->getImageCompressionQuality() * $this->imageQuality;
if ($a == 0)
$a = 75;
$this->image->setImageCompressionQuality($a);
$this->image->stripImage();
}
}
相关接口定义:
interface IImageTransfer{
/**
* 更改图像大小
* @param string $fileName
* @param int $width
* @param int $height
*/
public function resize($fileName, $width, $height);
}

浙公网安备 33010602011771号