Image Thumbnail Creator

  • MasterZ
  • Expert
  • Expert
  • User avatar
  • Posts: 699
  • Loc: Colorado Springs

Post 3+ Months Ago

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: [ 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: [ 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?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

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


with
Code: [ 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
  • MasterZ
  • Expert
  • Expert
  • User avatar
  • Posts: 699
  • Loc: Colorado Springs

Post 3+ Months Ago

That did it!

Thank you very much joebert.
  • geethu_178
  • Born
  • Born
  • geethu_178
  • Posts: 1

Post 3+ Months Ago

Thanks for the answer.Iam facing this problem last few days
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

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

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 69 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.