PHP图片压缩类

  • 内容
  • 相关

首页服务器要安装 Imagick扩展

           //引入类
            import('imgick');

            $img = new Imgick_tool(); //实例化

            $img->slimming('图片路径','压缩比例','压缩完后的路径'); //压缩比例 1-100

压缩类:

<?php
/**
 * 图片压缩类库
 *
 */

class Imgick_tool{  
      
    //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))  
        {  
            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;  
            }  
            return false;  
        }  
        catch (ImagickException $e)  
        {  
            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();  
        }  
    }  
      
}

?>

压缩类下载地址:imgick.zip

本文标签:

版权声明:若无特殊注明,本文皆为《Leek》原创,转载请保留文章出处。

本文链接:PHP图片压缩类 - https://pjax.cc/90.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

未显示?请点击刷新

允许邮件通知