PHP Conference Japan 2024

image_type_to_extension

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

image_type_to_extension取得影像類型的檔案副檔名

說明

image_type_to_extension(int $image_type, bool $include_dot = true): string|false

傳回指定 IMAGETYPE_* 常數的副檔名。

參數

image_type

其中一個 IMAGETYPE_* 常數。

include_dot

是否在副檔名前加上點。預設為 true

返回值

一個字串,包含與給定影像類型相對應的副檔名,失敗時則返回 false

範例

範例 #1 image_type_to_extension() 範例

<?php
// 建立影像實例
$im = imagecreatetruecolor(100, 100);

// 儲存影像
imagepng($im, './test' . image_type_to_extension(IMAGETYPE_PNG));
imagedestroy($im);
?>

注意事項

注意:

這個函式不需要 GD 影像函式庫。

新增一則註記

使用者貢獻的註記 5 則註記

twitter.com/jonathansampson
2 年前
如果您想從檔名(字串)取得副檔名,以下程式碼可以避免您使用 explode 字串分割的麻煩(請務必閱讀 pathinfo 的文件警告)

<?php

$filename
= "wallpaper.jpg";
$extension = pathinfo( $filename, PATHINFO_EXTENSION );

echo
$extension; // "jpg"

?>
Ian Paul Short chukdocsAtHotmailDotCom
18 年前
致: mail at spybreak dot de

我注意到您的解決方案是針對 mime_type_to_extension,但它有缺陷,因為 MIME 類型對應到副檔名並非唯一的。請參考我的範例,了解我的觀察結果。

此函式會執行影像類型或 MIME 類型到副檔名的轉換。由於限制,它不會嘗試處理重複的 MIME 類型。並非絕對可靠!
<?php
if(!function_exists('image_type_to_extension')){

$extension;

function
image_type_or_mime_type_to_extension($image_type, $include_dot) {
define ("INVALID_IMAGETYPE", '');

$extension = INVALID_IMAGETYPE; /// Default return value for invalid input

$image_type_identifiers = array ( ### These values correspond to the IMAGETYPE constants
array (IMAGETYPE_GIF => 'gif', "mime_type" => 'image/gif'), ### 1 = GIF
array (IMAGETYPE_JPEG => 'jpg', "mime_type" => 'image/jpeg'), ### 2 = JPG
array (IMAGETYPE_PNG => 'png', "mime_type" => 'image/png'), ### 3 = PNG
array (IMAGETYPE_SWF => 'swf', "mime_type" => 'application/x-shockwave-flash'), ### 4 = SWF // A. Duplicated MIME type
array (IMAGETYPE_PSD => 'psd', "mime_type" => 'image/psd'), ### 5 = PSD
array (IMAGETYPE_BMP => 'bmp', "mime_type" => 'image/bmp'), ### 6 = BMP
array (IMAGETYPE_TIFF_II => 'tiff', "mime_type" => 'image/tiff'), ### 7 = TIFF (intel byte order)
array (IMAGETYPE_TIFF_MM => 'tiff', "mime_type" => 'image/tiff'), ### 8 = TIFF (motorola byte order)
array (IMAGETYPE_JPC => 'jpc', "mime_type" => 'application/octet-stream'), ### 9 = JPC // B. Duplicated MIME type
array (IMAGETYPE_JP2 => 'jp2', "mime_type" => 'image/jp2'), ### 10 = JP2
array (IMAGETYPE_JPX => 'jpf', "mime_type" => 'application/octet-stream'), ### 11 = JPX // B. Duplicated MIME type
array (IMAGETYPE_JB2 => 'jb2', "mime_type" => 'application/octet-stream'), ### 12 = JB2 // B. Duplicated MIME type
array (IMAGETYPE_SWC => 'swc', "mime_type" => 'application/x-shockwave-flash'), ### 13 = SWC // A. Duplicated MIME type
array (IMAGETYPE_IFF => 'aiff', "mime_type" => 'image/iff'), ### 14 = IFF
array (IMAGETYPE_WBMP => 'wbmp', "mime_type" => 'image/vnd.wap.wbmp'), ### 15 = WBMP
array (IMAGETYPE_XBM => 'xbm', "mime_type" => 'image/xbm') ### 16 = XBM
);

if((
is_int($image_type)) AND (IMAGETYPE_GIF <= $image_type) AND (IMAGETYPE_XBM >= $image_type)){
$extension = $image_type_identifiers[$image_type-1]; // -1 because $image_type_identifiers array starts at [0]
$extension = $extension[$image_type];
}
elseif(
is_string($image_type) AND (($image_type != 'application/x-shockwave-flash') OR ($image_type != 'application/octet-stream'))){

$extension = match_mime_type_to_extension($image_type, $image_type_identifiers);
}
else
{
$extension = INVALID_IMAGETYPE;
}

if(
is_bool($include_dot)){

if((
false != $include_dot) AND (INVALID_IMAGETYPE != $extension)){
$extension = '.' . $extension;
}
}
else
{
$extension = INVALID_IMAGETYPE;
}

return
$extension;

}
}

