...

View Full Version : Resolved Image Cropper failing on PNG & GIF



YourDirector
04-26-2012, 05:04 PM
Hi there,

I'm currently using this code to resize and crop and save a thumbnail. It works fine for jpgs but with gifs and PNG's it throw up the error:



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


Here's the code:

$file_name=$target01;
$crop_height="90";
$crop_width="120";
$file_type= explode('.', $file_name);
$file_type = $file_type[count($file_type) -1];
$file_type = strtolower($file_type);
$file_title = explode('.', $pic01);
$file_title = $file_title[1];
$original_image_size = getimagesize($file_name);
$original_width = $original_image_size[0];
$original_height = $original_image_size[1];

if($file_type=='jpg')
{
$original_image_gd = imagecreatefromjpeg($file_name);
}

if($file_type=='gif')
{ $original_image_gd = imagecreatefromgif($file_name);
}

if($file_type=='png')
{
$original_image_gd = imagecreatefrompng($file_name);
}

$cropped_image_gd = imagecreatetruecolor($crop_width, $crop_height);
$wm = $original_width /$crop_width;
$hm = $original_height /$crop_height;
$h_height = $crop_height/2;
$w_height = $crop_width/2;

if($original_width > $original_height )
{
$adjusted_width =$original_width / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;

imagecopyresampled($cropped_image_gd ,$original_image_gd ,-$int_width,0,0,0, $adjusted_width, $crop_height, $original_width , $original_height );
}
elseif(($original_width < $original_height ) || ($original_width == $original_height ))
{
$adjusted_height = $original_height / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;

imagecopyresampled($cropped_image_gd , $original_image_gd ,0,-$int_height,0,0, $crop_width, $adjusted_height, $original_width , $original_height );
}
else {

imagecopyresampled($cropped_image_gd , $original_image_gd ,0,0,0,0, $crop_width, $crop_height, $original_width , $original_height );
}



if($file_type=='jpg')
{
$dest =$target."tm_".$pic01;
imagejpeg($cropped_image_gd ,$dest,100);
}
if($file_type=='gif')
{
$dest =$target."tm_".$pic01;
imagegif($cropped_image_gd ,$dest); ;
}

if($file_type=='png')
{
$dest =$target."tm_".$pic01;
imagepng($cropped_image_gd ,$dest,8);
}

I've checked gd_info and PNG's and GIFs are enabled. Any ideas?

Thanks

Josh

Fou-Lu
04-26-2012, 05:48 PM
The error specifies that the provided image is not a png image. It could be corrupt or just a rename from a different image type.
Try using the getimagesize()'s offset 2 to determine the filetype. Its not guaranteed to be accurate, but its better then using the extension. The results can be compared to one of the IMAGETYPE_XXX (http://ca2.php.net/manual/en/image.constants.php) constants.

bcarl314
04-26-2012, 05:49 PM
Are you sure your .png file is really a .png file? In linux, you can check the file using the following command:

$> file test.png

Which should output something like:

test.png: PNG image, 24 x 24, 8-bit/color RGB, non-interlaced

jpg would output:
test.png: JPEG image data, JFIF standard 1.01

and gif would output
test.png: GIF image data, version 89a, 24 x 24

Just because the file extension is .png doesn't mean the file is really a .png file.

YourDirector
04-26-2012, 06:42 PM
The error specifies that the provided image is not a png image. It could be corrupt or just a rename from a different image type.
Try using the getimagesize()'s offset 2 to determine the filetype. Its not guaranteed to be accurate, but its better then using the extension. The results can be compared to one of the IMAGETYPE_XXX (http://ca2.php.net/manual/en/image.constants.php) constants.

Just checked and it returned "3" which as far as I'm aware is PNG.
I've tried with several files and all png's and gif's return the same error. Even if I exported a new one from photoshop so I think it must be the code rather than the image itself?

Fou-Lu
04-26-2012, 07:09 PM
What version of GD are you using?
Also, I assume you can view these images in browsers or image viewer programs as well?

YourDirector
04-26-2012, 07:23 PM
What version of GD are you using?
Also, I assume you can view these images in browsers or image viewer programs as well?

This was the report:

["GD Version"]=> string(27) "bundled (2.0.34 compatible)" ["FreeType Support"]=> bool(true) ["FreeType Linkage"]=> string(13) "with freetype" ["T1Lib Support"]=> bool(false) ["GIF Read Support"]=> bool(true) ["GIF Create Support"]=> bool(true) ["JPG Support"]=> bool(true) ["PNG Support"]=> bool(true) ["WBMP Support"]=> bool(true) ["XPM Support"]=> bool(false) ["XBM Support"]=> bool(true) ["JIS-mapped Japanese Font Support"]=> bool(false) }

And yes, the images all open fine if just dragged into a browser or opened in any normal image viewer.

Fou-Lu
04-26-2012, 07:32 PM
if ($fp = @fopen('../images/test.png', 'r'))
{
$i = 0;
while ($i++ < 8)
{
printf('%d ', fread($fp, 1));
}
fclose($fp);
}
else
{
die('Could not open file for reading');
}

What is the results of this?

YourDirector
04-26-2012, 07:57 PM
What is the results of this?


0 0 0 0 0 0 0 0

What is that returning?

Fou-Lu
04-26-2012, 08:13 PM
Nothing, if this is right the first 8 bytes are empty (which means its not an image of any sorts). Although, change the read mode to 'rb'; binary flag is required for windows machines, and try again.

YourDirector
04-27-2012, 12:21 AM
It definitely can't be the images that I'm using though. I've tried 35 different png's and gif's sourced from all different places (Different websites, and made myself in photoshop). They all load ok when dragged straight into a browser and view in image viewers fine.

It is on the imagecreatefromgif() & imagecreatefrompng() functions that the error is generated. I heard somewhere that when resizing png's with php if the quality is set to more than 8 it can throw errors but I can't figure out how to set the quality for this function?

YourDirector
04-27-2012, 12:56 AM
Ah ok, Apologies must be made profusely on my behalf. It was the image itself, however, in my special little way I managed to get confused.

The image I uploaded was most definitely a png, however, what I was forgetting was that once it was saved on the server, I then had a bit of code resize that (To ensure the full size image is never larger than a set size), that then saves it back again and that is when the cropping script steps in. The initial resizer was saving with the original extension but I think it's actually a jpg.

Anyway, as my thumb saves to a different directory I've switched them round so it makes the thumb then reopens the original file and resizes it for the full one. Hey presto, perfect little thumbs. Thank you all for your help and apologies for my stupidity!

Fou-Lu
04-27-2012, 01:16 AM
Hah yessir that would do it. As you can see the extension alone doesn't help determine the type.
This has also verified that using getimagesize is also just a simple check. I knew it was nothing special, but I thought it scanned the header, so this says it just goes based off of the extension as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum