Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Image Cropper failing on PNG & GIF

    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:
    PHP Code:
    $file_name=$target01;
    $crop_height="90";
    $crop_width="120";
    $file_typeexplode('.'$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
    Last edited by YourDirector; 04-26-2012 at 11:57 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 constants.

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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 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?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What version of GD are you using?
    Also, I assume you can view these images in browsers or image viewer programs as well?

  • #6
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    if ($fp = @fopen('../images/test.png''r'))
    {
        
    $i 0;
        while (
    $i++ < 8)
        {
            
    printf('%d 'fread($fp1));
        }
        
    fclose($fp);
    }
    else
    {
        die(
    'Could not open file for reading');

    What is the results of this?

  • #8
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    What is the results of this?
    0 0 0 0 0 0 0 0
    What is that returning?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #10
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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?

  • #11
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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!

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    YourDirector (04-27-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •