搜索
首页后端开发php教程php实现的支持imagemagick及gd库两种处理的缩略图生成类,imagemagickgd_PHP教程

php实现的支持imagemagick及gd库两种处理的缩略图生成类,imagemagickgd

本文实例讲述了php实现的支持imagemagick及gd库两种处理的缩略图生成类及其用法实例,非常具有实用价值。分享给大家供大家参考。具体如下:

一、功能:

1.按比例缩小/放大
2.填充背景色
3.按区域裁剪
4.添加水印,包括水印的位置,透明度等

使用imagemagick/GD库实现,imagemagick地址:www.imagemagick.org
需要安装imagemagick,安装方法如下:http://www.bkjia.com/article/55528.htm

二、实现方法:

PicThumb.class.php类文件如下:

<&#63;php 
/** 缩略图生成类,支持imagemagick及gd库两种处理 
*  Date:  2013-07-15 
*  Author: fdipzone 
*  Ver:  1.2 
* 
*  Func: 
*  public set_config: 设置参数 
*  public create_thumb: 生成缩略图 
*  private fit: 缩略图片 
*  private crop: 裁剪图片 
*  private gd_fit: GD库缩略图片 
*  private gd_crop: GD库裁剪图片 
*  private get_size: 获取要转换的size 
*  private get_crop_offset: 获取裁图的偏移量 
*  private add_watermark: 添加水印 
*  private check_handler: 判断处理程序是否已安装 
*  private create_dirs: 创建目录 
*  private exists: 判断参数是否存在 
*  private to_log: 记录log 
*  private hex2rgb: hex颜色转rgb颜色 
*  private get_file_ext: 获取图片类型 
* 
*  ver:  1.1 增加GD库处理 
*  ver:  1.2 增加width,height错误参数处理 
*        增加当图片colorspace不为RGB时作转RGB处理 
*        修正使用crop保存为gif时出现透明无效区域问题,使用+repage参数,删除透明无效区域即可 
* 
*  tips:建议使用imagemagick 
*    GD库不支持透明度水印,如果必须使用透明水印,请将水印图片做成有透明度。 
*    GD库输出gif如加透明水印,会有问题。 
*/ 
 
class PicThumb{ // class start 
 
  private $_log = null;      // log file 
  private $_handler = null;    // 进行图片处理的程序,imagemagick/gd库 
  private $_type = 'fit';     // fit or crop 
  private $_source = null;     // 原图路径 
  private $_dest = null;      // 缩略图路径 
  private $_watermark = null;   // 水印图片 
  private $_opacity = 75;     // 水印圖片透明度,gd库不支持 
  private $_gravity = 'SouthEast'; // 水印摆放位置 NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast 
  private $_geometry = '+10+10';  // 水印定位,gd库不支持 
  private $_croppos = 'TL';    // 截图的位置 TL TM TR ML MM MR BL BM BR 
  private $_bgcolor = null;    // 填充的背景色 
  private $_quality = 90;     // 生成的图片质量 
  private $_width = null;     // 指定区域宽度 
  private $_height = null;     // 指定区域高度 
 
  // 初始化 
  public function __construct($logfile=''){ 
    if($logfile!=''){ 
      $this->_log = $logfile; 
    } 
  } 
 
  // 设置参数 
  public function set_config($param=array()){ 
    $this->_handler = $this->exists($param, 'handler')&#63; strtolower($param['handler']) : null; 
    $this->_type = $this->exists($param, 'type')&#63; strtolower($param['type']) : 'fit'; 
    $this->_watermark = $this->exists($param, 'watermark')&#63; $param['watermark'] : null; 
    $this->_opacity = $this->exists($param, 'opacity')&#63; $param['opacity'] : 75; 
    $this->_gravity = $this->exists($param, 'gravity')&#63; $param['gravity'] : 'SouthEast'; 
    $this->_geometry = $this->exists($param, 'geometry')&#63; $param['geometry'] : '+10+10'; 
    $this->_croppos = $this->exists($param, 'croppos')&#63; $param['croppos'] : 'TL'; 
    $this->_bgcolor = $this->exists($param, 'bgcolor')&#63; $param['bgcolor'] : null; 
    $this->_quality = $this->exists($param, 'quality')&#63; $param['quality'] : 90; 
    $this->_width = $this->exists($param, 'width')&#63; $param['width'] : null; 
    $this->_height = $this->exists($param, 'height')&#63; $param['height'] : null; 
  } 
 
