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

支持png透明圖片的php生成縮略圖類分享

 更新時間:2015年02月08日 14:35:30   投稿:junjie  
這篇文章主要介紹了支持png透明圖片的php生成縮略圖類分享,本文代碼基于GD2圖形庫,實現(xiàn)支持png透明圖片生成縮略圖,需要的朋友可以參考下

注:此功能依賴GD2圖形庫

最近要用php生成縮略圖,在網(wǎng)上找了一下,發(fā)現(xiàn)了這篇文章:PHP生成圖片縮略圖

試用了一下后,發(fā)現(xiàn)有這樣幾個問題:

1、png圖片生成的縮略圖是jpg格式的

2、png圖片生成的縮略圖沒有了透明(半透明)效果(填充了黑色背景)

3、代碼語法比較老

因此,在這個版本的基礎(chǔ)上簡單修改優(yōu)化了一下。

PHP生成縮略圖類

<?php
  /*
   * desc: Resize Image(png, jpg, gif)
   * author: 十年后的盧哥哥
   * date: 2014.11.13
   */
  class ResizeImage {
    //圖片類型
    private $type;
    //實際寬度
    private $width;
    //實際高度
    private $height;
    //改變后的寬度
    private $resize_width;
    //改變后的高度
    private $resize_height;
    //是否裁圖
    private $cut;
    //源圖象
    private $srcimg;
    //目標圖象地址
    private $dstimg;
    //臨時創(chuàng)建的圖象
    private $im;

    function __construct($imgPath, $width, $height, $isCut, $savePath) {
      $this->srcimg = $imgPath;
      $this->resize_width = $width;
      $this->resize_height = $height;
      $this->cut = $isCut;
      //圖片的類型

      $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));

      //初始化圖象
      $this->initi_img();
      //目標圖象地址
      $this -> dst_img($savePath);
      //--
      $this->width = imagesx($this->im);
      $this->height = imagesy($this->im);
      //生成圖象
      $this->newimg();
      ImageDestroy ($this->im);
    }

    private function newimg() {
      //改變后的圖象的比例
      $resize_ratio = ($this->resize_width)/($this->resize_height);
      //實際圖象的比例
      $ratio = ($this->width)/($this->height);
      if($this->cut) {
        //裁圖
        $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
        if($this->type=="png") {
          imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
        }
        if($ratio>=$resize_ratio) {
          //高度優(yōu)先
          imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
        } else {
          //寬度優(yōu)先
          imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
        }
      } else {
        //不裁圖
        if($ratio>=$resize_ratio) {
          $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
          if($this->type=="png") {
            imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
          }
          imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
        } else {
          $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
          if($this->type=="png") {
            imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
          }
          imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
        }
      }
      if($this->type=="png") {
        imagesavealpha($newimg, true);
        imagepng ($newimg,$this->dstimg);
      } else {
        imagejpeg ($newimg,$this->dstimg);
      }
    }

    //初始化圖象
    private function initi_img() {
      if($this->type=="jpg") {
        $this->im = imagecreatefromjpeg($this->srcimg);
      }
      if($this->type=="gif") {
        $this->im = imagecreatefromgif($this->srcimg);
      }
      if($this->type=="png") {
        $this->im = imagecreatefrompng($this->srcimg);
      }
    }

    //圖象目標地址
    private function dst_img($dstpath) {
      $full_length = strlen($this->srcimg);

      $type_length = strlen($this->type);
      $name_length = $full_length-$type_length;


      $name     = substr($this->srcimg,0,$name_length-1);
      $this->dstimg = $dstpath;
    }
  }
?>

使用

使用時,直接調(diào)用類的構(gòu)造函數(shù)即可,構(gòu)造函數(shù)如下:

$resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath);

參數(shù)
$imgPath:原圖片地址

$width:縮略圖寬

$height:縮略圖高

$isCut:是否裁剪,bool值

$savePath:縮略圖地址(可以跟原圖片地址相同)

示例

<?php
  include "ResizeImage.php";

  //jpg
  $jpgResize = new ResizeImage("img/test_1920_1200.jpg", 320, 240, false, "img/test_320_240.jpg");

  //png
  $pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");

?>

效果

