Image Thumbnail Creator

Post September 7th, 2008, 9:39 pm

I have a small php script to make a thumbnail image out of an uploaded image. But the problem is that only the jpg part works, if the image is png then it creates the file but it is corrupt or something so you can't actually see it.

The Error
Code: [ Download ] [ Select ]
Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in /homepages/18/d195457837/htdocs/zenorsoft/Development/AZ00101/acp/ShoppingCart.php on line 295

Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /homepages/18/d195457837/htdocs/zenorsoft/Development/AZ00101/acp/ShoppingCart.php on line 295

Warning: unlink(/tmp/phpLx0kPA) [function.unlink]: No such file or directory in /homepages/18/d195457837/htdocs/zenorsoft/Development/AZ00101/acp/ShoppingCart.php on line 413
  1. Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in /homepages/18/d195457837/htdocs/zenorsoft/Development/AZ00101/acp/ShoppingCart.php on line 295
  2. Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /homepages/18/d195457837/htdocs/zenorsoft/Development/AZ00101/acp/ShoppingCart.php on line 295
  3. Warning: unlink(/tmp/phpLx0kPA) [function.unlink]: No such file or directory in /homepages/18/d195457837/htdocs/zenorsoft/Development/AZ00101/acp/ShoppingCart.php on line 413


The Code
Code: [ Download ] [ Select ]
    // ************************
    // Function to resize image
    // ************************
    function ResizeImage($file_name, $destName, $maxSize, $type)
    {
        // $file_name = Name of the file to modify
        // $destName = Name of the file to save to
        // $maxSize = Maximum width or height of the file
        // $type = The type of image, jpg, png, or gif sent as a mime type

        $ImageDir = "../images/Store/";

    switch ($type) {
         case 'image/gif':
            $imSource = imagecreatefromgif($ImageDir . $file_name);
            break;
         case 'image/jpeg':
            $imSource = imagecreatefromjpeg($ImageDir . $file_name);
            break;
         case 'image/png':
            $imSource = imagecreatefrompng($ImageDir . $file_name);
            break;
    };


        $wSource = imagesx($imSource);
        $hSource = imagesy($imSource);
        $wDest = imagesx($imSource);
        $hDest = imagesy($imSource);

        if($wSource > $hSource)
        {
            if($wSource > $maxSize)
            {
                $wDest = $maxSize;
                $hDest = $hSource * ($maxSize / $wSource);
            };
        } else {
            if($hSource > $maxSize)
            {
                $hDest = $maxSize;
                $wDest = $wSource * ($maxSize / $hSource);
            };
        };

        $imDest = imagecreatetruecolor($wDest, $hDest);
        imagecopyresampled($imDest, $imSource, 0, 0, 0, 0, $wDest, $hDest, $wSource, $hSource);

        switch ( $type ) {
            case 'image/gif':
                $return = imagegif($imDest, $ImageDir . $destName, 80);
                break;
            case 'image/jpeg':
                $return = imagejpeg($imDest, $ImageDir . $destName, 80);
                break;
            case 'image/png':
                $return = imagepng($imDest, $ImageDir . $destName, 80);
                break;

        };

        return $return;
    }
  1.     // ************************
  2.     // Function to resize image
  3.     // ************************
  4.     function ResizeImage($file_name, $destName, $maxSize, $type)
  5.     {
  6.         // $file_name = Name of the file to modify
  7.         // $destName = Name of the file to save to
  8.         // $maxSize = Maximum width or height of the file
  9.         // $type = The type of image, jpg, png, or gif sent as a mime type
  10.         $ImageDir = "../images/Store/";
  11.     switch ($type) {
  12.          case 'image/gif':
  13.             $imSource = imagecreatefromgif($ImageDir . $file_name);
  14.             break;
  15.          case 'image/jpeg':
  16.             $imSource = imagecreatefromjpeg($ImageDir . $file_name);
  17.             break;
  18.          case 'image/png':
  19.             $imSource = imagecreatefrompng($ImageDir . $file_name);
  20.             break;
  21.     };
  22.         $wSource = imagesx($imSource);
  23.         $hSource = imagesy($imSource);
  24.         $wDest = imagesx($imSource);
  25.         $hDest = imagesy($imSource);
  26.         if($wSource > $hSource)
  27.         {
  28.             if($wSource > $maxSize)
  29.             {
  30.                 $wDest = $maxSize;
  31.                 $hDest = $hSource * ($maxSize / $wSource);
  32.             };
  33.         } else {
  34.             if($hSource > $maxSize)
  35.             {
  36.                 $hDest = $maxSize;
  37.                 $wDest = $wSource * ($maxSize / $hSource);
  38.             };
  39.         };
  40.         $imDest = imagecreatetruecolor($wDest, $hDest);
  41.         imagecopyresampled($imDest, $imSource, 0, 0, 0, 0, $wDest, $hDest, $wSource, $hSource);
  42.         switch ( $type ) {
  43.             case 'image/gif':
  44.                 $return = imagegif($imDest, $ImageDir . $destName, 80);
  45.                 break;
  46.             case 'image/jpeg':
  47.                 $return = imagejpeg($imDest, $ImageDir . $destName, 80);
  48.                 break;
  49.             case 'image/png':
  50.                 $return = imagepng($imDest, $ImageDir . $destName, 80);
  51.                 break;
  52.         };
  53.         return $return;
  54.     }


I'm a beginner at working with images and don't fully understand all of this. Actually this code is modified from a script that someone at Ozzu made for me a couple of years back.

Any ideas?
Image Eternal Truth Ministry - Biblical Resources, Forums
Have mercy on me, O God, according to your unfailing love; according to your great compassion blot out my transgressions. - Psalm 51:1 http://www.zssites.net - ZS Sites Web Hosting
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post September 7th, 2008, 9:39 pm

  • joebert
  • Weathered
  • Genius
  • User avatar
  • Joined: Feb 10, 2004
  • Posts: 11882
  • Loc: Clearwater, FL
  • Status: Offline

Post September 7th, 2008, 10:23 pm

replace
Code: [ Download ] [ Select ]
$return = imagepng($imDest, $ImageDir . $destName, 80);


with
Code: [ Download ] [ Select ]
$return = imagepng($imDest, $ImageDir . $destName, 8);


The problem is that the JPEG function accepts a quality of 0-100 and the PNG one accepts 0-9
Why yes, yes I am.

Post September 7th, 2008, 10:29 pm

That did it!

Thank you very much joebert.
Image Eternal Truth Ministry - Biblical Resources, Forums
Have mercy on me, O God, according to your unfailing love; according to your great compassion blot out my transgressions. - Psalm 51:1 http://www.zssites.net - ZS Sites Web Hosting
  • geethu_178
  • Born
  • Born
  • No Avatar
  • Joined: Oct 30, 2009
  • Posts: 1
  • Status: Offline

Post October 30th, 2009, 7:31 am

Thanks for the answer.Iam facing this problem last few days

Post November 9th, 2009, 3:02 am

I use the following class to resize my images ... I just have a tweaked one lying around here somewhere to suit my needs more specifically ... it works really well though ... http://www.white-hat-web-design.co.uk/a ... sizing.php
RewriteEngine On

RewriteRule ^(awesome|excellent|extraordinary)$ RT

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: spork and 170 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.