Ubuntu 安装 ImageMagic(6.9.1-6)及 PHP 的 imagick (3.0.1)扩展

关于 ImageMagic 和 imagick 的介绍,见《图片处理神器ImageMagick以及PHP的imagick扩展》 和 《Ubuntu下安装ImageMagick和MagicWand For PHP》,安装和代码也都参考自这几篇文章,同时记录下了自己的安装过程以及自己在安装过程中遇到的问题。

说明:ImageMagic 的 PHP 扩展可以用 imagick  和 MagicWand for PHP,这里安装使用 imagick。

安装环境:Ubuntu 13.10 (GNU/Linux 3.11.0-12-generic i686)

Nginx 版本:1.2.7

Nginx 安装路径:/usr/local/nginx

Nginx 网站根目录:/home/wwwroot

PHP 版本:5.3.27

PHP 框架:ThinkPHP 3.2.2

PHP 安装路径:/usr/local/php

PHP 配置文件 php.ini 路径:/usr/local/php/etc/php.ini

(一)安装 ImageMagic (6.9.1-6)

下载地址:http://www.imagemagick.org/download/

① 下载安装包 ImageMagick-6.9.1-6.tar.gz 并通过 ftp 上传到服务器环境,可以通过命令:

find / -name *.tar.gz

找到之前安装包的存放路径,cd 进入该目录:

cd /home/***/lnmp/lnmp1.0-full/

解压压缩包:

tar -zxvf ImageMagick-6.9.1-6.tar.gz

cd 进入解压之后的目录:

cd ImageMagick-6.9.1-6/

② 配置:

./configure --enable-shared --enable-lzw --without-perl --with-modules 

③ 编译和安装:

make && make install

④ 测试 ImageMagic 是否安装成功:

convert -version

如果执行该命令后报错:

/usr/local/bin/convert: error while loading shared libraries: libMagickCore-6.Q16.so.2: cannot open shared object file: No such file or directory

则需要执行 ldconfig(ldconfig命令的用途, 主要是在默认搜寻目录( /lib和/usr/lib ) 以及动态库配置文件 /etc/ld.so.conf 内所列的目录下, 搜索出可共享的动态链接库( 格式如lib*.so* ), 进而创建出动态装入程序( ld.so )所需的连接和缓存文件. 缓存文件默认为/etc/ld.so.cache, 此文件保存已排好序的动态链接库名字列表. ):

[email protected]***:/home/***/lnmp/lnmp1.0-full/ImageMagick-6.9.1-6# ldconfig

此时再使用 convert -version,就可以显示版本信息了,也就说明安装成功了:

[email protected]***:/home/***/lnmp/lnmp1.0-full/ImageMagick-6.9.1-6# /usr/local/bin/convert -version

Version: ImageMagick 6.9.1-6 Q16 i686 2016-06-15 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib freetype jng jpeg ltdl png xml zlib

