Page 1 of 1

Image Gallery

Posted: Mon Apr 09, 2007 12:38 pm
by Montano
Hey everyone,

Is it possible to create a script that will automatically resize and compress images? I'm looking to have an upload image form that allows users to upload product images. Once the image is uploaded, a thumbnail version is created from the original. I want the thumbnail to be resized to a specific size and compressed so the filesize isn't huge. It would still have to keep its quality though.

Does anyone know how to do this or is there a script out there that can integrate with my current site?

Thanks!

Montano

Posted: Mon Apr 09, 2007 1:25 pm
by Burrito
look for something written by onion2k in the GD forum.

if you're interested in doing it yourself, it's not too hard, you just need to make sure you have the GD library enabled on your installation of PHP.

Posted: Mon Apr 09, 2007 1:48 pm
by Benjamin
This should give you a good head start..

Code: Select all

function new_image_by_type($name, $extension)
{
    switch($extension)
    {
        case ".jpg":
            return imagecreatefromjpeg($name);
            break;
        case ".jpeg":
            return imagecreatefromjpeg($name);
            break;
        case ".gif":
            return imagecreatefromgif($name);
            break;
        case ".png":
            return imagecreatefrompng($name);
            break;
        default:
            return false;
    }
}

        // validate the file extension..
        $FILE_INFO      = pathinfo($_FILES['UPLOAD_IMAGE']['name']);
        $FILE_EXTENSION = '.' . strtolower($FILE_INFO["extension"]);
        $WHITE_LIST     = array('.jpg','.jpeg', '.gif', '.png');
        if (!in_array($FILE_EXTENSION, $WHITE_LIST))
        {
            throw new Exception('The uploaded file is not a valid image type.');
        }

        // enforce maximum image file size..
        if ($_FILES['UPLOAD_IMAGE']['size'] > 5242880)
        {
            throw new Exception('The image has been rejected due to a large file size.  Please resize and/or optimize the image and try again');
        }


        // verify upload is actually an image
        if (false === ($IMAGE_DATA = getimagesize($_FILES['UPLOAD_IMAGE']['tmp_name'])))
        {
            throw new Exception('The file uploaded is either corrupt or not a valid image file.  Please open the image in your favorite editor, save it as a jpg and try again.');
        }

        $ORIGINAL_WIDTH  = $IMAGE_DATA[0];
        $ORIGINAL_HEIGHT = $IMAGE_DATA[1];

            // BE SURE TO ADD A CHECK HERE TO VERIFY THAT THE IMAGE NEEDS TO BE RESIZED..
            // calculate the new image dimensions..
            if ($ORIGINAL_WIDTH > $ORIGINAL_HEIGHT)
            {
                $NEW_WIDTH  = floor((MAX_IMAGE_HEIGHT / $ORIGINAL_HEIGHT) * $ORIGINAL_WIDTH);
                $NEW_HEIGHT = MAX_IMAGE_HEIGHT;
            } else {
                $NEW_HEIGHT = floor((MAX_IMAGE_WIDTH / $ORIGINAL_WIDTH) * $ORIGINAL_HEIGHT);
                $NEW_WIDTH  = MAX_IMAGE_WIDTH;
            }

            // create a blank true color image..
            $NEW_IMAGE = imagecreatetruecolor($NEW_WIDTH, $NEW_HEIGHT);

            // create a new image based on the uploaded image..
            if (false === ($OLD_IMAGE = new_image_by_type($_FILES['UPLOAD_IMAGE']['tmp_name'], $FILE_EXTENSION)))
            {
                //ERROR
            }

            // resample and resize the image..
            imagecopyresampled($NEW_IMAGE, $OLD_IMAGE, 0, 0, 0, 0, $NEW_WIDTH, $NEW_HEIGHT, $ORIGINAL_WIDTH, $ORIGINAL_HEIGHT);

            // save image as a jpg and optimize the file size..
            imagejpeg($NEW_IMAGE, $FINAL_LOCATION, 85);

          // set file permissions..
          chmod($FINAL_LOCATION, 0644);