[PHP] 图片缩放水印类 →→→→→进入此内容的聊天室

来自 , 2019-07-14, 写在 PHP, 查看 110 次.
URL http://www.code666.cn/view/058d6f2f
  1. /**
  2. * 图片缩放水印类
  3. *
  4. */
  5. class cls_photo
  6. {
  7.         protected $waterrate = 0.2; //水印图标在图片上的比例
  8.         protected $width = 300; //缩略图默认宽度
  9.         protected $height = 200; //缩略图默认高度
  10.         protected $padding = 5;  //水印图到边的距离
  11.         protected $water_mark = "./water.png";
  12.         protected $water_mark_pos = 5;//水印图片位置(1=左上角,2=右上角,3=左下角,4=右下角,5中央)
  13.         protected $watermode = 0;// 0缩略图时不打水印 1缩略图时打水印
  14.         protected $magick_handle;//图片操作句柄
  15.         protected $format = array ( 'jpg','gif','png','jpeg' ); // 图片文件格式限定
  16.         protected $smallpic_mode = 2;//默认模式 0为不生成缩略图, 1为裁切缩放 ,2为比例缩放 3为缩放填充模式
  17.  
  18.         /**
  19.          * 设置图片类参数
  20.          *
  21.          * @param $arg 图片参数 多次可放入数组里 如下
  22.          * @param $protected 参数值
  23.          * array(
  24.          *      'waterrate'=>0.2,
  25.          *      'water_mark'=>'./water.png',
  26.          *      'water_mark_pos'=>4,
  27.          *      'smallpic_mode'=>1
  28.          *      );
  29.          * @return ture/false
  30.          */
  31.         public function set_args ( $arg,$val="" )
  32.         {
  33.                 $params = array ( 'waterrate','water_mark','water_mark_pos','smallpic_mode','watermode','width','height' );
  34.                 if ( is_array ( $arg ) )
  35.                 {
  36.                         foreach ( $arg as $k =>$v )
  37.                         {
  38.                                 if ( in_array ( $k,$params ) )
  39.                                 {
  40.                                         $this->$k = $v;
  41.                                 }
  42.                         }
  43.                 }
  44.                 else
  45.                 {
  46.                         if ( empty ( $val ) )
  47.                         {
  48.                                 return false;
  49.                         }
  50.                         else
  51.                         {
  52.                                 if ( in_array ( $arg,$params ) )
  53.                                 {
  54.                                         $this->$arg = $val;
  55.                                 }
  56.                         }
  57.                 }
  58.                 return true;
  59.         }
  60.  
  61.         /**
  62.          * 图片缩放
  63.          *
  64.          * @param $src_file 源文件路径
  65.          * @param $dst_file 目标文件路径
  66.          * @return 缩略图片路径/false
  67.          */
  68.         public function scale ( $src_file,$dst_file="" )
  69.         {
  70.                 $dst_width  = $this->width;
  71.                 $dst_height = $this->height;
  72.                 $mode       = $this->smallpic_mode;
  73.                 $magic_water_handle = NewMagickWand();
  74.                 if ( !MagickReadImage ( $magic_water_handle, $src_file ) ) return false;
  75.  
  76.                 //类型
  77.                 $srcext = strtolower ( MagickGetImageFormat ( $magic_water_handle ) );
  78.                 if ( $srcext=='bmp' )
  79.                 {
  80.                         $srcext = 'jpeg';
  81.                 }
  82.                 if ( !in_array ( $srcext,$this->format ) ) return false;
  83.                 //尺寸
  84.                 $src_width = MagickGetImageWidth ( $magic_water_handle );
  85.                 $src_height = MagickGetImageHeight ( $magic_water_handle );
  86.  
  87.                 //裁切缩放模式
  88.                 if ( $mode == 1 )
  89.                 {
  90.                         $pos_x=$pos_y = 0;//裁切临时位置
  91.                         $src_widthc = $src_width;//裁切临时宽度
  92.                         $src_heightc = $src_height;//裁切临时高度
  93.                         if ( $src_width/$src_height>$dst_width/$dst_height )
  94.                         {
  95.                                 $src_widthc = $src_height*$dst_width/$dst_height;
  96.                                 $pos_x = ( $src_width-$src_widthc ) /2;
  97.  
  98.                         }
  99.                         else
  100.                         {
  101.                                 $src_heightc = $src_width*$dst_height/$dst_width;
  102.                                 $pos_y = ( $src_height-$src_heightc ) /2;
  103.                         }
  104.                         MagickCropImage ( $magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y );//裁切
  105.                         //因为MagickCropImage函数后,Gif 图像改,但画布不变
  106.                         $this->magick_handle = NewMagickWand();
  107.                         MagickNewImage ( $this->magick_handle,$src_widthc,$src_heightc,'#ffffff' );
  108.                         MagickSetFormat ( $this->magick_handle,$srcext );
  109.                         MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0 );
  110.                         //缩放
  111.                         MagickScaleImage ( $this->magick_handle, $dst_width, $dst_height );
  112.  
  113.                 }
  114.                 //比例缩放模式
  115.                 if ( $mode == 2 )
  116.                 {
  117.                         if ( $src_width/$src_height>$dst_width/$dst_height )
  118.                         {
  119.                                 $dst_height=$dst_width*$src_height/$src_width;
  120.                         }
  121.                         else
  122.                         {
  123.                                 $dst_width=$dst_height*$src_width/$src_height;
  124.                         }
  125.                         $this->magick_handle=$magic_water_handle;//替换
  126.                         MagickScaleImage ( $this->magick_handle, $dst_width, $dst_height );//缩放
  127.                 }
  128.                 //缩放填充模式
  129.                 if ( $mode == 3 )
  130.                 {
  131.                         if ( $src_width/$src_height>$dst_width/$dst_height )
  132.                         {
  133.                                 $dst_heightc=$dst_width*$src_height/$src_width;
  134.                                 $dst_widthc=$dst_width;
  135.                         }
  136.                         else
  137.                         {
  138.                                 $dst_widthc=$dst_height*$src_width/$src_height;
  139.                                 $dst_heightc=$dst_height;
  140.                         }
  141.                         MagickScaleImage ( $magic_water_handle, $dst_widthc, $dst_heightc );//缩放
  142.                         $this->magick_handle = NewMagickWand();
  143.                         MagickNewImage ( $this->magick_handle,$dst_width,$dst_height,$this->smallpic_bgcolor );
  144.                         MagickSetFormat ( $this->magick_handle,$srcext );
  145.                         MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp, ( $dst_width-$dst_widthc ) /2, ( $dst_height-$dst_heightc ) /2 );
  146.                 }
  147.                 //打水印
  148.                 if ( $this->watermode == 1 )
  149.                 {
  150.                         $this->set_mark();
  151.                 }
  152.                 if ( empty ( $dst_file ) )
  153.                 {
  154.                         //建立临时文件
  155.                         $dst_file = tempnam ( $_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG" );
  156.                 }
  157.                 MagickWriteImage ( $this->magick_handle, $dst_file );
  158.                 return $dst_file;
  159.         }
  160.  
  161.         /**
  162.          * 打水印
  163.          *
  164.          * @param $src_file 要打水印的图片路径
  165.          * @param $dst_file 生产水印的文件保存路径,为空则生产随机临时文件
  166.          * @return 水印文件路径/false
  167.          */
  168.         public function water_mark ( $src_file,$dst_file="" )
  169.         {
  170.                 $this->magick_handle = NewMagickWand();
  171.                 if ( !MagickReadImage ( $this->magick_handle, $src_file ) )
  172.                         return false;
  173.                 $this->set_mark();
  174.                 if ( empty ( $dst_file ) )
  175.                 {
  176.                         //建立临时文件
  177.                         $dst_file = tempnam ( $_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG" );
  178.                 }
  179.                 MagickWriteImage ( $this->magick_handle, $dst_file );
  180.                 return $dst_file;
  181.         }
  182.  
  183.         /**
  184.          * 对内接口
  185.          * 给图片打水印
  186.          *
  187.          */
  188.         protected  function set_mark()
  189.         {
  190.  
  191.                 //尺寸
  192.                 $dst_width = MagickGetImageWidth ( $this->magick_handle );
  193.                 $dst_height = MagickGetImageHeight ( $this->magick_handle );
  194.                 //处理水印图
  195.                 if ( $this->water_mark && is_file ( $this->water_mark ) )
  196.                 {
  197.                         $magic_water_handle = NewMagickWand();
  198.                         MagickRemoveImage ( $magic_water_handle );
  199.                         if ( MagickReadImage ( $magic_water_handle, $this->water_mark ) )
  200.                         {
  201.                                 MagickScaleImage ( $magic_water_handle, $dst_width*$this->waterrate, $dst_width*$this->waterrate*MagickGetImageHeight ( $magic_water_handle ) /MagickGetImageWidth ( $magic_water_handle ) );//缩放水印到图片的1/5
  202.                                 if ( $this->water_mark_pos == 1 )
  203.                                 {
  204.                                         $left = $this->padding;
  205.                                         $top = $this->padding;
  206.                                 }
  207.                                 elseif ( $this->water_mark_pos == 2 )
  208.                                 {
  209.                                         $left = $dst_width-$this->padding-MagickGetImageWidth ( $magic_water_handle );
  210.                                         $top = $this->padding;
  211.                                 }
  212.                                 elseif ( $this->water_mark_pos == 3 )
  213.                                 {
  214.                                         $left = $this->padding;
  215.                                         $top = $dst_height -$this->padding-MagickGetImageHeight ( $magic_water_handle );
  216.                                 }
  217.                                 elseif ( $this->water_mark_pos == 4 )
  218.                                 {
  219.                                         $left = $dst_width-$this->padding-MagickGetImageWidth ( $magic_water_handle );
  220.                                         $top =$dst_height -$this->padding-MagickGetImageHeight ( $magic_water_handle );
  221.                                 }
  222.                                 elseif ( $this->water_mark_pos == 5 )
  223.                                 {
  224.                                         $left = ( $dst_width-MagickGetImageWidth ( $magic_water_handle ) ) /2;
  225.                                         $top = ( $dst_height -MagickGetImageHeight ( $magic_water_handle ) ) /2;
  226.                                 }
  227.                                 MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$top );
  228.                         }
  229.                 }
  230.         }
  231. }

回复 "图片缩放水印类"

这儿你可以回复上面这条便签

captcha