php image類型實現(xiàn)轉(zhuǎn)換的方法:1、創(chuàng)建一個PHP示例文件;2、通過“function image_change($image_path, $to_ext = 'png',$save_path=null){...}”方法實現(xiàn)轉(zhuǎn)換即可。
本文操作環(huán)境:Windows7系統(tǒng),PHP7.1版,Dell G3電腦。
php image類型怎么實現(xiàn)轉(zhuǎn)換?
php圖片格式轉(zhuǎ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 存儲路徑,null則返回二進制內(nèi)容,string則返回true|false * @return boolean|string $save_path是null則返回二進制內(nèi)容,是string則返回true|false * @throws Exception */ function image_change($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'); } } }
推薦學(xué)習(xí):《PHP視頻教程》
立即學(xué)習(xí)“PHP免費學(xué)習(xí)筆記(深入)”;
以上就是php image類型怎么實現(xiàn)轉(zhuǎn)換的詳細內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
PHP怎么學(xué)習(xí)?PHP怎么入門?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號