中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

PHP圖片處理類 (水印圖 縮略圖 等比例壓縮 裁剪壓縮)

2019-07-23    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
<?php
/**
 * 
 * 圖像處理類
 * @author FC_LAMP
 * @internal功能包含:水印,縮略圖
 */
class Img
{
 //圖片格式
 private $exts = array ('jpg', 'jpeg', 'gif', 'bmp', 'png' );

 /**
  * 
  * 
  * @throws Exception
  */
 public function __construct()
 {
  if (! function_exists ( 'gd_info' ))
  {
   throw new Exception ( '加載GD庫失!' );
  }
 }

 /**
  * 
  * 裁剪壓縮
  * @param $src_img 圖片
  * @param $save_img 生成后的圖片
  * @param $option 參數(shù)選項,包括: $maxwidth  寬  $maxheight 高
  * array('width'=>xx,'height'=>xxx)
  * @internal
  * 我們一般的壓縮圖片方法,在圖片過長或過寬時生成的圖片
  * 都會被“壓扁”,針對這個應(yīng)采用先裁剪后按比例壓縮的方法
  */
 public function thumb_img($src_img, $save_img = '', $option)
 {

  if (empty ( $option ['width'] ) or empty ( $option ['height'] ))
  {
   return array ('flag' => False, 'msg' => '原圖長度與寬度不能小于0' );
  }
  $org_ext = $this->is_img ( $src_img );
  if (! $org_ext ['flag'])
  {
   return $org_ext;
  }

  //如果有保存路徑,則確定路徑是否正確
  if (! empty ( $save_img ))
  {
   $f = $this->check_dir ( $save_img );
   if (! $f ['flag'])
   {
    return $f;
   }
  }

  //獲取出相應(yīng)的方法
  $org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );

  //獲取原大小
  $source = $org_funcs ['create_func'] ( $src_img );
  $src_w = imagesx ( $source );
  $src_h = imagesy ( $source );

  //調(diào)整原始圖像(保持圖片原形狀裁剪圖像)
  $dst_scale = $option ['height'] / $option ['width']; //目標(biāo)圖像長寬比
  $src_scale = $src_h / $src_w; // 原圖長寬比
  if ($src_scale >= $dst_scale)
  { // 過高
   $w = intval ( $src_w );
   $h = intval ( $dst_scale * $w );

   $x = 0;
   $y = ($src_h - $h) / 3;
  } else
  { // 過寬
   $h = intval ( $src_h );
   $w = intval ( $h / $dst_scale );

   $x = ($src_w - $w) / 2;
   $y = 0;
  }
  // 剪裁
  $croped = imagecreatetruecolor ( $w, $h );
  imagecopy ( $croped, $source, 0, 0, $x, $y, $src_w, $src_h );
  // 縮放
  $scale = $option ['width'] / $w;
  $target = imagecreatetruecolor ( $option ['width'], $option ['height'] );
  $final_w = intval ( $w * $scale );
  $final_h = intval ( $h * $scale );
  imagecopyresampled ( $target, $croped, 0, 0, 0, 0, $final_w, $final_h, $w, $h );
  imagedestroy ( $croped );

