In a site I’m developing I need to be able to pass all my images through imagejpeg(), so I decided (as my site only accepts JPG, BMP + PNG uploads) to simply convert BMPs and PNGs to a JPG first.
Now to convert the BMP I used the script found here: http://forums.codewalkers.com/php-coding-7/how-to-convert-bmp-to-jpg-879135.html
The script works fine when I pass a normal BMP through it.
Now, I had a PNG which I had problems converting via imagecreatefrompng(), and after a while I realised it had the mime-type image/x-ms-bmp….
I tried passing the image through the BMP script but I get the following error:
Warning: imagecreatefromgd() [function.imagecreatefromgd]: ‘C:UsersTomAppDataLocalTempGD50C1.tmp’ is not a valid GD file in C:xampphtdocstestcropimageFCreateImageFromBMP.php on line 10
If anyone has come accross this before, please help. If you need to see any code, just let me know.
Thanks in advance, Tom.
Edit: Might be useful to mention, the line which the error occurs on (as from the link above) si this one:
$tmp_name = tempnam("/tmp", "GD");
Advertisement
Answer
The code you linked to bails out if a few preconditions fail:
if(!($src_f = fopen($src, "rb"))) { ... if(!($dest_f = fopen($dest, "wb"))) { ... if($type != 0x4D42) { // signature "BM"
So assuming that the source and tmp files are read/writable, it looks like the file you are giving it (which is sent as a .png but gives a BMP mime type) is in fact not a BMP file either, because the file contents don’t start with the “BM” identifier. If you attach the file, perhaps somebody could identify what it really is.
Another solution I’ve used to this problem is to use ImageMagick’s convert command to convert most file types, whatever they may be, to the desired format:
// convert uses the file extension to determine the output format, so // change .jpg to whatever you'd like $tmp_name = tempnam("/tmp", "convert") . '.jpg'; exec('c:pathtoimagemagickconvert ' . $inputFile . ' ' . $tmp_name);
You can get ImageMagick for Windows and other platforms here: Link