  /** 创建缩略图 
  * @param String $source 原图 
  * @param String $dest  目标图 
  * @return boolean 
  */ 
  public function create_thumb($source, $dest){ 
    // 检查使用的handler是否已安装 
    if(!$this->check_handler()){ 
      $this->to_log('handler not installed'); 
      return false; 
    } 
    // 判断区域宽高是否正确 
    if(!is_numeric($this->_width) || !is_numeric($this->_height) || $this->_width<=0 || $this->_height<=0){ 
      $this->to_log('width or height invalid'); 
      return false; 
    } 
 
    // 判断源文件是否存在 
    if(!file_exists($source)){ 
      $this->to_log($source.' not exists'); 
      return false; 
    } 
 
    // 创建目标文件路径 
    if(!$this->create_dirs($dest)){ 
      $this->to_log(dirname($dest).' create fail'); 
      return false; 
    } 
 
    $this->_source = $source;  // 源文件 
    $this->_dest = $dest;    // 目标文件 
 
    // 处理图片 
    switch($this->_type){ 
      case 'fit': 
        if($this->_handler=='imagemagick'){ 
          return $this->fit(); 
        }else{ 
          return $this->gd_fit(); 
        } 
        break; 
 
      case 'crop': 
        if($this->_handler=='imagemagick'){ 
          return $this->crop(); 
        }else{ 
          return $this->gd_crop(); 
        } 
        break; 
 
      default: 
        $this->to_log($this->_type.' not fit and crop'); 
        return false; 
    } 
  } 
 
  /** 按比例压缩或拉伸图片 
  * @return boolean 
  */ 
  private function fit(){ 
 
    // 判断是否填充背景 
    $bgcolor = ($this->_bgcolor!=null)&#63;  
    sprintf(" -background '%s' -gravity center -extent '%sx%s' ", $this->_bgcolor, $this->_width, $this->_height) : ""; 
 
    // 判断是否要转为RGB 
    $source_info = getimagesize($this->_source); 
    $colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)&#63; ' -colorspace RGB ' : ''; 
 
    // 命令行 
    $cmd = sprintf("convert -resize '%sx%s' '%s' %s -quality %s %s '%s'", $this->_width, $this->_height, $this->_source, $bgcolor, $this->_quality, $colorspace, $this->_dest); 
 
    // 记录执行的命令 
    $this->to_log($cmd); 
 
    // 执行命令 
    exec($cmd); 
 
    // 添加水印 
    $this->add_watermark($this->_dest); 
 
