Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post

    Resize uploaded image

    I'm using this script for multiple image upload, but I want to resize each images uploaded through the form so that the maximum height and width should be 500px, without loosing the proportions. Here is the script:

    PHP Code:
    while(list($key,$value) = each($_FILES['images']['name']))

            {

                if(!empty(
    $value))

                {

                    
    $filename $value;

                        
    $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line



                        
    while(file_exists('upimg/'.$filename))

      
    $filename=rand(1,9999).$filename;

    $add "upimg/$filename";  

                           
    //echo $_FILES['images']['type'][$key];

                     // echo "<br>";

                        
    copy($_FILES['images']['tmp_name'][$key], $add);

                        
    chmod("$add",0777);

                } 
    Can anyone please help?

    Thanks in advance

  • #2
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    Well theres no way to essentially change the properties of the image using php, but there is a way to test to make sure that what your uploading is within the ranges you want. So heres what i would do :

    PHP Code:
    list($width,$height) = getimagesize($_FILES['images']['tmp_name']);

    if(
    $width<500 && $height<500){
    // contiune upload process

    But if this is not what you want there is a way to take the image and create a new image from your old one with new image sizes, but this is long and unideal.

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Killermud View Post
    But if this is not what you want there is a way to take the image and create a new image from your old one with new image sizes, but this is long and unideal.
    Not ideal why? The method is used on a multitude of sites. It's a far better option than chucking grossly over-sized images down the wire.

  • #4
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    Well firstly because you need to load the image, test its image type, size, then adjust the file sizes, then you need to save the file, then output thats a lot to do rather than just testing the image size alone, much easier quicker and less strainous for the server.
    Last edited by Killermud; 02-21-2010 at 03:47 PM.

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Killermud View Post
    Well firstly because you need to load the image, test its image type, size, then adjust the file sizes, then you need to save the file, then output thats a lot to do rather than just testing the image size alone, much easier quicker and less strainous for the server.
    You resize each image exactly once, at the time when they are uploaded. Again, constantly serving a grossly over-sized image is far more detrimental on both server and client resources than a resize on upload.

  • #6
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    I am unaware of this method, all the methods i know of require you upload the image and then create a new image off this with your properties. Even the imagecopyresampled() function requires you to upload an image and create a new image for it.

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Killermud View Post
    I am unaware of this method, all the methods i know of require you upload the image and then create a new image off this with your properties. Even the imagecopyresampled() function requires you to upload an image and create a new image for it.
    Which is exactly what the O.P is wanting to do. Upload an image, (or images), and have them resized to around 500px or so whilst retaining the width/height ratio.

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Resize function:

    PHP Code:
    function image_resize($sourcefile$width$height$delete false)
    {
            
    $ext strtolower(substr(strrchr($sourcefile'.'), 1));
            
    $extensions = array('gif''jpg''jpeg''png');

            if (empty(
    $ext) || !in_array($ext$extensions))
            {
                    return 
    false;
            }

            list(
    $currwidth$currheight$type) = getimagesize($sourcefile);

            if (empty(
    $currwidth) || empty($currheight) || $type 3)
            {
                    return 
    false;
            }

            
    $ratio = ($currwidth/$currheight);

            if (
    $currwidth <= $width && $currheight <= $height)
            {
                    if (
    is_file($sourcefile))
                    {
                            
    copy($sourcefile$sourcefile.'_new');
                            return 
    $sourcefile.'_new';
                    }
                    return 
    false;
            }
            else
            {
                    
    $outputfile $sourcefile.time();

                    if ((
    $width/$height) > $ratio)
                    {
                            
    $width = ($height*$ratio);
                    }
                    else
                    {
                            
    $height = ($width/$ratio);
                    }

                    
    $dimg imagecreatetruecolor($width$height);
                    
    $background imagecolorallocate($dimg255255255);
                    
    imagefill($dimg00$background);

                    if (
    $type == 1)
                    {
                            
    $simg imagecreatefromgif($sourcefile);
                            
    imagecopyresampled($dimg$simg0000$width$height$currwidth$currheight);
                            
    imagegif($dimg$outputfile);
                    }
                    else if (
    $type == 2)
                    {
                            
    $simg imagecreatefromjpeg($sourcefile);
                            
    imagecopyresampled($dimg$simg0000$width$height$currwidth$currheight);
                            
    imagejpeg($dimg$outputfile85);
                    }
                    else if (
    $type == 3)
                    {
                            
    $simg imagecreatefrompng($sourcefile);
                            
    imagealphablending($simgtrue);
                            
    imagesavealpha($simgtrue);
                            
    imagecopyresampled($dimg$simg0000$width$height$currwidth$currheight);
                            
    imagepng($dimg$outputfile9);
                    }
                    
    imagedestroy($dimg);
                    
    imagedestroy($simg);

                    if (
    is_file($outputfile))
                    {
                            if (
    $delete)
                            {
                                    
    unlink($sourcefile);
                            }
                            return 
    $outputfile;
                    }
                    return 
    false;
            }

    Last edited by Inigoesdr; 02-21-2010 at 05:21 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •