Re: function to resize images

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Monday 28 February 2005 10:34 am, Bosky, Dave wrote:
> Does anyone have a nice function that will resize an uploaded image to
> specific width/height dimensions?
>
> I wanted to find something that would work for only GIF and JPG image
> types. I've converted my shopping cart
>
> application from Cold Fusion to PHP and need to create thumbnails and
> reduce the size of large uploaded images.
>
>
>
> I've already done a google search and found an example but can't get it to
> work for some reason and it includes a
>
> bunch of other stuff that's not needed and too much trouble to remove.
>
>
>
>
>
> Thanks,
>
> Dave
>


This one works for me: currently I have a max of 600kb on upload which is 
reduced to 10% (60kb). Assumes you have a 'pix' directory where you load 
jpegs etc:


if ($_FILES["pix"]["type"] == "image/jpeg")
        {
                $pix_file = $_FILES["pix"]["name"];
                $pix_temp_file = $_FILES["pix"]["tmp_name"];

        /* Rename file with id and 'U' for upload */

                $new_pix_file = "U{$_SESSION['rid']}-{$_SESSION['sid']}.jpg";


                if(is_uploaded_file($pix_temp_file)){
                        move_uploaded_file($pix_temp_file, 
"/your_site/web/pix/{$_SESSION['rid']}-{$_SESSION['sid']}.jpg");
                }

                $original = 
"/your_site/pix/{$_SESSION['rid']}-{$_SESSION['sid']}.jpg";

                /* Resize the uploaded picture */

                $dest_h = "190";
                $dest_w = "117";

                // create the blank limited-palette image
                $base_image = imageCreate($dest_w, $dest_h);

                move_uploaded_file($base_image, "/your_site/pix/convert.jpg");

                // convert and save it to temp.jpg
                imagejpeg($base_image, '/your_site/pix/convert.jpg');



                // get the image pointer to the temp jpeg
                $image = imageCreateFromJpeg('/your_site/pix/convert.jpg');

                // get the image pointer to the original image
                $imageToResize = 
imageCreateFromJpeg("/your_site/pix/{$_SESSION['rid']}-{$_SESSION['sid']}.jpg");
                        $src_w = imagesx($imageToResize);   // Current Image 
Width
                        $src_h = imagesy($imageToResize);   // Current Image 
Height

                // resize the original image over temp.jpg
                // since you have GD2, you could also use imageCopyResampled
                imageCopyResized($image, $imageToResize, 0, 0, 0, 0, $dest_w, 
$dest_h,
                $src_w, $src_h);

                // values for output jpeg quality
                $jpegQuality = 75;

                // create the resized image
                imageJpeg($image,                       
"/your_site/pix/{$_SESSION['rid']}-{$_SESSION['sid']}.jpg", $jpegQuality);

                // cleanup temp files
        }
}

Hth,
Andre

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux