php识别图像类型为image/pjpeg格式,pjpeg是什么?

调试一套php系统,突然发现数据库里记录的jpg格式的图片附件,类型字段显示为附件image/pjpeg,很惊异,之前从来没有见过,还以为 是程序哪里出错了,在源程序里查找image/pjpeg,结果没有这样的字符串,于是google一下,相关的资料不是非常多,就网上查到的信息整理如 下:

image/pjpeg究竟是什么,与image/jpeg有什么区别?

image/pjpeg对应.jfif文件,image/jpeg对应.jpg、.jpe、.jpeg文件。
JFIF文件格式(JPEG文件交换格式,   JPEG   File   Interchonge   Format,是 progressive JPEG 的缩写)。JFIF文件格式只是将一种图像格或环绕JPEG压缩的一种简单方法,它们没有其他的更多功能。
JPEG文件格式(Joint   Photographic   Experts   Group(联合图像专家组))是一种有损压缩格式,能够将图像压缩在很小的储存空间,图像中重复或不重要的资料会被丢失,因此容易造成图像数据的损伤。 尤其是使用过高的压缩比例,将使最终解压缩后恢复的图像质量明显降低,如果追求高品质图像,不宜采用过高压缩比例。

英文原文:What is progressive JPEG?

一个错误的说法:

有人说IE下上传jpg 会被服务器端程序识别为image/pjpeg格式,但firefox则显示为正常的jpg,

“原因是ie会把 jpg、jpeg翻译成image/pjpeg,png翻译成image/x-png 。而火狐则很标准:jpg、jpeg翻译成image/jpeg,png翻译成image/png”

其实这种说法不对 的,虽然IE很烂,但这里却不是它的错,这些图片严格的讲确实是image/pjpeg格式的,firefox没有这样识别而已。

PHP与image/pjpeg

PHP GD库对image/pjpeg格式的图片支持不是非常完美,或者说是要求太严格,有时会出现这样的错误

Warning: imagecreatefromjpeg(): ‘/tmp/lalala’ is not a valid JPEG file in /path/upload.php on line 1

出现这个Warning是由于GD函数库检测发现是非标准JPEG图片格式导致。

解决方法,如果PHP版本 > 5.1.3,可以在php.ini中增加:
gd.jpeg_ignore_warning = 1

在 MIME 类型中,图像方面有这样两种: image/jpeg 与 image/pjpeg ,GD库只认识前者的传统格式,后者是 progressive JPEG 的缩写。

php官方BUG讨论区http://bugs.php.net/bug.php?id=29878

[2004-08-28 19:00 UTC] cyleriggs at kc dot rr dot com

Description: ———— When calling ImageCreateFromJPEG() on a valid jpeg file it fails and i get errors such as the following in my php error log: <code> [28-Aug-2004 05:21:29] PHP Warning: imagecreatefromjpeg() [<a href=’function.imagecreatefromjpeg’>function.imagecreatefromjpeg</a>]: ‘/usr/local/apache2/htdocs/pictures/Before Dad Went to Iraq/IMGP0008.JPG’ is not a valid JPEG file in /home/www/pictures/index.php on line 43 </code> however as can be seen through this link the file is a valid jpeg file: http://cyle.dyndns.org/pictures/Before Dad Went to Iraq/IMGP0008.JPG Also i have a memory_limit of 25mb so this cannot be the issue. Something that might help: http://cyle.dyndns.org/phpinfo.php I am writing this code for a picture gallery browser, while most pictures load, about 10% of my pictures cannot be opened through ImageCreateFromJpeg(), however it is always the same pictures that cannot be opened. The only trend i can see is that this does not appear to be happening on small jpegs. The page that I am having problems with is http://cyle.dyndns.org/pictures/index.php. The source code for this is at http://cyle.dyndns.org/pictures/index.phps. Expected result: —————- The image would be loaded and code continue to execute and i get the error message described above. Actual result: ————– Sometimes images load sometimes images cannot be opened.

[2004-08-29 08:22 UTC] pajoye@php.net

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

Your jpeg file is not valid. PHP could not take care of each possible failures in image files. Desktop softwares could. Fix your image and replace it.

--Pierre

[2004-09-30 17:05 UTC] paul at gslip dot com

I disagree. I have used my code successfully on montypics.com until the upgrade to php 5.0.1. I now get exactly the same results as the original poster.
Images that worked fine on previous versions now do not. I have a sample image that can be provided. I also have code I can share with the PHP team although I cannot post it here as it is lengthy and copyrighted.

[2004-10-05 00:34 UTC] cyleriggs at kc dot rr dot com

After playing around with the images themselves i have found that converting them from jpeg to png then back to jpeg fixed the problem and yielded no quality loss. I used the linux tool set called ImageMagick to do this. I wrote a short perl script to go through all my images and fix them. Other than this i have found no other way of stopping this. one interesting fact though is that while converting the images with the imagemagick utility it complained about some corruptions, however it was still able to open them fine and convert them. Maybe PHP is being too picky about the images that it opens as despite a few corrupted bytes EVERYthing was able to open these images except for PHP.

[2004-10-05 05:33 UTC] derick@php.net

PHP only reads non-coprrupted files - there is no bug here.

[2004-10-06 23:18 UTC] cyleriggs at kc dot rr dot com

Okay, so how about a feature suggestion than, i think that there should be the option in GD that allows you to try and open images that have a few corrupted bytes. If other programs can open them just fine what would be the harm in allowing programmers to choose to live with the corruptions?

[2004-10-07 05:12 UTC] derick@php.net

Corrupted files are invalid, and may crash an application if attempted to use. That's why we will not implement that. (Actually, it's not a PHP issue either, it has to do with how libgd handles opening files).

[2004-10-12 13:23 UTC] paul at gslip dot com

Has anyone at php verified that the images are corrupt? In what way are they corrupt?
Here's what I know:
1- These images were supported with no issues whatsoever before I 'upgraded' to php 5.x

2- Every other piece of software I own, including Windows Image Viewer, Photoshop, Paint, IE, Netscape, Imagemagick, etc. open the files with no complaint.

3- A visual inspection of the files on the byte level show nothing out of the ordinary.

That all leads me to believe that the images are not corrupt and that PHP or GD has a bug.

Please, stop being so stubborn and at least look at the problem. I can show you hundreds of examples of where this is happening on the web. Just do a google search for 'is not a valid JPEG file in' to see for yourselves how many sites are having this issue.

I suspect there's some new twist on the JPEG format that new digital cameras are using ,or something along those lines, that GD and PHP just aren't yet up to speed with.

Let's get this solved folks.

[2006-02-05 15:09 UTC] pajoye@php.net

This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

Thank you for the report, and for helping us make PHP better.

You can now allow the jpeg decoder to be more tolerant/weak:

error_reporting(E_ALL);
// 0 is the current behavior
ini_set('gd.jpeg_ignore_warning', 1);
$im = imagecreatefromjpeg($file);

Php 5.1.3+ will contain the fix.
发表评论?

0 条评论。

发表评论