最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP生成圖片縮略圖類示例

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

本文實(shí)例講述了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ù)文件名,初始化圖片,
   * 計(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ù)圖片路徑獲取相關(guān)寬、高、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;
  }
  /**
   * 將圖片文件轉(zhuǎn)為資源類類型
   *
   * @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    重新采樣圖片,默認(rèn)是
   *
   * @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:這里再為大家推薦幾款比較實(shí)用的圖片處理工具供大家參考使用:

在線圖片轉(zhuǎn)換BASE64工具:
http://tools.jb51.net/transcoding/img2base64

ICO圖標(biāo)在線生成工具:
http://tools.jb51.net/aideddesign/ico_img

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

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

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

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • PHP過濾黑名單關(guān)鍵字的方法

    PHP過濾黑名單關(guān)鍵字的方法

    這篇文章主要介紹了PHP過濾黑名單關(guān)鍵字的方法,通過保存過濾關(guān)鍵字到txt文件,再讀取txt文件內(nèi)容進(jìn)行正則匹配替換來實(shí)現(xiàn)過濾功能,需要的朋友可以參考下
    2014-12-12
  • php實(shí)現(xiàn)根據(jù)url自動(dòng)生成縮略圖的方法

    php實(shí)現(xiàn)根據(jù)url自動(dòng)生成縮略圖的方法

    這篇文章主要介紹了php實(shí)現(xiàn)根據(jù)url自動(dòng)生成縮略圖的方法,詳細(xì)講述了相關(guān)庫的使用、配置文件寫法及類文件的用法示例,需要的朋友可以參考下
    2014-09-09
  • php防止sql注入簡單分析

    php防止sql注入簡單分析

    這篇文章主要介紹了php防止sql注入的方法,簡單分析了通過stripslashes及mysql_real_escape_string函數(shù)進(jìn)行字符轉(zhuǎn)移處理的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • php生成xml簡單實(shí)例代碼

    php生成xml簡單實(shí)例代碼

    使用PHP DOMDocument創(chuàng)建動(dòng)態(tài)XML文件
    2009-12-12
  • php實(shí)現(xiàn)的一段簡單概率相關(guān)代碼

    php實(shí)現(xiàn)的一段簡單概率相關(guān)代碼

    這篇文章主要介紹了php實(shí)現(xiàn)的一段簡單概率相關(guān)代碼,通過數(shù)字的運(yùn)算分析概率統(tǒng)計(jì)的原理與使用技巧,需要的朋友可以參考下
    2016-05-05
  • 最新評(píng)論

    崇左市| 沙河市| 上栗县| 黑水县| 墨江| 渑池县| 融水| 潍坊市| 北碚区| 凉山| 吐鲁番市| 郯城县| 白朗县| 葫芦岛市| 宜阳县| 娄底市| 义马市| 定州市| 延庆县| 东光县| 盐池县| 平利县| 时尚| 宕昌县| 大英县| 赣州市| 黑山县| 孟州市| 饶河县| 噶尔县| 任丘市| 昌图县| 长沙市| 武山县| 大方县| 舟山市| 库车县| 沧源| 阿拉善盟| 浦县| 定西市|