Home > php教程 > php手册 > 在IE上传JPG图片的问题

在IE上传JPG图片的问题

WBOY
Release: 2016-06-13 09:38:20
Original
1016 people have browsed it

前一段时间做了一个图片上传的小程序,今天有人说jpg图片上传不了。我在本地Chrome、Firefox测了下,发现没有问题,上传正常。我问他用什么浏览器,原来是世界之窗、还有360......我囧。我想,是不是IE的问题?于是我在IE下测了,果然不行。原来的程序如下:

	switch($type)
	{
		case "image/jpeg":
		$resultImage = imagecreatefromjpeg($original);
		imagejpeg($resultImage, $target, $quality); 
		break;
		
		case "image/png":
		$resultImage = imagecreatefrompng($original);
		imagepng($resultImage, $target, $quality_png);
		break;
		
		case "image/gif":
		$resultImage = imagecreatefromgif($original);
		imagegif($resultImage, $target, $quality);
		break;
		
		default :
		die("不支持此文件类型");
        exit;
	}
Copy after login

后来google了下,发现关于jpg格式的图片在IE下确实有些差异。

在IE下上传一副jpg图片,然后打印该上传文件的信息如下:

Array
(
	[name] => bkjia.jpg
	[type] => image/pjpeg
	[tmp_name] => /tmp/phprY0loE
	[error] => 0
	[size] => 71189
)
Copy after login

jpg格式的图片的Mimetype为image/pjpeg。由于开发中都用Chrome或者Firefox开发,在判断时并没有加image/pjpeg,所以导致jpg格式的图片在IE内核的浏览器无法被识别。

下面是各格式图片在不同浏览器中的格式对照:

firefox image/jpeg image/bmp image/gif image/png
ie 6 image/pjpeg image/bmp image/gif image/x-png
ie 7 image/pjpeg image/bmp image/gif image/x-png
ie 8 image/pjpeg image/bmp image/gif image/x-png
Copy after login

传图片时,ie会把 jpg、jpeg翻译成image/pjpeg,png翻译成image/x-png 。而火狐则很标准:jpg、jpeg翻译成image/jpeg,png翻译成image/png。

程序改成这样就OK了。

	switch($type)
	{
		case "image/jpeg":
		$resultImage = imagecreatefromjpeg($original);
		imagejpeg($resultImage, $target, $quality); 
		break;
		
		case "image/pjpeg":
		$resultImage = imagecreatefromjpeg($original);
		imagejpeg($resultImage, $target, $quality); 
		break;
		
		case "image/png":
		$resultImage = imagecreatefrompng($original);
		imagepng($resultImage, $target, $quality_png);
		break;
		
		case "image/gif":
		$resultImage = imagecreatefromgif($original);
		imagegif($resultImage, $target, $quality);
		break;
		
		default :
		die("不支持此文件类型");
        exit;
	}
Copy after login
Related labels:
ie
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template