我們可以通過php_imagick實現圖片剪下、旋轉、銳化、減色或增加,下面是小編給大家提供的實現方法,大家可以參考閱讀,更多詳情請關注應屆畢業生考試網。
一個可以供PHP呼叫ImageMagick功能的PHP擴充套件。使用這個擴充套件可以使PHP具備和ImageMagick相同的功能。
ImageMagick是一套功能強大、穩定而且免費的工具集和開發包,可以用來讀、寫和處理超過185種基本格式的圖片檔案,包括流行的TIFF, JPEG, GIF, PNG, PDF以及PhotoCD等格式。利用ImageMagick,你可以根據web應用程式的需要動態生成圖片, 還可以對一個(或一組)圖片進行改變大小、旋轉、銳化、減色或增加特效等操作,並將操作的結果以相同格式或其它格式儲存。
php_imagick是PHP對圖片處理的一個擴充套件包,可以完成對圖片改變大小、旋轉、銳化、減色或增加特效等操作。
一、windows下安裝Imagick擴充套件:
1、下載 ImageMagick並安裝
http://image__magick/binaries/
2、下載php_
_nts/php_
如果你用的是執行緒安全的php,請下載
windows-builds/php53/imagick-2.3.0-dev/vc9_zts/php_
3、設定
在中新增
extension=php_ ,重啟web server
二、linux下安裝Imagick擴充套件:
安裝ImageMagick
yum install ImageMagick ImageMagick-devel
2.測試是否安裝成功
convert -version
3.安裝imagick擴充套件
xzvf ze05../ install
4.編輯檔案,在檔案末尾新增如下程式碼
extension=
5. 重新啟動apache伺服器
service httpd restart
三、案例
1. 邊框處理
複製程式碼 程式碼如下:
header('Content-type: image/jpeg');
$image = new Imagick('test.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->borderImage($color,5,4);
$image->blurImage(5,5,imagick::CHANNEL_GREEN);
echo $image;
我們先來看個簡單的'例項
php_imagick程式示例
1.建立一個縮圖並顯示出來
複製程式碼 程式碼如下:
<?php
header('Content-type: image/jpeg');
$image = new Imagick('image.jpg');
// If 0 is provided as a width or height parameter,// aspect ratio is maintained
$image->thumbnailImage(100, 0);
echo $image;
?>
2.建立一個目錄下的縮圖,並儲存
複製程式碼 程式碼如下:
<?php
$images = new Imagick(glob('images/*.JPG'));
foreach($images as $image) {
// Providing 0 forces thumbnailImage to maintain aspect ratio
$image->thumbnailImage(1024,0);
}
$images->writeImages();
?>
3.縮略GIF動畫圖片
複製程式碼 程式碼如下:
<?php
/* Create a new imagick object and read in GIF */
$im = new Imagick("example.gif");
/* Resize all frames */
foreach ($im as $frame) {
/* 50x50 frames */
$frame->thumbnailImage(50, 50);
/* Set the virtual canvas to correct size */
$frame->setImagePage(50, 50, 0, 0);
}/* Notice writeImages instead of writeImage */
$im->writeImages("example_small.gif", true);
?>
現在我們進入正題吧,
示例:
裁切/生成縮圖/新增水印, 自動檢測和處理 GIF
呼叫方式:
複製程式碼 程式碼如下:
include '';
$image = new lib_image_imagick();
$image->open('a.gif');
$image->resize_to(100, 100, 'scale_fill');
$image->add_text('', 10, 20);
$image->add_watermark('1024i.gif', 10, 50);
$image->save_to('x.gif');
<?php
class lib_image_imagick
{
private $image = null;
private $type = null;
// 建構函式
public function __construct(){}
// 解構函式
public function __destruct()
{
if($this->image!==null) $this->image->destroy();
}
// 載入影象
public function open($path)
{
$this->image = new Imagick( $path );
if($this->image)
{
$this->type = strtolower($this->image->getImageFormat());
}
return $this->image;
}
public function crop($x=0, $y=0, $width=null, $height=null)
{
if($width==null) $width = $this->image->getImageWidth()-$x;
if($height==null) $height = $this->image->getImageHeight()-$y;
if($width<=0 || $height<=0) return;
if($this->type=='gif')
{
$image = $this->image;
$canvas = new Imagick();
$images = $image->coalesceImages();
foreach($images as $frame){
$img = new Imagick();
$img->readImageBlob($frame);
$img->cropImage($width, $height, $x, $y);
$canvas->addImage( $img );
$canvas->setImageDelay( $img->getImageDelay() );
$canvas->setImagePage($width, $height, 0, 0);
}
$image->destroy();
$this->image = $canvas;
}
else
{
$this->image->cropImage($width, $height, $x, $y);
}
}
/*
* 更改影象大小
$fit: 適應大小方式
'force': 把圖片強制變形成 $width X $height 大小
'scale': 按比例在安全框 $width X $height 內縮放圖片, 輸出縮放後圖像大小 不完全等於 $width X $height
'scale_fill': 按比例在安全框 $width X $height 內縮放圖片,安全框內沒有畫素的地方填充色, 使用此引數時可設定背景填充色 $bg_color = array(255,255,255)(紅,綠,藍, 透明度) 透明度(0不透明-127完全透明))
其它: 智慧模能 縮放影象並載取影象的中間部分 $width X $height 畫素大小
$fit = 'force','scale','scale_fill' 時: 輸出完整影象
$fit = 影象方位值 時, 輸出指定位置部分影象
字母與影象的對應關係如下:
north_west north north_east
west center east
south_west south south_east
*/
public function resize_to($width = 100, $height = 100, $fit = 'center', $fill_color = array(255,255,255,0) )
{
switch($fit)
{
case 'force':
if($this->type=='gif')
{
$image = $this->image;
$canvas = new Imagick();
$images = $image->coalesceImages();
foreach($images as $frame){
$img = new Imagick();
$img->readImageBlob($frame);
$img->thumbnailImage( $width, $height, false );
$canvas->addImage( $img );
$canvas->setImageDelay( $img->getImageDelay() );
}
$image->destroy();
$this->image = $canvas;
}
else
{
$this->image->thumbnailImage( $width, $height, false );
}
break;
case 'scale':
if($this->type=='gif')
{
$image = $this->image;
$images = $image->coalesceImages();
$canvas = new Imagick();
foreach($images as $frame){
$img = new Imagick();
$img->readImageBlob($frame);
$img->thumbnailImage( $width, $height, true );
$canvas->addImage( $img );
$canvas->setImageDelay( $img->getImageDelay() );
}
$image->destroy();
$this->image = $canvas;
}
else
{
$this->image->thumbnailImage( $width, $height, true );
}
break;
case 'scale_fill':
$size = $this->image->getImagePage();
$src_width = $size['width'];
$src_height = $size['height'];
$x = 0;
$y = 0;
$dst_width = $width;
$dst_height = $height;
if($src_width*$height > $src_height*$width)
{
$dst_height = intval($width*$src_height/$src_width);
$y = intval( ($height-$dst_height)/2 );
}
else
{
$dst_width = intval($height*$src_width/$src_height);
$x = intval( ($width-$dst_width)/2 );
}
$image = $this->image;
$canvas = new Imagick();
$color = 'rgba('.$fill_color[0].','.$fill_color[1].','.$fill_color[2].','.$fill_color[3].')';
if($this->type=='gif')
{
$images = $image->coalesceImages();
foreach($images as $frame)
{
$frame->thumbnailImage( $width, $height, true );
$draw = new ImagickDraw();