    return is_file($this->_dest)&#63; true : false; 
  } 
 
  /** 裁剪图片 
  * @return boolean 
  */ 
  private function crop(){ 
    // 获取生成的图片尺寸 
    list($pic_w, $pic_h) = $this->get_size(); 
 
    // 获取截图的偏移量 
    list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h); 
 
    // 判断是否要转为RGB 
    $source_info = getimagesize($this->_source); 
    $colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)&#63; ' -colorspace RGB ' : ''; 
 
    // 命令行 
    $cmd = sprintf("convert -resize '%sx%s' '%s' -quality %s %s -crop %sx%s+%s+%s +repage '%s'", $pic_w, $pic_h, $this->_source, $this->_quality, $colorspace, $this->_width, $this->_height, $offset_w, $offset_h, $this->_dest); 
 
    // 记录执行的命令 
    $this->to_log($cmd); 
 
    // 执行命令 
    exec($cmd); 
 
    // 添加水印 
    $this->add_watermark($this->_dest); 
 
    return is_file($this->_dest)&#63; true : false; 
  } 
 
  /** GD库按比例压缩或拉伸图片 
  * @return boolean 
  */ 
  private function gd_fit(){ 
    // 获取生成的图片尺寸 
    list($pic_w, $pic_h) = $this->get_size(); 
 
    list($owidth, $oheight, $otype) = getimagesize($this->_source); 
 
    switch($otype){ 
      case 1: $source_img = imagecreatefromgif($this->_source); break; 
      case 2: $source_img = imagecreatefromjpeg($this->_source); break; 
      case 3: $source_img = imagecreatefrompng($this->_source); break; 
      default: return false; 
    } 
 
    // 按比例缩略/拉伸图片 
    $new_img = imagecreatetruecolor($pic_w, $pic_h); 
    imagecopyresampled($new_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight); 
 
    // 判断是否填充背景 
    if($this->_bgcolor!=null){ 
      $bg_img = imagecreatetruecolor($this->_width, $this->_height); 
      $rgb = $this->hex2rgb($this->_bgcolor); 
      $bgcolor =imagecolorallocate($bg_img, $rgb['r'], $rgb['g'], $rgb['b']); 
      imagefill($bg_img, 0, 0, $bgcolor); 
      imagecopy($bg_img, $new_img, (int)(($this->_width-$pic_w)/2), (int)(($this->_height-$pic_h)/2), 0, 0, $pic_w, $pic_h); 
      $new_img = $bg_img; 
    } 
 
    // 获取目标图片的类型 
    $dest_ext = $this->get_file_ext($this->_dest); 
 
    // 生成图片 
    switch($dest_ext){ 
      case 1: imagegif($new_img, $this->_dest, $this->_quality); break; 
      case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break; 
      case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break; 
    } 
 
    if(isset($source_img)){ 
      imagedestroy($source_img); 
    } 
 
    if(isset($new_img)){ 
      imagedestroy($new_img); 
    } 
 
    // 添加水印 
    $this->add_watermark($this->_dest); 
 
    return is_file($this->_dest)&#63; true : false; 
  } 
 
  /** GD库裁剪图片 
  * @return boolean 
  */ 
  private function gd_crop(){ 
 
    // 获取生成的图片尺寸 
    list($pic_w, $pic_h) = $this->get_size(); 
 
    // 获取截图的偏移量 
    list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h); 
 
    list($owidth, $oheight, $otype) = getimagesize($this->_source); 
 
    switch($otype){ 
      case 1: $source_img = imagecreatefromgif($this->_source); break; 
      case 2: $source_img = imagecreatefromjpeg($this->_source); break; 
      case 3: $source_img = imagecreatefrompng($this->_source); break; 
      default: return false; 
    } 
 
    // 按比例缩略/拉伸图片 
    $tmp_img = imagecreatetruecolor($pic_w, $pic_h); 
    imagecopyresampled($tmp_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight); 
 
    // 裁剪图片 
    $new_img = imagecreatetruecolor($this->_width, $this->_height); 
    imagecopyresampled($new_img, $tmp_img, 0, 0, $offset_w, $offset_h, $this->_width, $this->_height, $this->_width, $this->_height); 
 
    // 获取目标图片的类型 
    $dest_ext = $this->get_file_ext($this->_dest); 
 
    // 生成图片 
    switch($dest_ext){ 
      case 1: imagegif($new_img, $this->_dest, $this->_quality); break; 
      case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break; 
      case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break; 
    } 
 
    if(isset($source_img)){ 
      imagedestroy($source_img); 
    } 
 
    if(isset($tmp_img)){ 
      imagedestroy($tmp_img); 
    } 
 
    if(isset($new_img)){ 
      imagedestroy($new_img); 
    } 
 
    // 添加水印 
    $this->add_watermark($this->_dest); 
 
    return is_file($this->_dest)&#63; true : false; 
  } 
 
  /** 获取目标图生成的size 
  * @return Array $width, $height 
  */ 
  private function get_size(){ 
    list($owidth, $oheight) = getimagesize($this->_source); 
    $width = (int)($this->_width); 
    $height = (int)($this->_height); 
     
    switch($this->_type){ 
      case 'fit': 
        $pic_w = $width; 
        $pic_h = (int)($pic_w*$oheight/$owidth); 
        if($pic_h>$height){ 
          $pic_h = $height; 
          $pic_w = (int)($pic_h*$owidth/$oheight); 
        } 
        break; 
      case 'crop': 
        if($owidth>$oheight){ 
          $pic_h = $height; 
          $pic_w = (int)($pic_h*$owidth/$oheight); 
        }else{ 
          $pic_w = $width; 
          $pic_h = (int)($pic_w*$oheight/$owidth); 
        } 
        break; 
    } 
    return array($pic_w, $pic_h); 
  } 
 
  /** 获取截图的偏移量 
  * @param int $pic_w 图宽度 
  * @param int $pic_h 图高度 
  * @return Array $offset_w, $offset_h 
  */ 
  private function get_crop_offset($pic_w, $pic_h){ 
    $offset_w = 0; 
    $offset_h = 0; 
     
    switch(strtoupper($this->_croppos)){ 
      case 'TL': 
        $offset_w = 0; 
        $offset_h = 0; 
        break; 
 
      case 'TM': 
        $offset_w = (int)(($pic_w-$this->_width)/2); 
        $offset_h = 0; 
        break; 
 
      case 'TR': 
        $offset_w = (int)($pic_w-$this->_width); 
        $offset_h = 0; 
        break; 
 
      case 'ML': 
        $offset_w = 0; 
        $offset_h = (int)(($pic_h-$this->_height)/2); 
        break; 
 
      case 'MM': 
        $offset_w = (int)(($pic_w-$this->_width)/2); 
        $offset_h = (int)(($pic_h-$this->_height)/2); 
        break; 
 
      case 'MR': 
        $offset_w = (int)($pic_w-$this->_width); 
        $offset_h = (int)(($pic_h-$this->_height)/2); 
        break; 
 
      case 'BL': 
        $offset_w = 0; 
        $offset_h = (int)($pic_h-$this->_height); 
        break; 
 
      case 'BM': 
        $offset_w = (int)(($pic_w-$this->_width)/2); 
        $offset_h = (int)($pic_h-$this->_height); 
        break; 
 
      case 'BR': 
        $offset_w = (int)($pic_w-$this->_width); 
        $offset_h = (int)($pic_h-$this->_height); 
        break; 
    } 
    return array($offset_w, $offset_h); 
  } 
 
  /** 添加水印 
  * @param String $dest 图片路径 
  */ 
  private function add_watermark($dest){ 
    if($this->_watermark!=null && file_exists($this->_watermark) && file_exists($dest)){ 
      list($owidth, $oheight, $otype) = getimagesize($dest); 
      list($w, $h, $wtype) = getimagesize($this->_watermark); 
 
      // 水印图比原图要小才加水印 
      if($w<=$owidth && $h<=$oheight){ 
 
        if($this->_handler=='imagemagick'){ // imagemagick 添加水印 
 
          $cmd = sprintf("composite -gravity %s -geometry %s -dissolve %s '%s' %s %s", $this->_gravity, $this->_geometry, $this->_opacity, $this->_watermark, $dest, $dest); 
 
          $this->to_log($cmd); 
 
          exec($cmd); 
 
        }else{ // gd 添加水印 
 
          switch($wtype){ 
            case 1: $water_img = imagecreatefromgif($this->_watermark); break; 
            case 2: $water_img = imagecreatefromjpeg($this->_watermark); break; 
            case 3: $water_img = imagecreatefrompng($this->_watermark); break; 
            default: return false; 
          } 
 
          switch($otype){ 
            case 1: $dest_img = imagecreatefromgif($dest); break; 
            case 2: $dest_img = imagecreatefromjpeg($dest); break; 
            case 3: $dest_img = imagecreatefrompng($dest); break; 
            default: return false; 
          } 
 
          // 水印位置 
          switch(strtolower($this->_gravity)){ 
            case 'northwest': 
              $posX = 0; 
              $posY = 0; 
              break; 
            case 'north': 
              $posX = ($owidth - $w) / 2; 
              $posY = 0; 
              break; 
            case 'northeast': 
              $posX = $owidth - $w; 
              $posY = 0; 
              break; 
            case 'west': 
              $posX = 0; 
              $posY = ($oheight - $h) / 2; 
              break; 
            case 'center': 
              $posX = ($owidth - $w) / 2; 
              $posY = ($oheight - $h) / 2; 
              break; 
            case 'east': 
              $posX = $owidth - $w; 
              $posY = ($oheight - $h) / 2; 
              break; 
            case 'southwest': 
              $posX = 0; 
              $posY = $oheight - $h; 
              break; 
            case 'south': 
              $posX = ($owidth - $w) / 2; 
              $posY = $oheight - $h; 
              break; 
            case 'southeast': 
              $posX = $owidth - $w; 
              $posY = $oheight - $h; 
              break; 
          } 
 
          imagealphablending($dest_img, true); 
          imagecopy($dest_img, $water_img, $posX, $posY, 0, 0, $w, $h); 
 
          switch($otype){ 
            case 1:imagegif($dest_img, $dest, $this->_quality); break; 
            case 2:imagejpeg($dest_img, $dest, $this->_quality); break; 
            case 3:imagepng($dest_img, $dest, (int)(($this->_quality-1)/10)); break; 
          } 
 
          if(isset($water_img)){ 
            imagedestroy($water_img); 
          } 
 
          if(isset($dest_img)){ 
            imagedestroy($dest_img); 
          } 
        } 
      } 
    } 
  } 
 
  /** 判断处理程序是否已安装 
  * @return boolean 
  */ 
  private function check_handler(){ 
 
    $handler = $this->_handler; 
 
    if(!in_array($handler, array('imagemagick', 'gd', null))){ 
      return false; 
    } 
 
    // 检查是否有安装imagemagick 
    $imagemagick_installed = strstr(shell_exec('convert -version'),'Version: ImageMagick')!=''&#63; true : false; 
 
    // 检查是否有安装gd库 
    $gd_installed = function_exists('gd_info')&#63; true : false; 
 
    switch($handler){ 
      case 'imagemagick': 
        return $imagemagick_installed; 
        break; 
 
      case 'gd': 
        return $gd_installed; 
        break; 
 
      case null: 
        if($imagemagick_installed){ 
          $this->_handler = 'imagemagick'; 
          return true; 
        } 
 
        if($gd_installed){ 
          $this->_handler = 'gd'; 
          return true; 
        } 
        break; 
    } 
    return false; 
  } 
 
  /** 创建图片目录 
  * @param String $path 
  * @return boolean 
  */ 
  private function create_dirs($dest){ 
    if(!is_dir(dirname($dest))){ 
      return mkdir(dirname($dest), 0777, true); 
    } 
    return true; 
  } 
 
  /** 判断参数是否存在 
  * @param Array  $obj 数组对象 
  * @param String $key 要查找的key 
  * @return boolean 
  */ 
  private function exists($obj,$key=''){ 
    if($key==''){ 
      return isset($obj) && !empty($obj); 
    }else{ 
      $keys = explode('.',$key); 
      for($i=0,$max=count($keys); $i<$max; $i++){ 
        if(isset($obj[$keys[$i]])){ 
          $obj = $obj[$keys[$i]]; 
        }else{ 
          return false; 
        } 
      } 
      return isset($obj) && !empty($obj); 
    } 
  } 
 
  /** 记录log 
  * @param String $msg 要记录的log讯息 
  */ 
  private function to_log($msg){ 
    if($this->_log){ 
      $msg = '['.date('Y-m-d H:i:s').']'.$msg."\r\n"; 
      file_put_contents($this->_log, $msg, FILE_APPEND); 
    } 
  } 
 
  /** hex颜色转rgb颜色 
  * @param String $color hex颜色 
  * @return Array 
  */ 
  private function hex2rgb($hexcolor){ 
    $color = str_replace('#', '', $hexcolor); 
    if (strlen($color) > 3) { 
      $rgb = array( 
        'r' => hexdec(substr($color, 0, 2)), 
        'g' => hexdec(substr($color, 2, 2)), 
        'b' => hexdec(substr($color, 4, 2)) 
      ); 
    } else { 
      $r = substr($color, 0, 1) . substr($color, 0, 1); 
      $g = substr($color, 1, 1) . substr($color, 1, 1); 
      $b = substr($color, 2, 1) . substr($color, 2, 1); 
      $rgb = array( 
        'r' => hexdec($r), 
        'g' => hexdec($g), 
        'b' => hexdec($b) 
      ); 
    } 
    return $rgb; 
  } 
 
  /** 获取图片类型 
  * @param String $file 图片路径 
  * @return int 
  */ 
  private function get_file_ext($file){ 
    $filename = basename($file); 
    list($name, $ext)= explode('.', $filename); 
 
    $ext_type = 0; 
 
    switch(strtolower($ext)){ 
      case 'jpg': 
      case 'jpeg': 
        $ext_type = 2; 
        break; 
      case 'gif': 
        $ext_type = 1; 
        break; 
      case 'png': 
        $ext_type = 3; 
        break; 
    } 
    return $ext_type; 
  } 
 
} // class end 
&#63;>

