佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > PHP簡單實(shí)現(xiàn)圖片格式轉(zhuǎn)換(jpg轉(zhuǎn)png,gif轉(zhuǎn)png等)

PHP簡單實(shí)現(xiàn)圖片格式轉(zhuǎn)換(jpg轉(zhuǎn)png,gif轉(zhuǎn)png等)

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 服務(wù)器配置 Linux服務(wù)器 團(tuán)購網(wǎng)站 銀行業(yè)務(wù) 科大訊飛語音識(shí)別系統(tǒng) 電子圍欄 阿里云

需求

開發(fā)過程中總會(huì)遇到一些需求需要對圖片格式進(jìn)行轉(zhuǎn)換。比如 gif轉(zhuǎn)png,jpg轉(zhuǎn)png

如最近使用某平臺(tái)的圖片文件識(shí)別,居然不支持gif格式,那么就需要將gif處理成png等。

依賴

php擴(kuò)展 gd 和 exif

實(shí)現(xiàn)

/**
 * 圖片格式轉(zhuǎn)換
 * @param string $image_path 文件路徑或url
 * @param string $to_ext 待轉(zhuǎn)格式,支持png,gif,jpeg,wbmp,webp,xbm
 * @param null|string $save_path 存儲(chǔ)路徑,null則返回二進(jìn)制內(nèi)容,string則返回true|false
 * @return boolean|string $save_path是null則返回二進(jìn)制內(nèi)容,是string則返回true|false
 * @throws Exception 
 * @author klinson klinson@163.com>
 */
function transform_image($image_path, $to_ext = 'png', $save_path = null)
{
  if (! in_array($to_ext, ['png', 'gif', 'jpeg', 'wbmp', 'webp', 'xbm'])) {
    throw new \Exception('unsupport transform image to ' . $to_ext);
  }
  switch (exif_imagetype($image_path)) {
    case IMAGETYPE_GIF :
      $img = imagecreatefromgif($image_path);
      break;
    case IMAGETYPE_JPEG :
    case IMAGETYPE_JPEG2000:
      $img = imagecreatefromjpeg($image_path);
      break;
    case IMAGETYPE_PNG:
      $img = imagecreatefrompng($image_path);
      break;
    case IMAGETYPE_BMP:
    case IMAGETYPE_WBMP:
      $img = imagecreatefromwbmp($image_path);
      break;
    case IMAGETYPE_XBM:
      $img = imagecreatefromxbm($image_path);
      break;
    case IMAGETYPE_WEBP: //(從 PHP 7.1.0 開始支持)
      $img = imagecreatefromwebp($image_path);
      break;
    default :
      throw new \Exception('Invalid image type');
  }
  $function = 'image'.$to_ext;
  if ($save_path) {
    return $function($img, $save_path);
  } else {
    $tmp = __DIR__.'/'.uniqid().'.'.$to_ext;
    if ($function($img, $tmp)) {
      $content = file_get_contents($tmp);
      unlink($tmp);
      return $content;
    } else {
      unlink($tmp);
      throw new \Exception('the file '.$tmp.' can not write');
    }
  }
}

使用

// 轉(zhuǎn)換后保存在test.png
transform_image($url, 'png', './test.png');
transform_image($filepath, 'png', './test.png');
// 轉(zhuǎn)換后二進(jìn)制結(jié)果直接返回
transform_image($url, 'png');
transform_image($filepath, 'png');

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP中使用Imagick讀取pdf并生成png縮略圖實(shí)例
  • PHP使用imagick讀取PDF生成png縮略圖的兩種方法
  • PHP輸出圖像imagegif、imagejpeg與imagepng函數(shù)用法分析
  • php縮放gif和png圖透明背景變成黑色的解決方法
  • PHP實(shí)現(xiàn)生成透明背景的PNG縮略圖函數(shù)分享
  • PHP基于GD庫的縮略圖生成代碼(支持jpg,gif,png格式)
  • php 處理png圖片白色背景色改為透明色的實(shí)例代碼
  • PHP實(shí)現(xiàn)對png圖像進(jìn)行縮放的方法(支持透明背景)
  • 支持png透明圖片的php生成縮略圖類分享
  • PHP添加PNG圖片背景透明水印操作類定義與用法示例
  • php 實(shí)現(xiàn)svg轉(zhuǎn)化png格式的方法分析

標(biāo)簽:衢州 萍鄉(xiāng) 廣元 衡水 大理 蚌埠 棗莊 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP簡單實(shí)現(xiàn)圖片格式轉(zhuǎn)換(jpg轉(zhuǎn)png,gif轉(zhuǎn)png等)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    上蔡县| 丰县| 乾安县| 社会| 大兴区| 南宫市| 高雄县| 霍州市| 基隆市| 陇南市| 陆河县| 永康市| 白城市| 拉萨市| 德昌县| 安丘市| 汕头市| 巴里| 明溪县| 玛沁县| 新沂市| 昆山市| 松滋市| 阳西县| 通山县| 剑阁县| 云阳县| 交口县| 东乡| 临西县| 城市| 湟中县| 平凉市| 清水河县| 彰化县| 班戈县| 广宗县| 大悟县| 嘉义县| 西平县| 六盘水市|