Unable to upload gif and png files to photo gallery

I have a website with a gallery page where I can add, delete and edit photos and categories of photos.
Now according to my code (I had taken this code from the sitepoint article by Mayank Gandhi called Build an automated php gallery system in minutes) for the file, upload.php, I can upload only certain types of photos.
I am enclosing the part of the code which says so.

 
// initialization
$result_final = "";
$counter = 0;
// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/bmp' => 'bmp',
'image/x-png' => 'png');
 
// GD Function Suffix List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG');

According to the code, if I try to upload a file which contains an extension different from what is mentioned above, I will get a message, File is not a photo. (refer part of code below).

if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                        {
                                $result_final .= "File ".($counter+1)." is not a photo!<br />";

I am able to upload jpg files but when I tried to upload a gif or a png file, I was getting the message, File 1 is not a photo.
Now I can understand that the code does not contain type gif. But it does contain the extension, PNG. Why is it that I am still not able to upload it? What do I do to enable upload of all file types?

You misunderstod me - the quality comes when saving the image on the imagejpeg() line:


//Instead of 
imagejpeg($im, $result_final, 90);
// Try 
imagejpeg($im, $result_final, 9);
 

Your array was OK as it was.

Thank you so much. I changed the code at the part where it mentioned about saving the image as well as the thumnail. I tried uploading a png image and it worked. I also added the line,

'image/gif' => 'gif',

to the list of known photo types and now I am able to upload gif images as well.

 
// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/bmp' => 'bmp',
'image/gif' => 'gif',
'image/x-png' => 'png');

Thank you so much for helping me.

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘)’ in /home/jppp/public_html/upload.php on line 14

I am getting the above error after having changed my code like this.

 
// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/bmp' => 'bmp',
'image/x-png' => 'png('','', 9)'); //Line 14
 
// GD Function Suffix List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG('','', 9)');
 
 

But I am not able to locate my error.

Thanks for helping.

What happens if you change image/x-png to image/png

I still get the message, File is not a photo.
I changed it back to image/x-png and now I am getting an error like this.
[B]

Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in /home/jppp/public_html/upload.php on line 98

Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /home/jppp/public_html/upload.php on line 98

Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in /home/jppp/public_html/upload.php on line 147

Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /home/jppp/public_html/upload.php on line 147

File 1 Added Successfully!

[/B]

You shouldn’t be using “type” from _FILES to determine if it is an image or not.

logic_earth, I am a newbie in this matter. What should I be doing instead? Is it possible to rectify things by making a few changes to my code?

Normally, when one has an admin file to enable uploading of photos by the client, does everybody code in such a way so as to enable uploading of all types of photos or only certain types? What do the pros do?

If you search google there are some posts about

gd-png: fatal libpng error

The first post is about the quality setting

In php5, the quality range changes for imagePNG from 0-99 to 0-9. therefore you need to change your code to relect this, e.g.

imagePNG(‘’,‘’, 9);

(instead of imagePNG(‘’,‘’,90)) - I divided the quality value by 10 and then round or floor the value accordingly.

You shouldn’t be using “type” from _FILES to determine if it is an image or not.

What happens if you change image/x-png to image/png