ホームページ  >  記事  >  バックエンド開発  >  PHP 拡張機能 imagick_PHP チュートリアルに関する簡単な説明

PHP 拡張機能 imagick_PHP チュートリアルに関する簡単な説明

WBOY
WBOYオリジナル
2016-07-13 10:29:04663ブラウズ

GD ライブラリは通常、PHP マッピングに使用されます。これは組み込みであり、サーバーに追加のプラグインをインストールする必要がないため、プログラムのメイン機能であれば、より安心して使用できます。画像を処理する場合、GD の使用はお勧めできません。GD は非効率で機能が弱いだけでなく、多くのシステム リソースを消費します。また、GD の creatfrom にもバグがありますが、imagick は良い代替品です。このため、最近プロジェクトの 1 つを GD から imagick に変更しましたが、変更後にいくつかの状況が発生したので、ここで共有したいと思います。

まずここでの状況について話させてください:

状況1: 画像操作クラスの書き換えが必要

状況 2: imagick がマルチスレッドの場合、CPU 使用率が 100% に急増します

ちなみに、imagickをcentos6.4にインストールする方法は次のとおりです:

1.ImageMagickをインストールします

コードをコピーします コードは次のとおりです:
wget http://soft.vpser.net/web/imagemagick/ImageMagick-6.7.1-2.tar.gz
tar zxvf ImageMagick-6.7 .1-2 .tar.gz
cd ImageMagick-6.7.1-2/
./configure --prefix=/usr/local/imagemagick --disable-openmp
make && make install
ldconfig

ImageMagick が適切に動作するかどうかをテストします:

コードをコピーします コードは次のとおりです:
/usr/local/imagemagick/bin/convert -version

2. PHP 拡張機能をインストールします: imagick

コードをコピーします コードは次のとおりです:
wget http://pecl.php.net/get/imagick-3.0.1.tgz
tar zxvf imagick-3.0.1.tgz
cd imagick-3.0.1/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-imagick=/usr/ local/imagemagick
make && make install
ldconfig
vi /usr/local/php/etc/php.ini
追加: extension = "imagick.so"

lnmpを再起動します


コードをコピーします コードは次のとおりです:
/root/lnmp reload

次に、上記の 2 つの状況に対する解決策を提案します:

状況 1 の解決策は次のとおりです:

コードをコピーします コードは次のとおりです:

/**
Imagick图像处理类
用法:
//Imagick物件
if(!define('CLASS_IMAGICK')){require(Inc.'class_imagick.php');}
$Imagick=new ;
$Imagick ->open('a.gif');
$Imagick->resize_to(100,100,'scale_fill');
$Imagick->add_text('1024i.com',10,20);
$Imagick- >add_watermark('1024i.gif',10,50);
$Imagick->save_to('x.gif');
unset($Imagick);
/**/
define('CLASS_IMAGICK',TRUE);
class class_imagick{
private $image=null;
private $type=null;
// 构造
public function __construct(){}
// 析构
public function __destruct(){
if($this->image!==null){$this->image->destroy();}
}
// ダウンロード入画像
public function open($ path){
if(!file_exists($ path)){
$ this-> image = null; if($this->image){
$this->type=strto lower($this->image->getImageFormat());
}
$this->image->stripImage();
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 if($this->type=='gif'){
$image=$this->image;
$canvas =new Imagick();
$images=$image->coalesceImages();
foreach($images as $frame){
$img=new Imagick();
$img-& gt;readImageBlob($frame);
$img->cropImage($width,$height,$x,$y);
$canvas->addImage($img);
$canvas->setImageDelay($img->get ImageDelay()) ;
$canvas->setImagePage($width,$height,0,0);
}
$image->destroy();
$this->image=$canvas;
}else{
$this ->image->cropImage($width,$height,$x,$y);
}
}
/**
画像サイズを変更します パラメータ:
$ width: 新しい幅
$ height: 新しい高さ
$ FIT: サイズの適応
'force': 画像を $ width x $ height に強制します
'scale':: 比率を変更します比率で比率で比率で比率 $width X $height 内で画像を拡大縮小すると、結果は $width 、緑、青、透明度 [0 不透明 - 127 完全透明]) と正確には等しくなりません
を使用する と使用する -いつ'scale_fill'を使用した場合、完全な画像が出力されます $fit=画像の向きを使用した場合、文字と画像の対応関係は次のようになります:
South_westSouthSouth_East
/ **/
public function sinner_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 ->合体画像();
$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 を $frame){
$frame->thumbnailImage($width,$height,true);
$draw=new ImagickDraw();
$draw->composite( $frame->getImageCompose() ,$x,$y,$dst_width,$dst_height,$frame);
$img=new Imagick();
$img->newImage($width,$height,$color,'gif');
$ img->drawImage($draw);
$canvas->addImage($img);
$canvas->setImageDelay($img->getImageDelay());
$canvas->setImagePage($width ,$height,0,0);
}
}else{
$image->thumbnailImage($width,$height,true);
$draw=new
$draw->composite($ image->getImageCompose(),$x,$y,$dst_width,$dst_height,$image);
$canvas->newImage($width,$height,$color,$this->get_type()) ;
$canvas->drawImage($draw);
$canvas->setImagePage($width,$height,0,0);
}
$image->destroy ();
$this-> image=$canvas;
break;
default:
$size=$this->image->getImagePage();
$src_width=$size['width'];
$src_高さ=$サイズ['高さ' ];
$crop_x=0;
$crop_y=0;
$crop_w=$src_width;
$crop_h=$src_height;
if($src _幅*$高さ > $src_height*$width){
$crop_w=intval($src_height*$width/$height);
}else{
$crop_h=intval($src_width*$height/$width);
}
            switch($fit){
                case 'north_west':
                    $crop_x=0;
                    $crop_y=0;
                    break;
                case 'north':
                    $crop_x=intval(($src_width-$crop_w)/2);
                    $crop_y=0;
                    break;
                case 'north_east':
                    $crop_x=$src_width-$crop_w;
                    $crop_y=0;
                    break;
                case 'west':
                    $crop_x=0;
                    $crop_y=intval(($src_height-$crop_h)/2);
                    break;
                case 'center':
                    $crop_x=intval(($src_width-$crop_w)/2);
                    $crop_y=intval(($src_height-$crop_h)/2);
                    break;
                case 'east':
                    $crop_x=$src_width-$crop_w;
                    $crop_y=intval(($src_height-$crop_h)/2);
                    break;
                case 'south_west':
                    $crop_x=0;
                    $crop_y=$src_height-$crop_h;
                    break;
                case 'south':
                    $crop_x=intval(($src_width-$crop_w)/2);
                    $crop_y=$src_height-$crop_h;
                    break;
                case 'south_east':
                    $crop_x=$src_width-$crop_w;
                    $crop_y=$src_height-$crop_h;
                    break;
                default:
                    $crop_x=intval(($src_width-$crop_w)/2);
$crop_y=intval(($src_height-$crop_h)/2);
}
$image=$this->image;
$canvas= new Imagick();
if($this->type=='gif'){
$images=$image->coalesceImages();
foreach($images as $frame){
$img=new Imagick ();
$img->readImageBlob($frame);
$img->cropImage($crop_w,$crop_h,$crop_x,$crop_y);
$img->thumbnailImage($width,$height, true);
$canvas->addImage($img);
$canvas->setImageDelay($img->getImageDelay());
$canvas ->setImagePage($width,$height,0,0 );
}
}else{
$image->cropImage($crop_w,$crop_h,$crop_x,$crop_y);
$image-&g t;thumbnailImage($width,$height,true);
$canvas- >addImage($image);
$canvas->setImagePage($width,$height,0,0);
}
$image->destroy();
$this->image =$キャンバス;
}
}
/**
透かしの画像を追加する数 パラメーター:
$ PATH: 透かし画像 (完全なパスを含む)
$ x, $ y: ウォーター印刷ラベル
/**/
public function add_watermark($path,$x=0,$y=0){
$watermark=new Imagick($path);
$draw=new ImagickDraw();
$ draw->composite($watermark->getImageCompose(),$x,$y,$watermark->getImageWidth(),$watermark->getimageheight(),$watermark);
if($this-> ;type=='gif'){
$image=$this->image;
$canvas=new Imagick();
$images=$image->coalesceImages();
foreach($image as $frame ){
$img=new Imagick();
$img->readImageBlob($frame);
$img->drawImage($draw);
$canvas->addImage($img);
$canvas ->setImageDelay($img->getImageDelay());
}
$image->destroy();
$this->image=$canvas;
}else{
$this->image- >drawImage($draw);
}
}
/**印 テキストウォーターマーク追加 加 パラメータ:
$ Text: ウォーターマークテキスト
$ x, $ y: 水印刷シートラベル
/**/
public function add_text($text,$x=0,$y=0,$angle=0,$style=array() ){
$draw=new ImagickDraw();
if(isset($style['font'])) $draw->setFont($style['font']);
if(isset($style[' font_size'])) $draw->setFontSize($style['font_size']);
if(isset($style['fill_color'])) $draw->setFillColor($style['fill_color']) ;
if(isset($style['under_color'])) $draw->setTextUnderColor($style['under_color']);
if($this->type=='gif'){
foreach( $this->image as $frame){
$frame->annotateImage($draw,$x,$y,$angle,$text);
}
}else{
$this->image-> ;annotateImage($draw,$x,$y,$angle,$text);
}
}
/**
画像アーカイブ: パラメーター:
$ PATH: アーカイブの場所と新しいファイル名
/**/
public function save_to($path){
$this->image->stripImage();
switch($this->type){
case 'gif':
$this->image->writeImages($path,true);
return ;
case 'jpg':
case 'jpeg':
$this->image->setImageCompressionQuality($_ENV['ImgQ' ]);
$this->image->writeImage($path);
return ;
case 'png':
$flag = $this->image->getImageAlphaChannel();
// 如果png背景不透明则压缩
if(imagick::ALPHACHANNEL_UNDEFINED == $flag or imagick::ALPHACHANNEL_DEACTIVATE == $flag){
$this->image->setImageType(imagick::IMGTYPE_PALETTE) );
$this-> image->writeImage($path);
}else{
$this->image->writeImage($path);
}unset($flag);
return ;
デフォルト:
$this-> Image-> writeImage($ path);
return;
}
}
// .$this->type);
echo $this->image->getImagesBlob();
}
/**
リダクションマップを構築する場合 $$FIT が true の場合、比率を維持し、$幅 x $高さの狭いグラフを生成します*/
パブリック関数サムネイル($width=100,$height=100, $fit=true){$this->image->thumbnailImage($width,$height,$fit);}
/**
$幅:左右の境界線の幅*/
public function border($width,$height,$color) ='rgb(220,220,220)'){
$color=new ImagickPixel();
$color->setColor($color);
$this->image->borderImage($color,$width,$height) );
}
// 画像高さ取得
public function get_width(){$size=$this->image->getImagePage();return $size['width'];}
// 画像高さ取得
public function get_height(){$size=$this->image->getImagePage();return $size['height'];}
// 设置图像类型
public function set_type($type='png') {$this->type=$type;$this->image->setImageFormat($type);}
// 画像类型を取得
public function get_type(){return $this->type;}
public function Blur($radius,$sigma){$this->image->blurImage($radius,$sigma);} // 模糊
public function gaussian_blur($radius,$sigma){$this-> image->gaussianBlurImage($radius,$sigma);}// ガウス ブラー
public function motion_blur($radius,$sigma,$angle){$this->image->motionBlurImage($radius,$sigma,$angle);} // モーション ブラー
public function radio_blur( $radius){$this->image->radialBlurImage($radius);} // 放射状ブラー
パブリック関数 add_noise($type=null){$this->image->addNoiseImage($type= = null?imagick::NOISE_IMPULSE:$type);} // ノイズを追加
パブリック関数 level($black_point,$gamma,$white_point){$this->image->levelImage($black_point,$gamma,$white_point) );} // カラー レベルを調整します
public function modulate($brightness,$saturation,$hue){$this->image->modulateImage($brightness,$saturation,$hue);} // カラー レベルを調整します明るさ、彩度、色相
public function Charcoal($radius,$sigma){$this->image->charcoalImage($radius,$sigma);} // スケッチ効果
public functionoil_paint($radius){$ this->image->oilPaintImage($radius);} // 油絵効果
public function flop(){$this->image->flopImage();} // 水平反転
public function flick( ) {$this->image->flipImage();} // 垂直反転
}

