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

一、功能:

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

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

二、实现方法:

PicThumb.class.php类文件如下:

<" -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)"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)"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)"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')!=''"\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 
"htmlcode">
<"/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="/UploadFiles/2021-04-02/'.basename($dest1).'">本站下载。

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

标签:
php,支持,imagemagick,gd库,缩略图,生成,类

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com