demo示例代码如下:

<&#63;php 
define('ROOT', dirname(__FILE__)); 
 
require(ROOT."/PicThumb.class.php"); 
 
$logfile = ROOT.'/PicThumb.log'; 
$source1 = ROOT.'/pic/source.jpg'; 
$dest1 = ROOT.'/pic/1.jpg'; 
$dest2 = ROOT.'/pic/2.gif'; 
$dest3 = ROOT.'/pic/3.png'; 
 
$source2 = ROOT.'/pic/source_cmyk.jpg'; 
$dest4 = ROOT.'/pic/4.jpg'; 
$dest5 = ROOT.'/pic/5.gif'; 
$dest6 = ROOT.'/pic/6.png'; 
 
$watermark = ROOT.'/pic/watermark.png'; 
 
// 按比例生成缩略图 
$param = array( 
  'type' => 'fit', 
  'width' => 100, 
  'height' => 100, 
); 
 
$obj = new PicThumb($logfile); 
$obj->set_config($param); 
$flag = $obj->create_thumb($source1, $dest1); 
 
if($flag){ 
  echo '<img  src="/static/imghwm/default1.png"  data-src="pic/'.basename($dest1).'"  class="lazy".basename($dest1).'" alt="php实现的支持imagemagick及gd库两种处理的缩略图生成类,imagemagickgd_PHP教程" >'; 
}else{ 
  echo 'create thumb fail'; 
} 
 
