php image_type_to_extension,image_type_to_extension

这篇博客作者遇到了在PHP 5.1.4(本地Windows XP和远程Linux环境)中`image_type_to_extension`函数不可用的问题。为了解决这个问题,作者分享了一个自定义函数,该函数模仿了`image_type_to_extension`的功能,根据图像类型返回相应的文件扩展名。此函数适用于那些同样遇到此问题的PHP开发者。
摘要由CSDN通过智能技术生成

[#6]

Ian Paul Short [2006-09-28 16:40:42]

2006-09-29 Author: Ian Paul Short (chukdocs at hotmail dot com)

For reasons I have yet to determine the function ??image_type_to_extension?? is not available to both PHP 5.1.4 on my local host running under Windows XP or on my remote host running under Linux. Has anyone else had this problem? If so kindly post your findings here please.

In the meantime I needed a workaround and with many thanks to the previous contributors on this page - I took bits and pieces developed the following function to emulate the function image_type_to_extension:

if(!function_exists('image_type_to_extension')){### Include this for graceful tranisition to using

### PHP's equivalent function when it's available in the box

function image_type_to_extension($image_type, $include_dot){

// Assign definitions and intialise variables

define ("INVALID_IMAGETYPE", '');// Empty string

$extension = INVALID_IMAGETYPE; // Return this (empty string) if "$image_type or $include_dot" do not contain legal values

if((is_int($image_type)) AND (is_bool($include_dot)) AND (IMAGETYPE_GIF <= $image_type) AND (IMAGETYPE_XBM >= $image_type)){ // Check input data are legal

$image_type_extension = array (### These integer values correspond to the the respective IMAGETYPE constants

IMAGETYPE_GIF => 'gif',###  1 = GIF

IMAGETYPE_JPEG => 'jpg',###  2 = JPG

IMAGETYPE_PNG => 'png',###  3 = PNG

IMAGETYPE_SWF => 'swf',###  4 = SWF

IMAGETYPE_PSD => 'psd',###  5 = PSD

IMAGETYPE_BMP => 'bmp',###  6 = BMP

IMAGETYPE_TIFF_II => 'tiff',###  7 = TIFF (intel byte order)

IMAGETYPE_TIFF_MM => 'tiff',###  8 = TIFF (motorola byte order)

IMAGETYPE_JPC => 'jpc',###  9 = JPC

IMAGETYPE_JP2 => 'jp2',### 10 = JP2

IMAGETYPE_JPX => 'jpf',### 11 = JPX Yes! jpf extension is correct for JPX image type

IMAGETYPE_JB2 => 'jb2',### 12 = JB2

IMAGETYPE_SWC => 'swc',### 13 = SWC

IMAGETYPE_IFF => 'aiff',### 14 = IFF

IMAGETYPE_WBMP => 'wbmp',### 15 = WBMP

IMAGETYPE_XBM => 'xbm'### 16 = XBM

);

$extension = $image_type_extension[$image_type]; // Get extension by using array as a look up table using $image_type as key

if($include_dot != false) $extension = '.' . $extension; // If $include_dot is true prefix a dot to extension

}

### Limitation - No user friendly error handler

return $extension;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值