ImageMagick庫使用

來源:互聯網
上載者:User

ImageMagick庫使用
ImageMagick相對於以前的GD自己感覺使用更加方便,圖片處理的清晰度要高。
以前也用過,都是使用命令的方式,現在使用PHP擴充支援更加方便

安裝:
#wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
#wget http://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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.