// 按比例生成缩略图,不足部分用#FF0000填充 
$param = array( 
  'type' => 'fit', 
  'width' => 100, 
  'height' => 100, 
  'bgcolor' => '#FFFF00' 
); 
 
$obj = new PicThumb($logfile); 
$obj->set_config($param); 
$flag = $obj->create_thumb($source1, $dest2); 
 
if($flag){ 
  echo '<img  src="/static/imghwm/default1.png"  data-src="pic/'.basename($dest2).'"  class="lazy".basename($dest2).'" alt="php实现的支持imagemagick及gd库两种处理的缩略图生成类,imagemagickgd_PHP教程" >'; 
}else{ 
  echo 'create thumb fail'; 
} 
 
// 裁剪250x250的缩略图,裁剪位置是底部中间,水印位置西南,透明度50 
$param = array( 
  'type' => 'crop', 
  'croppos' => 'BM', 
  'width' => 250, 
  'height' => 250, 
  'watermark' => $watermark, 
  'opacity' => 50, 
  'gravity' => 'SouthWest' 
); 
 
$obj = new PicThumb($logfile); 
$obj->set_config($param); 
$flag = $obj->create_thumb($source1, $dest3); 
 
if($flag){ 
  echo '<img  src="/static/imghwm/default1.png"  data-src="pic/'.basename($dest3).'"  class="lazy".basename($dest3).'" alt="php实现的支持imagemagick及gd库两种处理的缩略图生成类,imagemagickgd_PHP教程" >'; 
}else{ 
  echo 'create thumb fail'; 
} 
 
