常用的php圖片處理類(浮水印、等比縮放、固定高寬)分享_php執行個體

來源:互聯網
上載者:User

常用的php圖片處理類(浮水印、等比縮放、固定高寬)分享

<?php  //PHP 添加浮水印 & 比例縮圖 & 固定高度 & 固定寬度 類。 class Image_process{   public $source; //原圖   public $source_width;  //原圖寬度   public $source_height; //原圖高度   public $source_type_id;   public $orign_name;   public $orign_dirname;       //傳入原圖路徑   public function __construct($source){     $this->typeList = array(1=>'gif',2=>'jpg',3=>'png');     $ginfo = getimagesize($source);     $this->source_width = $ginfo[0];     $this->source_height = $ginfo[1];     $this->source_type_id = $ginfo[2];     $this->orign_url = $source;     $this->orign_name = basename($source);     $this->orign_dirname = dirname($source);   }       //判斷圖片的檔案的格式,返回PHP可識別的編碼   public function judgeType($type,$source){     if($type == 1){       return imagecreatefromgif($source); //gif     }else if($type == 2){       return imagecreatefromjpeg($source); //jpg     }else if($type == 3){       return imagecreatefrompng($source); //png     }else{       return false;     }   }       //產生浮水印圖片   public function waterMakeImage($logo){     $linfo = getimagesize($logo);     $logo_width = $linfo[0];     $logo_height = $linfo[1];     $logo_type_id = $linfo[2];     $sourceHandle = $this->judgeType($this->source_type_id,$this->orign_url);     $logoHandle = $this->judgeType($logo_type_id,$logo);     if(!$sourceHandle || !$logoHandle){       return false;     }     $x = ($this->source_width - $logo_width)/2;     $y = ($this->source_height - $logo_height)/2;     imagecopy($sourceHandle,$logoHandle,$x,$y,0,0,$logo_width,$logo_height);     $newPic = $this->orign_dirname.'\water_'.time().'.'.$this->typeList[$this->source_type_id];     if($this->saveImage($sourceHandle,$newPic)){       imagedestroy($sourceHandle);       imagedestroy($logoHandle);     }   }       //固定高度寬度   public function fixSizeImage($width,$height){     if($width > $this->source_width) $this->source_width;     if($height > $this->source_height) $this->source_height;     if($width === false){       $width = floor($this->source_width / ($this->source_height / $height));     }     if($height === false){       $height = floor($this->source_height / ($this->source_width / $width));     }     $this->tinyImage($width,$height);   }       //等比例縮放圖片   public function scaleImage($scale){     $width = floor($this->source_width * $scale);     $height = floor($this->source_height * $scale);     $this->tinyImage($width, $height);   }       //建立縮圖   public function tinyImage($width,$height){     $tinyImage = imagecreatetruecolor($width,$height);     $handle = $this->judgeType($this->source_type_id,$this->orign_url);     if(function_exists('imagecopyresampled')){       imagecopyresampled($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $this->source_width, $this->source_height);     }else{       imagecopyresized($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $this->source_width, $this->source_height);     }     $newPic = $this->orign_dirname.'\thumb_'.time().'_'.$width."_".$height.".".$this->typeList[$this->source_type_id];     if($this->saveImage($tinyImage,$newPic)){       imagedestroy($tinyImage);       imagedestroy($handle);     }   }   //儲存圖片   private function saveImage($image,$url){     if(imagejpeg($image,$url)){       return true;     }   } } $imgHandle = new Image_process('D:\AppServ\www\test\getimg\14061907445601.jpg'); //$imgHandle->waterMakeImage('D:\AppServ\www\test\getimg\shougongke.png');  //產生浮水印圖片 //$imgHandle->fixSizeImage(200,150); //固定長度圖片 $imgHandle->scaleImage(0.2); //等比例縮放 ?>

樣本二:

<?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 參數選項,包括: $maxwidth 寬 $maxheight 高 * array('width'=>xx,'height'=>xxx) * @internal * 我們一般的壓縮圖片方法,在圖片過長或過寬時產生的圖片 * 都會被“壓扁”,針對這個應採用先裁剪後按比例壓縮的方法 */ 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;  } } //擷取出相應的方法 $org_funcs = $this->get_img_funcs ( $org_ext ['msg'] ); //擷取原大小 $source = $org_funcs ['create_func'] ( $src_img ); $src_w = imagesx ( $source ); $src_h = imagesy ( $source ); //調整原始映像(保持圖片原形狀裁剪映像) $dst_scale = $option ['height'] / $option ['width']; //靶心圖表像長寬比 $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 參數設定 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;  } } //擷取出相應的方法 $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 浮水印標記映像 * @param $save_img 當其目錄不存在時,會試著建立目錄 * @param array $option 為浮水印的一些基本設定包含: * 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;  } } //擷取相應畫布 $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 ); //拷貝浮水印圖片座標 $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 ); //合成產生點座標 $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 ); } /** *  * 返回正確的圖片函數 * @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 ); } /** *  * 檢查並試著建立目錄 * @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 無法建立!" );  } } 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 );}

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.