  //輸出(保存)圖片
  if (! empty ( $save_img ))
  {

   $org_funcs ['save_func'] ( $target, $save_img );
  } else
  {
   header ( $org_funcs ['header'] );
   $org_funcs ['save_func'] ( $target );
  }
  imagedestroy ( $target );
  return array ('flag' => True, 'msg' => '' );
 }

 /**
  * 
  * 等比例縮放圖像
  * @param $src_img 原圖片
  * @param $save_img 需要保存的地方
  * @param $option 參數(shù)設(shè)置 array('width'=>xx,'height'=>xxx)
  * 
  */
 function resize_image($src_img, $save_img = '', $option)
 {
  $org_ext = $this->is_img ( $src_img );
  if (! $org_ext ['flag'])
  {
   return $org_ext;
  }

  //如果有保存路徑,則確定路徑是否正確
  if (! empty ( $save_img ))
  {
   $f = $this->check_dir ( $save_img );
   if (! $f ['flag'])
   {
    return $f;
   }
  }

  //獲取出相應(yīng)的方法
  $org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );

  //獲取原大小
  $source = $org_funcs ['create_func'] ( $src_img );
  $src_w = imagesx ( $source );
  $src_h = imagesy ( $source );

  if (($option ['width'] && $src_w > $option ['width']) || ($option ['height'] && $src_h > $option ['height']))
  {
   if ($option ['width'] && $src_w > $option ['width'])
   {
    $widthratio = $option ['width'] / $src_w;
    $resizewidth_tag = true;
   }

   if ($option ['height'] && $src_h > $option ['height'])
   {
    $heightratio = $option ['height'] / $src_h;
    $resizeheight_tag = true;
   }

   if ($resizewidth_tag && $resizeheight_tag)
   {
    if ($widthratio < $heightratio)
     $ratio = $widthratio;
    else
     $ratio = $heightratio;
   }

   if ($resizewidth_tag && ! $resizeheight_tag)
    $ratio = $widthratio;
   if ($resizeheight_tag && ! $resizewidth_tag)
    $ratio = $heightratio;

   $newwidth = $src_w * $ratio;
   $newheight = $src_h * $ratio;

   if (function_exists ( "imagecopyresampled" ))
   {
    $newim = imagecreatetruecolor ( $newwidth, $newheight );
    imagecopyresampled ( $newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src_w, $src_h );
   } else
   {
    $newim = imagecreate ( $newwidth, $newheight );
    imagecopyresized ( $newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src_w, $src_h );
   }
  }
  //輸出(保存)圖片
  if (! empty ( $save_img ))
  {

   $org_funcs ['save_func'] ( $newim, $save_img );
  } else
  {
   header ( $org_funcs ['header'] );
   $org_funcs ['save_func'] ( $newim );
  }
  imagedestroy ( $newim );
  return array ('flag' => True, 'msg' => '' );
 }

 /**
  * 
  * 生成水印圖片
  * @param  $org_img 原圖像
  * @param  $mark_img 水印標(biāo)記圖像
  * @param  $save_img 當(dāng)其目錄不存在時,會試著創(chuàng)建目錄
  * @param array $option 為水印的一些基本設(shè)置包含:
  * x:水印的水平位置,默認為減去水印圖寬度后的值
  * y:水印的垂直位置,默認為減去水印圖高度后的值
  * alpha:alpha值(控制透明度),默認為50
  */
 public function water_mark($org_img, $mark_img, $save_img = '', $option = array())
 {
  //檢查圖片
  $org_ext = $this->is_img ( $org_img );
  if (! $org_ext ['flag'])
  {
   return $org_ext;
  }
  $mark_ext = $this->is_img ( $mark_img );
  if (! $mark_ext ['flag'])
  {
   return $mark_ext;
  }
  //如果有保存路徑,則確定路徑是否正確
  if (! empty ( $save_img ))
  {
   $f = $this->check_dir ( $save_img );
   if (! $f ['flag'])
   {
    return $f;
   }
  }

  //獲取相應(yīng)畫布
  $org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );
  $org_img_im = $org_funcs ['create_func'] ( $org_img );

  $mark_funcs = $this->get_img_funcs ( $mark_ext ['msg'] );
  $mark_img_im = $mark_funcs ['create_func'] ( $mark_img );

  //拷貝水印圖片坐標(biāo)
  $mark_img_im_x = 0;
  $mark_img_im_y = 0;
  //拷貝水印圖片高寬
  $mark_img_w = imagesx ( $mark_img_im );
  $mark_img_h = imagesy ( $mark_img_im );

  $org_img_w = imagesx ( $org_img_im );
  $org_img_h = imagesx ( $org_img_im );

  //合成生成點坐標(biāo)
  $x = $org_img_w - $mark_img_w;
  $org_img_im_x = isset ( $option ['x'] ) ? $option ['x'] : $x;
  $org_img_im_x = ($org_img_im_x > $org_img_w or $org_img_im_x < 0) ? $x : $org_img_im_x;
  $y = $org_img_h - $mark_img_h;
  $org_img_im_y = isset ( $option ['y'] ) ? $option ['y'] : $y;
  $org_img_im_y = ($org_img_im_y > $org_img_h or $org_img_im_y < 0) ? $y : $org_img_im_y;

  //alpha
  $alpha = isset ( $option ['alpha'] ) ? $option ['alpha'] : 50;
  $alpha = ($alpha > 100 or $alpha < 0) ? 50 : $alpha;

  //合并圖片
  imagecopymerge ( $org_img_im, $mark_img_im, $org_img_im_x, $org_img_im_y, $mark_img_im_x, $mark_img_im_y, $mark_img_w, $mark_img_h, $alpha );

  //輸出(保存)圖片
  if (! empty ( $save_img ))
  {

   $org_funcs ['save_func'] ( $org_img_im, $save_img );
  } else
  {
   header ( $org_funcs ['header'] );
   $org_funcs ['save_func'] ( $org_img_im );
  }
  //銷毀畫布
  imagedestroy ( $org_img_im );
  imagedestroy ( $mark_img_im );
  return array ('flag' => True, 'msg' => '' );

 }

 /**
  * 
  * 檢查圖片
  * @param unknown_type $img_path
  * @return array('flag'=>true/false,'msg'=>ext/錯誤信息) 
  */
 private function is_img($img_path)
 {
  if (! file_exists ( $img_path ))
  {
   return array ('flag' => False, 'msg' => "加載圖片 $img_path 失。" );
  }
  $ext = explode ( '.', $img_path );
  $ext = strtolower ( end ( $ext ) );
  if (! in_array ( $ext, $this->exts ))
  {
   return array ('flag' => False, 'msg' => "圖片 $img_path 格式不正確!" );
  }
  return array ('flag' => True, 'msg' => $ext );
 }

 /**
  * 
  * 返回正確的圖片函數(shù)
  * @param unknown_type $ext
  */
 private function get_img_funcs($ext)
 {
  //選擇
  switch ($ext)
  {
   case 'jpg' :
    $header = 'Content-Type:image/jpeg';
    $createfunc = 'imagecreatefromjpeg';
    $savefunc = 'imagejpeg';
    break;
   case 'jpeg' :
    $header = 'Content-Type:image/jpeg';
    $createfunc = 'imagecreatefromjpeg';
    $savefunc = 'imagejpeg';
    break;
   case 'gif' :
    $header = 'Content-Type:image/gif';
    $createfunc = 'imagecreatefromgif';
    $savefunc = 'imagegif';
    break;
   case 'bmp' :
    $header = 'Content-Type:image/bmp';
    $createfunc = 'imagecreatefrombmp';
    $savefunc = 'imagebmp';
    break;
   default :
    $header = 'Content-Type:image/png';
    $createfunc = 'imagecreatefrompng';
    $savefunc = 'imagepng';
  }
  return array ('save_func' => $savefunc, 'create_func' => $createfunc, 'header' => $header );
 }

 /**
  * 
  * 檢查并試著創(chuàng)建目錄
  * @param $save_img
  */
 private function check_dir($save_img)
 {
  $dir = dirname ( $save_img );
  if (! is_dir ( $dir ))
  {
   if (! mkdir ( $dir, 0777, true ))
   {
    return array ('flag' => False, 'msg' => "圖片保存目錄 $dir 無法創(chuàng)建!" );
   }
  }
  return array ('flag' => True, 'msg' => '' );
 }
}

if (! empty ( $_FILES ['test'] ['tmp_name'] ))
{
 //例子
 $img = new Img ();
 //原圖
 $name = explode ( '.', $_FILES ['test'] ['name'] );
 $org_img = 'D:/test.' . end ( $name );
 move_uploaded_file ( $_FILES ['test'] ['tmp_name'], $org_img );
 $option = array ('width' => $_POST ['width'], 'height' => $_POST ['height'] );
 if ($_POST ['type'] == 1)
 {
  $s = $img->resize_image ( $org_img, '', $option );
 } else
 {
  $img->thumb_img ( $org_img, '', $option );
 }
 unlink ( $org_img );
}


標(biāo)簽: [db:TAGG]

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:python操作mysql數(shù)據(jù)庫代碼

下一篇:php匹配url的正則表達式