Page 1 of 1

Image resize script, please make my image look better!

Posted: Tue Mar 25, 2008 1:11 am
by epoxy_nc
I found an example script for resizing images [sorry, forgot src :( ] and have hacked it together so it does more of what i need to do.

The pictures resize great, but now that I am adding real pics (photographs) i see the color is kind of crap and granually

It looks like it is due to using 256 colors.. i've tried my best to read around this problem, but didn't really get anywhere.

Any suggestions...?

Code: Select all

function resize_image($c, $type) {
 
    global $errors, $usermsg;
    
    @require ("lib/conf.inc.php");
 
    $newwidth = $dimension_w;
    $newheight = $dimension_h;
    $tnnewwidth = $dimension_tn_w;
    $tnnewheight = $dimension_tn_w;
        
    // set $url To equal the filename for later use
    $url = $_FILES['imagefile']['name'][$c];    
    
    // if file DO NOT EXIST, move to a perm location
    if(file_exists(dirname($_SERVER['SCRIPT_FILENAME']) . '/' . $images_path . '/' . $_FILES['imagefile']['name'][$c])) {
        $errors[] = "File '" . $_FILES['imagefile']['name'][$c] . "' already exists.. try changing the name.";
        return 0;
    } else {
        $copy = copy($_FILES['imagefile']['tmp_name'][$c], $images_path . '/' .  $_FILES['imagefile']['name'][$c]); 
    }
        
    if( ! $errors) {
        if($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location            
            /*
            ** RESIZE MAIN IMAGE
            */
            // Make A New Temporary Image To Create The resized image
            
            switch($type) {
                case 'jpg':
                    $simg = imagecreatefromjpeg($images_path . '/' . $url);
                    break;
                case 'gif':
                    $simg = imagecreatefromgif($images_path . '/' . $url);
                    break;
                case 'png':
                    $simg = imagecreatefrompng($images_path . '/' . $url);
                    break;
            }
    
            $currentwidth = imagesx($simg);   
            $currentheight = imagesy($simg);   
            // Make New Image For Thumbnail
            $dimg = imagecreate($newwidth, $newheight);   
            // Create New Color Pallete
            imagetruecolortopalette($simg, false, 256);   
            $palsize = ImageColorsTotal($simg);
            // Counting Colors In The Image
            for ($i = 0; $i < $palsize; $i++) {   
                // Number Of Colors Used
                $colors = ImageColorsForIndex($simg, $i);   
                // Tell The Server What Colors This Image Will Use
                ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   
            }
            // Copy Resized Image To The New Image (So We Can Save It)
            imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currentwidth, $currentheight);   
            // Saving The Image
            
            switch($type) {
                case 'jpg':
                    imagejpeg($dimg, $images_path . '/' . $url);   
                    break;
                case 'gif':
                    imagegif($dimg, $images_path . '/' . $url);   
                    break;
                case 'png':
                    imagepng($dimg, $images_path . '/' . $url);   
                    break;
            }
               
            // Destroying The Temporary Image
            imagedestroy($simg);   
            // Destroying The Other Temporary Image
            imagedestroy($dimg);   
                   
                   
            /*
            ** RESIZE MAIN IMAGE TO THUMBNAIL
            */
            switch($type) {
                case 'jpg':
                    $tnsimg = imagecreatefromjpeg($images_path . "/" . $url);   
                    break;
                case 'gif':
                    $tnsimg = imagecreatefromgif($images_path . "/" . $url);   
                    break;
                case 'png':
                    $tnsimg = imagecreatefrompng($images_path . "/" . $url);   
                    break;
            }
               
            $tncurrentwidth = imagesx($tnsimg);   
            $tncurrentheight = imagesy($tnsimg);   
            // Make New Image For Thumbnail
            $tndimg = imagecreate($tnnewwidth, $tnnewheight);   
            // Create New Color Pallete
            imagetruecolortopalette($tnsimg, false, 256);   
            $palsize = ImageColorsTotal($tnsimg);
            // Counting Colors In The Image
            for ($i2 = 0; $i2 < $tnpalsize; $i2++) {   
                // Number Of Colors Used
                $tncolors = ImageColorsForIndex($tnsimg, $i2);   
                // Tell The Server What Colors This Image Will Use
                ImageColorAllocate($tndimg, $tncolors['red'], $tncolors['green'], $tncolors['blue']);   
            }
            // Copy Resized Image To The New Image (So We Can Save It)
            imagecopyresized($tndimg, $tnsimg, 0, 0, 0, 0, $tnnewwidth, $tnnewheight, $tncurrentwidth, $tncurrentheight);   
            
            // Saving The Image
            switch($type) {
                case 'jpg':
                    imagejpeg($tndimg, $imagestn_path . "/" . $url); 
                    break;
                case 'gif':
                    imagegif($tndimg, $imagestn_path . "/" . $url);
                    break;
                case 'png':
                    imagepng($tndimg, $imagestn_path . "/" . $url);   
                    break;
            }
               
            // Destroying The Temporary Image
            imagedestroy($tnsimg);   
            // Destroying The Other Temporary Image
            imagedestroy($tndimg);
        } else {
            $error[] = "Unable to upload or convert " . $_FILES['imagefile']['name'][$c] . ".";
            return 0;   
        }    
    }
    
    return 1;
    
}

Re: Image resize script, please make my image look better!

Posted: Tue Mar 25, 2008 4:54 am
by onion2k
For some reason that code is converting your images to 256 colours like you say. And it's using imagecopyresized instead of imagecopyresampled. It's not really ideal for resizing photos. I'll have a go at fixing it when I'm less busy, but personally I'd go and find a different thumbnail generator.

This one, for example, http://www.phpgd.com/scripts.php?script=1 if you're just after basic thumbnails, or one of the fancier ones from http://www.phpgd.com/scripts.php?section=2 (Disclaimer: I wrote them).