tagomago Posted November 27, 2014 Posted November 27, 2014 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 imageresize($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 sizelist($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); // Resizeimagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Outputif(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?
wolstech Posted November 27, 2014 Posted November 27, 2014 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.
tagomago Posted November 27, 2014 Author Posted November 27, 2014 It all seems to work now! I think that it was just images from certain packages it didn't like so I have just suppressed warnings.
Byron Posted November 27, 2014 Posted November 27, 2014 Try one of your png images with this tool: http://bybyron.net/php/tools/image_uploader.php
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now