Jump to content

Problem Uploading Png And Bmp Images


tagomago

Recommended Posts

Hi,

 

I have been experiencing problems uploading PNG and BMP images, However this does not seem to occur with GIF and JPEG images and I cannot tell why. I get the following error message...

 

Warning: imagecreatefrompng() [function.imagecreatefrompng]: 'uploads/USRIMG0000000002-1788589870.png' is not a valid PNG file in /home1/tagomago/public_html/misc/forum/forum_fun.php on line 92

 

Warning: imagecopyresized() expects parameter 2 to be resource, boolean given in /home1/tagomago/public_html/misc/forum/forum_fun.php on line 102

 

When I download the files from the server they are black or corrupted. The code I am using is as follows...

 

 

$imagename = $_FILES['image']['name'];

$imagetmp = $_FILES['image']['tmp_name'];

$error = $_FILES['image']['error'];

$uid = $_SESSION['userid'];

 

if($uid && !$error)

{

//get extension

$pos = stripos($imagename,'.');

 

if($pos)

{

$ext = substr($imagename ,$pos+1,strlen($imagename)-$pos-1);

 

if(strncasecmp($ext,'GIF',3)==0

|| strncasecmp($ext,'PNG',3)==0

|| strncasecmp($ext,'BMP',3)==0

|| strncasecmp($ext,'JPG',3)==0

|| strncasecmp($ext,'JPEG',4)==0)

{

 

$target_path = createfilename($imagename,$uid);

 

if(move_uploaded_file($imagetmp, $target_path)) {

//resize image

resize($target_path,$ext);

 

//delete existing file

$query = "SELECT image FROM users WHERE userid='".$uid."'";

$result = mysql_query($query) or die('Error: ' . mysql_error());

$line = mysql_fetch_array($result,MYSQL_ASSOC);

unlink($line['image']);

 

//update image

$query = "UPDATE users SET image='".$target_path."' WHERE userid='".$uid."'";

mysql_query($query) or die('Error: ' . mysql_error());

} else{

echo "<script> alert('There was an error uploading the file, please try again!'); </script>";

}

}

else

{

echo "<script> alert('This image type is not supported'); </script>";

}

}

else

{

echo "<script> alert('This image type is not supported'); </script>";

}

}

else

{

echo "<script> alert('There was an error uploading the file, please try again!'); </script>";

}

 

 

With resize function...

 

function resize($file,$ext)

{

// get original size

list($width, $height) = getimagesize($file);

 

// calc new size

$max = max($width,$height);

$newwidth = $width*150/$max;

$newheight = $height*150/$max;

 

 

// Load

$thumb = imagecreatetruecolor($newwidth, $newheight);

 

if(strcasecmp($ext,'GIF')==0)

$source = imagecreatefromgif($file);

else if(strcasecmp($ext,'PNG')==0)

$source = imagecreatefrompng($file);

else if(strcasecmp($ext,'BMP')==0)

$source = imagecreatefromwbmp($file);

else if(strcasecmp($ext,'JPG')==0)

$source = imagecreatefromjpeg($file);

else if(strcasecmp($ext,'JPEG')==0)

$source = imagecreatefromjpeg($file);

 

 

// Resize

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

 

// Output

if(strcasecmp($ext,'GIF')==0)

imagegif($thumb,$file);

else if(strcasecmp($ext,'PNG')==0)

imagepng($thumb,$file);

else if(strcasecmp($ext,'BMP')==0)

imagewbmp($thumb,$file);

else if(strcasecmp($ext,'JPG')==0)

imagejpeg($thumb,$file);

else if(strcasecmp($ext,'JPEG')==0)

imagejpeg($thumb,$file);

 

imagedestroy($thumb);

}

 

Any ideas?

Link to comment
Share on other sites

The imagecreatefrompng() is a very picky function. There's about 15 different formats of PNG...alpha vs. no alpha, compressed or non-, with or without palette, different color depths, etc. Chances are the image isn't processable using that function. A PHP image manipulation system was my university capstone project...the image format support is a pain.

 

Try using imagecreatefromstring(file_get_contents($file)) instead of imagecreatefrompng(). It supports everything except BMP, and I'd advise against supporting BMP anyway due to size. BMP support can be added later if needed (PHP's site has some user-contributed functions for it).

 

There's also no reason to save in the original format. Save everything as a PNG since it saves space and uses loss-less compression. Filenames should be unique. unixtimestamp_random#.png is an easy way to name them.

 

That eliminates a lot of complexity and probably will fix your issue.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...