新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
phpimage類型怎么實現(xiàn)轉(zhuǎn)換
本文操作環(huán)境:Windows7系統(tǒng),php7.1版,Dell G3電腦。

莊河網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。成都創(chuàng)新互聯(lián)于2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
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則返回二進(jìn)制內(nèi)容,string則返回true|false
* @return boolean|string $save_path是null則返回二進(jìn)制內(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');
}
}
} 網(wǎng)頁標(biāo)題:phpimage類型怎么實現(xiàn)轉(zhuǎn)換
路徑分享:http://www.dlmjj.cn/article/cohsjej.html


咨詢
建站咨詢