function
match_mime_type_to_extension($image_type, $image_type_identifiers){
// Return from loop on a match
foreach($image_type_identifiers as $_key_outer_loop => $_val_outer_loop){
foreach(
$_val_outer_loop as $_key => $_val){
if(
is_int ($_key)){ // Keep record of extension for mime check
$extension = $_val;
}
if(
$_key == 'mime_type'){
if(
$_val === $image_type){ // Found match no need to continue looping
return $extension; ### Return
}
}
}
}
// Compared all values without match
return $extension = INVALID_IMAGETYPE;
}

$extension = image_type_or_mime_type_to_extension($image_type, $include_dot);
return
$extension;
}
?>
ergunadem5508 at gmail dot com
3 年前
// 如果您只想從檔案路徑名稱中找到格式,而無需建立任何影像

// 影像路徑名稱
$image_name = "image name.png";

// 最後一個點之後自然是格式。
$dots = explode(".",$image_name);

// 將 $dots 的最後一個資料賦值給變數 $type。
$type = $dots[(count($dots)-1)];

// 輸出的結果將不包含點號。
echo "without dot : ".$type;

// 包含點號
$type = ".".$type;
echo "dotted : ".$type;
aleksandrs dot bogdanovs at gmail dot com
18 年前
當我為我的相片網站寫一個腳本時,需要寫一個這樣的函式,它可以取得上傳檔案(圖片)的副檔名,所以函式如下:

<?php
function get_extension($imagetype)
{
if(empty(
$imagetype)) return false;
switch(
$imagetype)
{
case
'image/bmp': return '.bmp';
case
'image/cis-cod': return '.cod';
case
'image/gif': return '.gif';
case
'image/ief': return '.ief';
case
'image/jpeg': return '.jpg';
case
'image/pipeg': return '.jfif';
case
'image/tiff': return '.tif';
case
'image/x-cmu-raster': return '.ras';
case
'image/x-cmx': return '.cmx';
case
'image/x-icon': return '.ico';
case
'image/x-portable-anymap': return '.pnm';
case
'image/x-portable-bitmap': return '.pbm';
case
'image/x-portable-graymap': return '.pgm';
case
'image/x-portable-pixmap': return '.ppm';
case
'image/x-rgb': return '.rgb';
case
'image/x-xbitmap': return '.xbm';
case
'image/x-xpixmap': return '.xpm';
case
'image/x-xwindowdump': return '.xwd';
case
'image/png': return '.png';
case
'image/x-jps': return '.jps';
case
'image/x-freehand': return '.fh';
default: return
false;
}
}
?>

這對於那些將檔案上傳到伺服器的人很有用。
Ian Paul Short chukdocsAtHotmailDotCom
18 年前
2006-09-29

關於此頁面上一些貢獻的幾點說明。

1. 在我看來,所有模仿 "image_type_to_extension" 函式的方案都存在某些不足之處(請參閱我下面的評論)。這就是為什麼我寫了自己的程式碼並提交到這個頁面的原因。關於我的作品,任何評論、錯誤說明或改進建議都將不勝感激。

2. 避免以非常規的方式使用 Switch 語句來達到 "Break" 的效果(我注意到 return 語句的使用!)。即使在程式碼開始時它沒有作用,也要加入 default 案例(這讓其他人意識到不需要 default,或者最壞的情況下,忘記了它的存在)。

3. 在您可控的環境中,透過副檔名或 MIME 類型來判斷檔案內容可能看似一個具有吸引力的解決方案。然而,在現實世界中,無法保證 MIME 類型或副檔名與其關聯的檔案相符。

考慮使用函式來獲取圖片類型
getimagesize 或 (這個函式不需要 GD 函式庫)
exif_imagetype

4. 編碼不僅僅是拼湊一些東西來完成工作!!!無論做什麼都是有價值的 - 因此,髒話在這個論壇沒有立足之地!!

5. "oridan at hotmail dot com" 的想法非常巧妙。我會在我的專案中仔細研究一下。
To Top