中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在php中對imagick進行擴展

發布時間:2021-02-08 16:22:52 來源:億速云 閱讀:345 作者:Leah 欄目:開發技術

這篇文章給大家介紹如何在php中對imagick進行擴展,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

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

接下來我們針對上述兩個狀況分別提出解決辦法:

狀況一的解決辦法如下:

復制代碼 代碼如下:


/**
    Imagick圖像處理類
    用法:
        //引入Imagick物件
        if(!defined('CLASS_IMAGICK')){require(Inc.'class_imagick.php');}
        $Imagick=new class_imagick();
        $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;
            return ;
        }
        $this->image=new Imagick($path);
        if($this->image){
            $this->type=strtolower($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<=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);
        }
    }
    /**
        更改圖像大小
        參數:
            $width:新的寬度
            $height:新的高度
            $fit: 適應大小
                'force': 把圖像強制改為$width X $height
                'scale': 按比例在$width X $height內縮放圖片,結果不完全等於$width X $height
                'scale_fill':按比例在$width X $height內縮放圖片,沒有像素的地方填充顏色$fill_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();
                    $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 ImagickDraw();
                $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_height=$size['height'];
            $crop_x=0;
            $crop_y=0;
            $crop_w=$src_width;
            $crop_h=$src_height;
            if($src_width*$height > $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->thumbnailImage($width,$height,true);
                $canvas->addImage($image);
                $canvas->setImagePage($width,$height,0,0);
            }
            $image->destroy();
            $this->image=$canvas;
        }
    }
    /**
        添加圖片水印
        參數:
            $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 ;
        default:
            $this->image->writeImage($path);
            return ;
        }
    }
    // 直接輸出圖像到螢幕
    public function output($header=true){
        if($header) header('Content-type: '.$this->type);
        echo $this->image->getImagesBlob();
    }
    /**
        建立縮小圖
        $fit為真時,將保持比例并在$width X $height內產生縮小圖
    /**/
    public function thumbnail($width=100,$height=100,$fit=true){$this->image->thumbnailImage($width,$height,$fit);}
    /**
        給圖像添加邊框
        $width: 左右邊框寬度
        $height: 上下邊框寬度
        $color: 顏色
    /**/
    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 radial_blur($radius){$this->image->radialBlurImage($radius);} // 徑向模糊
    public function add_noise($type=null){$this->image->addNoiseImage($type==null?imagick::NOISE_IMPULSE:$type);} // 添加噪點
    public function 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 function oil_paint($radius){$this->image->oilPaintImage($radius);} // 油畫效果
    public function flop(){$this->image->flopImage();} // 水平翻轉
    public function flip(){$this->image->flipImage();} // 垂直翻轉
}

狀況二的解決辦法如下:

首先用/usr/local/imagemagick/bin/convert -version指令查看一下輸出內容是否已經開啟了多線程,Features:的值為空說明是單線程,如果Features:的值是openMP說明是多線程.imagick的多線程模式有一個bug,他會導致多核心的cpu使用率瞬間飆升到100所以一定要使用它的單線程模式才行.

復制代碼 代碼如下:


Version: ImageMagick 6.7.1-2 2014-05-29 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features:  

 上邊是我配置正確時顯示的結果,如果沒有配置正確會顯示下邊的結果

復制代碼 代碼如下:


Version: ImageMagick 6.7.1-2 2014-05-29 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: openMP

 第一種結果是單線程模式,第二種結果是多線程模式,因為imagick的多線程模式有bug,所以如果您剛開始是用多線程模式安裝的imagick那就必須要yum remove imagemagick將其卸載掉重新安裝才行.

經過重寫class,重裝imagick之后一切正常,而且處理圖像的效能比之以前有了大幅提升

關于如何在php中對imagick進行擴展就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

抚远县| 大渡口区| 新建县| 横峰县| 普安县| 静宁县| 凌源市| 彭水| 怀集县| 石首市| 南涧| 胶州市| 榆中县| 白朗县| 临安市| 永顺县| 敦煌市| 镇坪县| 北宁市| 临沧市| 噶尔县| 金阳县| 蒙自县| 九寨沟县| 台北县| 平山县| 阿图什市| 延川县| 新田县| 连江县| 瓦房店市| 朝阳区| 西城区| 万山特区| 茌平县| 潢川县| 瓮安县| 靖安县| 洪泽县| 田阳县| 图片|