欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP生成圖片縮略圖類示例

 更新時間:2017年01月12日 10:22:08   作者:lx_3278@126  
這篇文章主要介紹了PHP生成圖片縮略圖類,可實現(xiàn)針對圖形文件的屬性獲取、轉換、文件生成等功能,非常簡便實用,需要的朋友可以參考下

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

class App_image_helper {
  protected $imgFileName;
  protected $imgWidth;
  protected $imgHeight;
  protected $imgMime;
  protected $imgResource;
  static  $imgMineList
    = array(
      'jpeg' => 'image/jpeg',
      'gif' => 'image/gif',
      'png' => 'image/png',
      'wbmp' => 'image/wbmp',
    );
  /**
   * 根據(jù)文件名,初始化圖片,
   * 計算出給定圖片的寬、高、圖片類型,并獲取圖片的資源保存到內(nèi)存,便于下次使用
   * App_image_helper constructor.
   *
   * @param $fileName
   */
  public function __construct($fileName) {
    $this->imgFileName = $fileName;
    list($this->imgWidth, $this->imgHeight, $this->imgMime) = $this->getImageInfo($this->imgFileName);
    $this->imgResource = $this->getImageResource($this->imgFileName);
  }
  /**
   * 根據(jù)圖片路徑獲取相關寬、高、MIME類型信息
   *
   * @param $fileName
   *
   * @return array|null
   */
  protected function getImageInfo($fileName) {
    $result = null;
    if ( is_file($fileName) ) {
      $tmpImageInfo = getimagesize($fileName);
      if ( $tmpImageInfo ) {
        $result = array($tmpImageInfo[0], $tmpImageInfo[1], $tmpImageInfo['mime']);
      }
    }
    return $result;
  }
  /**
   * 將圖片文件轉為資源類類型
   *
   * @param $fileName
   *
   * @return null|resource
   */
  protected function getImageResource($fileName) {
    $image = null;
    if ( is_file($fileName) ) {
      switch ($this->imgMime) {
        case self::$imgMineList['jpeg']:
          $image = imagecreatefromjpeg($fileName);
          break;
        case self::$imgMineList['gif']:
          $image = imagecreatefromgif($fileName);
          break;
        case self::$imgMineList['png']:
          $image = imagecreatefrompng($fileName);
          break;
        case self::$imgMineList['wbmp']:
          $image = imagecreatefromwbmp($fileName);
          break;
        default:
          break;
      }
    }
    return $image;
  }
  /**
   * 可根據(jù)固定寬,等比縮放圖片;或根據(jù)百分比,等比縮放圖片
   *
   * @param int $width
   * @param int $percent
   *
   * @return array|null
   */
  protected function getSizeByScale($width = 360, $percent = 1) {
    $result = null;
    if ( $this->imgWidth && $this->imgHeight ) {
      if ( $width ) {
        $result = array($width, intval($width * $this->imgHeight / $this->imgWidth));
      } elseif ( $percent ) {
        $result = array(intval($this->imgWidth * $percent), intval($this->imgHeight * $percent));
      }
    }
    return $result;
  }
  /**
   * 外調(diào)
   *
   * @param int $percentOrWidth int整數(shù)表示圖片縮放為固定寬度,0.0~0.99999表示縮放百分比
   * @param null $fileName
   * @param int $quality
   * @param bool $reSample    重新采樣圖片,默認是
   *
   * @return bool
   */
  public function createImage($percentOrWidth = 1, $fileName = null, $quality = 75, $reSample = true) {
    $result = false;
    $fileName ? header('Content-Type: ' . $this->imgMime) : false;
    $size = $this->getSizeByScale(($percentOrWidth <= 1) ? null : $percentOrWidth, $percentOrWidth);
    if ( $size ) {
      $thumb = imagecreatetruecolor($size[0], $size[1]);
      if ( $reSample ) {
        imagecopyresampled($thumb, $this->imgResource, 0, 0, 0, 0, $size[0], $size[1], $this->imgWidth, $this->imgHeight);
      } else {
        imagecopyresized($thumb, $this->imgResource, 0, 0, 0, 0, $size[0], $size[1], $this->imgWidth, $this->imgHeight);
      }
      $result = imagejpeg($thumb, $fileName, $quality);
    }
    return $result;
  }
}

