7

in one of my PHP projects, imagecreatefrompng complains, that the image "is not a valid PNG file".

Warning: imagecreatefrompng() [function.imagecreatefrompng]:
'/.../...png' is not a valid PNG file in /.../...php on line ...

I'm pretty sure, the file is valid, because I can open it in any program. So I was wondering what else could cause this warning.

Does anybody know the details behind imagecreatefrompng or has had a similar problem before?

Thanks for your help,

Timo

Mike B
  • 31,886
  • 13
  • 87
  • 111
Timo
  • 663
  • 4
  • 9
  • 17
  • 1
    @Timo Welcome to SO. Instead of putting SOLVED in the title of your question, click the green checkmark next to the answer below. – Mike B Feb 19 '10 at 13:47

2 Answers2

15

Is it a PNG image? Run file against it, or try the fileinfo functions.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    Thanks for the link to the fileinfo functions. I let PHP check the mime type and surprisingly enough the image was a jpg file - renaming it to .jpg solved the problem. I guess every program I opened the file with automatically detected the mime type correctly... – Timo Feb 19 '10 at 12:01
  • 1
    I still get the same error after checking with those functions and getting the expected values: "PNG image, 873 x 361, 8-bit/color RGBA, non-interlaced" – spuas Sep 25 '12 at 15:32
  • 3
    @spuas: Then you have a different problem. – Ignacio Vazquez-Abrams Sep 25 '12 at 15:52
3

Check this mime type of file you are going to upload by using below function:

mime_content_type($_FILES['product_img']['tmp_name'])

Image editors can often recognize file type by its contents, php might just try to use the file based on its mime-type. If it don't match then there's an error.

entio
  • 3,816
  • 1
  • 24
  • 39
jalpa
  • 31
  • 1