(参考:《undefined reference to `inflateReset2‘》)

(二). 安装 imagick

主要参考《Linux下php安装imagick

① 下载、拷贝、安装 imagick

下载地址:http://pecl.php.net/get/imagick-3.0.1.tgz

[email protected]***:/home/***/lnmp/lnmp1.0-full# tar -zxvf imagick-3.0.1.tgz 

解压之后,进入解压后的目录:

[email protected]***:/home/***/lnmp/lnmp1.0-full# cd imagick-3.0.1/

②  用 phpize 生成 configure 配置文件

[email protected]***:/home/***/lnmp/lnmp1.0-full/imagick-3.0.1# /usr/local/php/bin/phpize 

如果报错:checking for MagickWand.h header file... configure: error: Cannot locate header file MagickWand.h,则:

ln -s /usr/local/include/ImageMagick-6 /usr/local/include/ImageMagick

(参考: 《安装imagick时Cannot locate header file MagickWand.h错误的解决》)

③ 配置:

[email protected]***:/home/***/lnmp/lnmp1.0-full/imagick-3.0.1# ./configure  --with-php-config=/usr/local/php/bin/php-config --with-imagick=/usr/local/imagemagick 

④ 编译与安装

make && make install

make install 成功后显示:

[email protected]***:/home/***/lnmp/lnmp1.0-full/imagick-3.0.1# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
Installing header files:          /usr/local/php/include/php/

⑤ 配置 php,让 php 支持 imagick

vi /usr/local/php/etc/php.ini  #编辑配置文件,在最后一行添加以下内容
extension="imagick.so"

⑥ 重启 php-fpm:

ps aux | grep php-fpm
kill -QUIT 1219
/usr/local/php/sbin/php-fpm 

(参考:《php修改php.ini重启nginx php.ini设置不生效》)

⑦ 重启 Nginx:

nginx -s reload

⑧ 测试 imagick 是否添加成功:

在 php 文件中输入:

<?php
phpinfo();

如果在 info 页有如下内容,则说明添加成功:

 (三)使用 imagick 减小上传图片的品质(80%)

工具类:

Imgicktool.class.php:

<?php
/*
 * 图片压缩类  重新封装了Imagick
 */

namespace Home\Common;

class Imgicktool{

    //Imagick对象实例
    public $obj = null;

    public function __construct()
    {
        //判断是否加载了该扩展
        if(!extension_loaded(‘Imagick‘))
        {
            return false;
        }
        $this->obj = new \Imagick();
    }
    /*
     * png2jpg转换图片格式
     *
     * @param string src_img 源图片路径
     * @param string dest_img 要生成的图片的路径
     * @return boolean 转换成共返回true  否则false
     */
    public function png2jpg($src_img,$dest_img)
    {
        if(!is_object($this->obj))
        {
            return false;
        }
        try
        {
            $this->obj->readImage($src_img);
            if($this->obj->writeImage($dest_img))
            {
                $this->destory();
                return $dest_img;
            }
            return false;
        }
        catch (ImagickException $e)
        {
            return false;
        }
    }

    /*
     * 去掉图片的profile信息
     *
     * @param string src_img 源图片路径
     * @return string src_img 图片名称 否则返回false
     */
    public function strip_profile($src_img,$dest_img = ‘‘)
    {
        if(!is_object($this->obj))
        {
            return false;
        }
        try
        {
            $dest_img = empty($dest_img) ? $src_img : $dest_img;
            $this->obj->readImage($src_img);
            $this->obj->stripImage ();
            if($this->obj->writeImage ($dest_img))
            {
                $this->destory();
                return $src_img;
            }
            return false;
        }
        catch (ImagickException $e)
        {
            return false;
        }
    }

    /*
     * 设置jpg图片质量
     *
     * @param string src_img 源图片路径
     * @param string dest_img 要生成的图片的路径
     * @return boolean 转换成共返回true  否则false
     */
    public function set_quality($src_img,$quality = 70,$dest_img = ‘‘)
    {
        if(!is_object($this->obj))
        {       echo ‘error1‘;
            return false;
        }
        try
        {
            $dest_img = empty($dest_img) ? $src_img : $dest_img;
            $this->obj->readImage($src_img);
            $this->obj->setImageCompression(Imagick::COMPRESSION_JPEG);
            $this->obj->setImageCompressionQuality($quality);
            if($this->obj->writeImage($dest_img))
            {
                $this->destory();
                return $dest_img;
            }
                        echo ‘error2‘;
            return false;
        }
        catch (ImagickException $e)
        {
                        echo ‘error3‘;
            return false;
        }
    }

    /*
     * 图片瘦身
     *
     * @param string src_img 源图片路径
     * @param int quality 设置图片压缩质量
     * @param string dest_img 要生成的图片的路径
     * @return boolean 转换成共返回true  否则false
     */
    public function slimming($src_img,$quality = 60,$dest_img = ‘‘)
    {
        if(!is_object($this->obj))
        {
            return false;
        }
        try
        {
            $dest_img = empty($dest_img) ? $src_img : $dest_img;
            $this->obj->readImage($src_img);
            $this->obj->setImageFormat(‘jpeg‘);
            $this->obj->setImageCompression(\Imagick::COMPRESSION_JPEG);
            //将图片的质量降低到原来的60%
            $quality = $this->obj->getImageCompressionQuality() * $quality / 100;
            $this->obj->setImageCompressionQuality($quality);
            $this->obj->stripImage();

            if($this->obj->writeImage($dest_img))
            {
                $this->destory();
                return $dest_img;
            }
            return false;
        }
        catch (ImagickException $e)
        {
            return false;
        }
    }

    /*
     * 生成缩略图
     *
     * @param string src_img 源图片路径
     * @param int quality 设置图片压缩质量
     * @param string dest_img 要生成的图片的路径
     * @return boolean 转换成共返回true  否则false
     */
    public function thump($src_img,$width = 250,$height = ‘‘)
    {
        if(!is_object($this->obj))
        {
            return false;
        }
        try
        {

            $file_info = pathinfo($src_img);
            //生成缩略图名称
            $file_name = substr($file_info[‘basename‘],0,strrpos($file_info[‘basename‘],‘.‘));
            $dest_img = $file_info[‘dirname‘] . ‘/‘ . $file_name . ‘_thump.‘ . $file_info[‘extension‘];
            $this->obj->readImage($src_img);
            //计算要获得缩略图的高度
            $img_width = $this->obj->getImageWidth();
            $img_height = $this->obj->getImageHeight();
            $dest_height = $img_height * ($width / $img_width);
            $this->obj->resizeImage($width, $dest_height, Imagick::FILTER_CATROM, 1, false);
            //生成图片
            if($this->obj->writeImage($dest_img))
            {
                $this->destory();
                return $dest_img;
            }
            return false;
        }
        catch (ImagickException $e)
        {
            return false;
        }
    }

    /*
     * 释放资源
     *
     */
    function destory()
    {
        if(is_object($this->obj))
        {
            $this->obj->clear();

            $this->obj->destroy();
        }
    }
}

测试文件 ImgController.class.php:

<?php
namespace Home\Controller;
use Home\Common\Imgicktool;
use Think\Controller;

class ImgController extends Controller{

    function info(){
        phpinfo();
    }

    function quality(){
        $srcFile = APP_PATH.‘/test/test.jpg‘;
        $destFile = APP_PATH.‘/test/test.jpg‘;
        $qua = new Imgicktool();
        $qua->slimming($srcFile, 80, $destFile);
    }        

}

参考:

1.《Ubuntu下安装ImageMagick和MagicWand For PHP

2.《undefined reference to `inflateReset2‘

3.《php修改php.ini重启nginx php.ini设置不生效

4.《Linux下php安装imagick

5.《安装imagick时Cannot locate header file MagickWand.h错误的解决

6.《TP3.2下面 new 不出来redis

时间: 2024-08-02 17:12:20

Ubuntu 安装 ImageMagic(6.9.1-6)及 PHP 的 imagick (3.0.1)扩展的相关文章

Ubuntu 安装 JDK 7 / JDK8 的两种方式

ubuntu 安装jdk 的两种方式: 1:通过ppa(源) 方式安装. 2:通过官网下载安装包安装. 这里推荐第1种,因为可以通过 apt-get upgrade 方式方便获得jdk的升级 使用ppa/源方式安装 1.添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 2.安装oracle-java-installer jdk7 sudo apt-get install oracle-java7-inst

ubuntu安装pip3

当初入门Linux 使用的是centos,那个时候是6.0版本,当然现在主流在使用的也是6.0系列的,现在都到6.7了,那个时候centos还是独立的,现在被redhat收购,本来一个红蓝就差不多,个人感觉除了yum源不一样,其他没有区别,更搞笑的是,redhat用了centos 的开放yum 源,升级后居然变成centos 了.因为yum用的太顺手了,所以开始挺排斥ubuntu的,觉得apt-get很不爽,不过最近学python,感觉用centos各种不爽,怪不得都说开发最好用ubuntu.所

ubuntu 安装 git & smartgit

1. 安装 git # sudo apt-get update# sudo apt-get install git ? 2. 配置 # git config --global user.name "Your Name" # git config --global user.email "[email protected]" ? 3. 查看配置 # gitconfig--list ? 4. ?安装 SmartGit # cd ~/Downloads # wget?ht

ubuntu 安装 amp 环境 和 svn 命令

我是使用的集成安装 sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server 回车,会让输入密码: 安装过程中会让输入mysql root管理员的密码 如图: 安装完成之后,在浏览器地址栏中输入 localhost 测试下,能看到 It Works ! 表示环境安装成功! ------------------------------ Ubuntu 安装 svn 客户端 终端操作命令: sudo apt-get

ubuntu 安装ffmpeg VLC

ffmpeg安装 1.下载ffmpeg源码 ffmpeg.org 2.解压 tar -jvxf ffmpeg-2.5.2.tar.bz2 3.进入目录 ./configure 1)解决 ffmpeg yasm not found, use --disable-yasm for a crippled build ref:blog.csdn.net/ranxiedao/article/details/16359183 A 如果是Windows系统, 从网上下载一个 yasm.exe 并安装在ming

