php中獲得圖像類型的兩個函數image_type_to_extension、image_type_to_mime_type,做圖像處理的時候一直不清楚這倆個函數有什么區別,今天特意查了一下,有了一些初步的了解。
首先,手冊的介紹搬過來。
image_type_to_extension — 取得圖像類型的文件后綴
說明
$imagetype [, bool $include_dot = TRUE ] )
根據給定的常量 IMAGETYPE_XXX 返回后綴名。
參數
-
imagetype -
IMAGETYPE_XXX 系列常量之一。
-
include_dot -
是否在后綴名前加一個點。默認是
TRUE。
<?php
// 創建圖像實例
$im = imagecreatetruecolor(100, 100);
// 保存圖像
imagepng($im, './test' . image_type_to_extension(IMAGETYPE_PNG));
imagedestroy($im);
?>
以獲得一個圖片的類型為例,
<?php $imagesrc='images/10.jpg'; $size=getimagesize($imagesrc); $imgtype=image_type_to_extension($size[2],false); echo $imgtype;//jpeg
getimagesize — 取得圖像大小
返回一個具有四個單元的數組。索引 0 包含圖像寬度的像素值,索引 1 包含圖像高度的像素值。索引 2 是圖像類型的標記:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。這些標記與 PHP 4.3.0 新加的 IMAGETYPE 常量對應。索引 3 是文本字符串,內容為"height="yyy" width="xxx"",可直接用於 IMG 標記。
image_type_to_mime_type — 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的圖像類型的 MIME 類型
說明
$imagetype )
image_type_to_mime_type() 函數可以判斷一個 IMAGETYPE 常量的 MIME 類型。
Example #1 image_type_to_mime_type (file)
<?php
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));
?>