PS:這里再為大家推薦幾款比較實用的圖片處理工具供大家參考使用:

在線圖片轉換BASE64工具:
http://tools.jb51.net/transcoding/img2base64

ICO圖標在線生成工具:
http://tools.jb51.net/aideddesign/ico_img

在線Email郵箱圖標制作工具:
http://tools.jb51.net/email/emaillogo

在線圖片格式轉換(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext

更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《PHP網(wǎng)絡編程技巧總結》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設計有所幫助。

相關文章

  • PHP定時自動生成靜態(tài)HTML的實現(xiàn)代碼

    PHP定時自動生成靜態(tài)HTML的實現(xiàn)代碼

    為了提高網(wǎng)站的訪問速度,我們往往采用生成靜態(tài)的方式來實現(xiàn),這樣確實把網(wǎng)站的訪問速度提高了非常多.
    2010-06-06
  • php DES加密算法實例分析

    php DES加密算法實例分析

    這篇文章主要介紹了php DES加密算法,結合實例形式分析了yii框架的DES加密算法類定義,以及兼容C#和java的3DES加密算法實現(xiàn)技巧,需要的朋友可以參考下
    2019-09-09
  • php將時間差轉換為字符串提示

    php將時間差轉換為字符串提示

    通過傳入數(shù)據(jù)庫中存儲的文章發(fā)表時的UNIX時間戳,來轉化為例如 幾分鐘前,幾小時前,幾天前 這樣的提示。如微博
    2011-09-09
  • php去除字符串兩邊空格、空字符串和換行符的方法

    php去除字符串兩邊空格、空字符串和換行符的方法

    這篇文章給大家介紹了php去除字符串兩邊空格、空字符串和換行符的方法,文中有詳細的代碼示例,對大家的學習或工作具有一定的參考價值,需要的朋友可以參考下
    2023-11-11
  • 生成php程序的php代碼

    生成php程序的php代碼

    前臺程序不少情況下需要生成.php文件,諸如多用戶的在線日記程序、留言簿以及自助網(wǎng)站程序等等,都不可避免地在與用戶的交互中生成.php程序文件。一般的,所生成的.php文件內(nèi)容并不復雜,但麻雀雖小五臟俱全,完整的.php文件結構必須得到保證。
    2008-04-04
  • PHP實現(xiàn)通過strace定位故障原因的方法

    PHP實現(xiàn)通過strace定位故障原因的方法

    這篇文章主要介紹了PHP實現(xiàn)通過strace定位故障原因的方法,結合實例形式分析了出現(xiàn)高負載情況下使用strace定位故障原因的相關命令與操作技巧,需要的朋友可以參考下
    2018-04-04
  • php閉包中使用use聲明變量的作用域實例分析

    php閉包中使用use聲明變量的作用域實例分析

    這篇文章主要介紹了php閉包中使用use聲明變量的作用域,結合實例形式分析了閉包中use聲明變量作用域的相關原理與分析,需要的朋友可以參考下
    2018-08-08
  • php實現(xiàn)多張圖片上傳加水印技巧

    php實現(xiàn)多張圖片上傳加水印技巧

    php如何實現(xiàn)多張圖片上傳加水印,接下來為大家詳細介紹下,感興趣的朋友可以參考下哈,希望對你學習php有所幫助
    2013-04-04
  • Thinkphp框架開發(fā)移動端接口(1)

    Thinkphp框架開發(fā)移動端接口(1)

    這篇文章主要為大家詳細介紹了Thinkphp框架開發(fā)移動端接口,具有一定的實用性,感興趣的小伙伴們可以參考一下
    2016-08-08
  • PHP單例模式應用示例【多次連接數(shù)據(jù)庫只實例化一次】

    PHP單例模式應用示例【多次連接數(shù)據(jù)庫只實例化一次】

    這篇文章主要介紹了PHP單例模式應用,結合實例形式分析了php基于單例模式實現(xiàn)多次連接數(shù)據(jù)庫只實例化一次的相關操作技巧與注意事項,需要的朋友可以參考下
    2018-12-12

最新評論