国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP生成圖片縮略圖類示例

2024-09-04 11:44:40
字體:
來源:轉載
供稿:網友

本文實例講述了PHP生成圖片縮略圖類。分享給大家供大家參考,具體如下:

  1. classApp_image_helper { 
  2.   protected$imgFileName
  3.   protected$imgWidth
  4.   protected$imgHeight
  5.   protected$imgMime
  6.   protected$imgResource
  7.   static $imgMineList 
  8.     =array
  9.       'jpeg'=>'image/jpeg'
  10.       'gif'=>'image/gif'
  11.       'png'=>'image/png'
  12.       'wbmp'=>'image/wbmp'
  13.     ); 
  14.   /** 
  15.    * 根據文件名,初始化圖片, 
  16.    * 計算出給定圖片的寬、高、圖片類型,并獲取圖片的資源保存到內存,便于下次使用 
  17.    * App_image_helper constructor. 
  18.    * 
  19.    * @param $fileName 
  20.    */ 
  21.   publicfunction__construct($fileName) { 
  22.     $this->imgFileName =$fileName
  23.     list($this->imgWidth,$this->imgHeight,$this->imgMime) =$this->getImageInfo($this->imgFileName); 
  24.     $this->imgResource =$this->getImageResource($this->imgFileName); 
  25.   } 
  26.   /** 
  27.    * 根據圖片路徑獲取相關寬、高、MIME類型信息 
  28.    * 
  29.    * @param $fileName 
  30.    * 
  31.    * @return array|null 
  32.    */ 
  33.   protectedfunctiongetImageInfo($fileName) { 
  34.     $result= null; 
  35.     if(is_file($fileName) ) { 
  36.       $tmpImageInfo=getimagesize($fileName); 
  37.       if($tmpImageInfo) { 
  38.         $result=array($tmpImageInfo[0],$tmpImageInfo[1],$tmpImageInfo['mime']); 
  39.       } 
  40.     } 
  41.     return$result
  42.   } 
  43.   /** 
  44.    * 將圖片文件轉為資源類類型 
  45.    * 
  46.    * @param $fileName 
  47.    * 
  48.    * @return null|resource 
  49.    */ 
  50.   protectedfunctiongetImageResource($fileName) { 
  51.     $image= null; 
  52.     if(is_file($fileName) ) { 
  53.       switch($this->imgMime) { 
  54.         caseself::$imgMineList['jpeg']: 
  55.           $image= imagecreatefromjpeg($fileName); 
  56.           break
  57.         caseself::$imgMineList['gif']: 
  58.           $image= imagecreatefromgif($fileName); 
  59.           break
  60.         caseself::$imgMineList['png']: 
  61.           $image= imagecreatefrompng($fileName); 
  62.           break
  63.         caseself::$imgMineList['wbmp']: 
  64.           $image= imagecreatefromwbmp($fileName); 
  65.           break
  66.         default
  67.           break
  68.       } 
  69.     } 
  70.     return$image
  71.   } 
  72.   /** 
  73.    * 可根據固定寬,等比縮放圖片;或根據百分比,等比縮放圖片 
  74.    * 
  75.    * @param int $width 
  76.    * @param int $percent 
  77.    * 
  78.    * @return array|null 
  79.    */ 
  80.   protectedfunctiongetSizeByScale($width= 360,$percent= 1) { 
  81.     $result= null; 
  82.     if($this->imgWidth &&$this->imgHeight ) { 
  83.       if($width) { 
  84.         $result=array($width,intval($width*$this->imgHeight /$this->imgWidth)); 
  85.       }elseif($percent) { 
  86.         $result=array(intval($this->imgWidth *$percent),intval($this->imgHeight *$percent)); 
  87.       } 
  88.     } 
  89.     return$result
  90.   } 
  91.   /** 
  92.    * 外調 
  93.    * 
  94.    * @param int $percentOrWidth int整數表示圖片縮放為固定寬度,0.0~0.99999表示縮放百分比 
  95.    * @param null $fileName 
  96.    * @param int $quality 
  97.    * @param bool $reSample    重新采樣圖片,默認是 
  98.    * 
  99.    * @return bool 
  100.    */ 
  101.   publicfunctioncreateImage($percentOrWidth= 1,$fileName= null,$quality= 75,$reSample= true) { 
  102.     $result= false; 
  103.     $fileName? header('Content-Type: '.$this->imgMime) : false; 
  104.     $size=$this->getSizeByScale(($percentOrWidth<= 1) ? null :$percentOrWidth,$percentOrWidth); 
  105.     if($size) { 
  106.       $thumb= imagecreatetruecolor($size[0],$size[1]); 
  107.       if($reSample) { 
  108.         imagecopyresampled($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight); 
  109.       }else
  110.         imagecopyresized($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight); 
  111.       } 
  112.       $result= imagejpeg($thumb,$fileName,$quality); 
  113.     } 
  114.     return$result
  115.   } 
  116. }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇东市| 苏尼特左旗| 凭祥市| 云林县| 民乐县| 常熟市| 永兴县| 安塞县| 封开县| 浏阳市| 英德市| 漯河市| 吉木萨尔县| 安阳市| 尖扎县| 文成县| 大埔县| 泗水县| 钦州市| 武冈市| 承德县| 靖远县| 冀州市| 彭山县| 隆安县| 太谷县| 西乡县| 绥滨县| 平凉市| 刚察县| 抚宁县| 洪江市| 志丹县| 冕宁县| 昔阳县| 泾阳县| 宝鸡市| 德兴市| 延长县| 甘孜| 宁陵县|