// 按比例生成缩略图 CMYK格式 
$param = array( 
  'type' => 'fit', 
  'width' => 100, 
  'height' => 100, 
); 
 
$obj = new PicThumb($logfile); 
$obj->set_config($param); 
$flag = $obj->create_thumb($source2, $dest4); 
 
if($flag){ 
  echo '<img  src="/static/imghwm/default1.png"  data-src="pic/'.basename($dest4).'"  class="lazy".basename($dest4).'" alt="php实现的支持imagemagick及gd库两种处理的缩略图生成类,imagemagickgd_PHP教程" >'; 
}else{ 
  echo 'create thumb fail'; 
} 
 
// 按比例生成缩略图,不足部分用#FF0000填充 CMYK格式 
$param = array( 
  'type' => 'fit', 
  'width' => 100, 
  'height' => 100, 
  'bgcolor' => '#FFFF00' 
); 
 
$obj = new PicThumb($logfile); 
$obj->set_config($param); 
$flag = $obj->create_thumb($source2, $dest5); 
 
if($flag){ 
  echo '<img  src="/static/imghwm/default1.png"  data-src="pic/'.basename($dest5).'"  class="lazy".basename($dest5).'" alt="php实现的支持imagemagick及gd库两种处理的缩略图生成类,imagemagickgd_PHP教程" >'; 
}else{ 
  echo 'create thumb fail'; 
} 
 