状況 2 の解決策は次のとおりです:

まず、/usr/local/imagemagick/bin/convert -version コマンドを使用して、出力コンテンツでマルチスレッドが有効になっているかどうかを確認します。 features: の値が空の場合は、シングルスレッドであることを意味します。特徴: openMP はマルチスレッドであることを意味します。.imagick のマルチスレッド モードにはバグがあり、マルチコア CPU 使用率が瞬時に 100 に達するため、シングルスレッドを使用する必要があります。モード。

コードをコピーします コードは次のとおりです:

バージョン: ImageMagick 6.7.1-2 2014-05-29 Q16 http://www.imagemagick.org
著作権: Copyright (C) 1999- 2011 ImageMagick Studio LLC
特徴:

上記は設定が正しい場合に表示される結果です。設定が正しくない場合は、以下の結果が表示されます。

コードをコピーします コードは次のとおりです:
バージョン: ImageMagick 6.7.1-2 2014-05-29 Q16 http://www.imagemagick.org
著作権: Copyright (C) 1999- 2011 ImageMagick Studio LLC
特徴: openMP

最初の結果はシングルスレッド モードで、2 番目の結果はマルチスレッド モードです。imagick のマルチスレッド モードにはバグがあるため、最初に imagick をマルチスレッド モードでインストールした場合は、imagemagick を yum 削除する必要があります。削除して再インストールしてください。

クラスを書き直してimagickを再インストールしたところ、すべてが正常になり、画像処理のパフォーマンスが以前に比べて大幅に向上しました


http://www.bkjia.com/PHPjc/779163.htmlwww.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/779163.html技術記事 GD ライブラリは通常、PHP マッピングに使用されます。これは組み込みであり、サーバーに追加のプラグインをインストールする必要がないため、プログラムのメイン機能の場合はより安心して使用できます。画像を処理するには...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。