相關(guān)文章

  • PHP計劃任務(wù)之關(guān)閉瀏覽器后仍然繼續(xù)執(zhí)行的函數(shù)

    PHP計劃任務(wù)之關(guān)閉瀏覽器后仍然繼續(xù)執(zhí)行的函數(shù)

    通過使用ignore_user_abort函數(shù)的特性,實現(xiàn)PHP計劃任務(wù),需要的朋友可以參考下。
    2010-07-07
  • PHP常用數(shù)組函數(shù)介紹

    PHP常用數(shù)組函數(shù)介紹

    編程怎么能少的了數(shù)組呢,以下是學(xué)習(xí)PHP時常用的數(shù)組處理函數(shù)。在編程中要遵循一個原則就是DRY(Don`t Repeat Yourself)原則,PHP中有大量的函數(shù),都記住這些函數(shù)不太現(xiàn)實,但常用的函數(shù)還是要熟練使用的,大部分的函數(shù)的使用方法可以通過查詢PHP的手冊來使用。
    2014-07-07
  • PHP實現(xiàn)自動登入google play下載app report的方法

    PHP實現(xiàn)自動登入google play下載app report的方法

    這篇文章主要介紹了PHP實現(xiàn)自動登入google play下載app report的方法,較為詳細的講述了登陸下載APP及對應(yīng)的實現(xiàn)代碼,具有不錯的實用價值,需要的朋友可以參考下
    2014-09-09
  • 如何用PHP實現(xiàn)分布算法之一致性哈希算法

    如何用PHP實現(xiàn)分布算法之一致性哈希算法

    進行大型網(wǎng)站的web開發(fā)時,分布式這個詞經(jīng)常出現(xiàn)在我們面前。如: memcache、redis服務(wù)器等緩存服務(wù)器的負載均衡(分布式cache)、 MySQL的分布式集群,這些都會用到分布式的思想,都要理解分布式算法。接下來以緩存服務(wù)器的負載均衡來談一下一致性哈希算法。
    2021-05-05
  • Apache2 httpd.conf 中文版

    Apache2 httpd.conf 中文版

    Apache2 httpd.conf 中文版...
    2006-12-12
  • PHP隨機字符串生成代碼(包括大小寫字母)

    PHP隨機字符串生成代碼(包括大小寫字母)

    PHP生成隨機字符串包括大小寫字母,這里介紹兩種方法,需要的朋友可以參考下
    2013-06-06
  • PHP實現(xiàn)批量生成App各種尺寸Logo

    PHP實現(xiàn)批量生成App各種尺寸Logo

    這篇文章主要介紹了PHP實現(xiàn)批量生成App各種尺寸Logo的方法和示例的核心代碼,非常的簡單實用,這里推薦給小伙伴們,有需要的可以參考下。
    2015-03-03
  • php 偽造本地文件包含漏洞的代碼

    php 偽造本地文件包含漏洞的代碼

    php 偽造本地文件包含漏洞的代碼,大家一定要注意防范。
    2011-11-11
  • php頁面函數(shù)設(shè)置超時限制的方法

    php頁面函數(shù)設(shè)置超時限制的方法

    這篇文章主要介紹了php頁面函數(shù)設(shè)置超時限制的方法,可通過函數(shù)控制超時限制,也可通過修改php配置文件實現(xiàn)修改超時限制,需要的朋友可以參考下
    2014-12-12
  • PHP常用的小程序代碼段

    PHP常用的小程序代碼段

    這篇文章主要介紹了PHP常用的小程序代碼段,包括計算時間差、分頁及查詢手機歸屬地等功能代碼,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11

最新評論

嵩明县| 丹寨县| 大石桥市| 吐鲁番市| 小金县| 山西省| 股票| 兴安县| 峨眉山市| 开封县| 桐城市| 株洲市| 洛阳市| 安乡县| 吉林市| 西安市| 南漳县| 茶陵县| 宝兴县| 集贤县| 青田县| 化德县| 韩城市| 本溪市| 德昌县| 包头市| 正定县| 成都市| 稷山县| 满洲里市| 涞源县| 周至县| 屏南县| 交口县| 黄平县| 新巴尔虎右旗| 五寨县| 韶关市| 石楼县| 神池县| 山东|