Ubuntu安装Latex

Ubuntu安装Latex  http://www.oschina.net/question/12_63776 Ubuntu下Latex中文环境配置  http://vistb.net/2012/05/config-latex-ch-env-in-ubuntu/ Ubuntu中配置LaTeX中文的方法http://blog.csdn.net/yangzhuoluo/article/details/5697205

Ubuntu 安装java环境搭建

1.下载JDK 8从http://www.oracle.com/technetwork/java/javasebusiness/downloads/选择下载JDK的最新版本 JDK 8. 2.解压文件$ sudo mkdir /usr/lib/jvm$ sudo mv jdk-8u11-linux-x64.tar.gz /usr/lib/jvm/ $ cd /usr/lib/jvm/$ sudo tar -zxvf jdk-8u11-linux-x64.tar.gz$ rm ./jdk-8u11

Ubuntu安装教程--Win7系统中含100M保留分区

1.检查 Win7 保留分区 1)进入 Win7 打开库文件夹,在左侧栏找到"计算机",瞄准点右键选择"管理"菜单: 2)在出来的管理面板左边找到"磁盘管理",点击它,在右边出来各个分区: 注意:如果里面有一个100M的系统保留分区,则进行下面(3-6)的操作,否则请跳至第2步复制启动文件: 3)瞄准100M"系统保留"分区点右键,选择"更改驱动器号和路径"菜单: 4)在出来的面板中,点左下角的"

ubuntu 安装 apk

adb devices查看你的所有设备 然后adb -s 设备序列号 install XXX.apk -s 用来指定设备 ubuntu 安装 apk,布布扣,bubuko.com