// 裁剪250x250的缩略图,裁剪位置是底部中间,水印位置西南,透明度50 CMYK格式 
$param = array( 
  'type' => 'crop', 
  'croppos' => 'BM', 
  'width' => 250, 
  'height' => 250, 
  'watermark' => $watermark, 
  'opacity' => 50, 
  'gravity' => 'SouthWest' 
); 
 
$obj = new PicThumb($logfile); 
$obj->set_config($param); 
$flag = $obj->create_thumb($source2, $dest6); 
 
if($flag){ 
  echo '<img  src="/static/imghwm/default1.png"  data-src="pic/'.basename($dest6).'"  class="lazy".basename($dest6).'" alt="php实现的支持imagemagick及gd库两种处理的缩略图生成类,imagemagickgd_PHP教程" >'; 
}else{ 
  echo 'create thumb fail'; 
} 
&#63;>

本文完整实例代码点击此处本站下载。

希望本文所述对大家的PHP程序设计有所帮助

PHP扩展之ImageMagick函数执行问题

试一下这个类吧 phpthumb
配置phpthumb让他使用ImageMagick来生成缩略图
phpthumb是一个功能非常强大的缩略图生成类,支持jpg,bmp,gif,png等格式图片生成。无论您是PHP项目还是其它项目只要你能有一台PHP的主机就可以使用phpthumb带给您编程上的方便。因为他还支持远程图片生成。
phpthumb可以配合ImageMagick来使用,这样就不会再受到PHP内存大小的限制了,还可以生成gif动画。下面就讲如何让phpthumb用ImageMagick来生成生成图片。

无论win平台还是linux平台phpthumb默认是开启ImageMagick的,只不过配置错误和程序有一点小问题。首先就是安装ImageMagick这个程序。不用安装php_imagick这个PHP扩展。phpthumb使用的是命令行操作的,所以php应该可以运行一个进程才可以,不然请选择另外的主机。安装好后请按下面的步骤进行phpthumb的修改。
1.打开phpThumb.config.php查找
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
// Windows: set absolute pathname
$PHPTHUMB_CONFIG['imagemagick_path'] = 'C:/Program Files/ImageMagick-6.6.0-Q16/convert.exe';
} else {
// *nix: set absolute pathname to "convert", or leave as null if "convert" is in the path (location detected with `which`)
//$PHPTHUMB_CONFIG['imagemagick_path'] = '/usr/local/bin/convert';
$PHPTHUMB_CONFIG['imagemagick_path'] = null;
}
将上面的$PHPTHUMB_CONFIG['imagemagick_path'] 设成你安装的正确路径。上面共计有两个地方,第一个是window平台。第二个是LINUX,UNIX平台的。请根据您的系统选择设定。上面红色部分为ImageMagick路径,替换时注意目录分隔符是/不是\。
2.打开phpThumb.class.php
查找 function ImageMagickVersion($returnRAW=false) {在这附近
有if (eregi('^Version: [^0-9]*([ 0-9\\.\\:Q/]+) (http|file)\:', $versionstring[1], $matches)) {
替换成if (eregi('^Version: (.+) (http|file)\:', $versionstring[1], $matches)) {

OK全部设定已经完成您的phpthumb已经自动使用ImageMagick生成图......余下全文>>
 

PHP怎把上传的照片生成高质量的缩略图?

ImageMagick没用过,一般直接用内置的GD库,没有发现你说的这么严重的失真问题。
利用GD库创建缩略图的大致思路如下:
依据设定的尺寸创建真彩色画布$im=createtruecolor(120,90);
读取原始文件尺寸,按照原始尺寸的宽度和高度比例,计算出缩略图的大小(可能与给定的尺寸有一定的偏差)
将原始图像拷贝并缩放到创建的真彩色缩略图画布上。
输出缩略图文件。

可能就是因为利用的是这个真彩色,缩略图效果还凑合,也不是说绝对不失真的。

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/882894.htmlTechArticlephp实现的支持imagemagick及gd库两种处理的缩略图生成类,imagemagickgd 本文实例讲述了php实现的支持imagemagick及gd库两种处理的缩略图生成类及其...
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器