GD: How to know image type? (JPEG, PNG, ..)

GD: How to know image type? (JPEG, PNG, ..)

am 15.01.2008 12:36:06 von tarmstrong

Hi.

I'm trying to create a Perl script which converts all files within a
directory into JPEG format. First, I read them using two lines
detecting extension (.jpg, .png, ...) but sometimes these files are
wrong and get this error message:
-----
gd-jpeg: JPEG library reports unrecoverable error: Not a JPEG file:
starts with 0x89 0x50
---------

Is there any method to get image type?
e.g.: GD::Image->getImageType("myfile.jpg")

Thank you very much.

Re: GD: How to know image type? (JPEG, PNG, ..)

am 15.01.2008 13:56:29 von Peter Ludikovsky

Thomas Armstrong wrote:
> Hi.
>
> I'm trying to create a Perl script which converts all files within a
> directory into JPEG format. First, I read them using two lines
> detecting extension (.jpg, .png, ...) but sometimes these files are
> wrong and get this error message:
> -----
> gd-jpeg: JPEG library reports unrecoverable error: Not a JPEG file:
> starts with 0x89 0x50
> ---------
>
> Is there any method to get image type?
> e.g.: GD::Image->getImageType("myfile.jpg")
>
> Thank you very much.
>


a) Invoke the divine powers of file(1)
b) open the file & read the identifying characters [0][1][2]...
c) use File::Type;

HTH
/peter

[0] http://filext.com/file-extension/PNG
[1] http://filext.com/file-extension/GIF
[2] http://filext.com/file-extension/JPG