SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Deal with image uploads in PHP, using the GD library?

    Hey everyone,

    I'm aiming to build a script that does the following:

    1) User submits image via web form. This image could be anywhere between 500kb and 2mb in size.

    2) Resizes the image so that it is proportionally 500px wide (or if it was less than 500px wide already, leave it alone). Save this in the images directory.

    3) Save another copy of the image in the images/thumbs directory, which is to be resized to 180px wide.

    Is there an easy way to do this? I can do the image uploads part, I just don't get how you can resize the image, save it, resize again, save it. Is this even possible?


    Thanks for your time, help is appreciated!!

    Richard

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the image resizing part this might be hulpful. Not exactly nice code, but it works decently:
    PHP Code:
            function ImageCreateFromType ($filename)
            {
                
    $ext explode("."$filename);
                switch ( 
    strtoupper$ext[count($ext) - 1] ) ){
                    case 
    "JPG":
                        
    $resc imagecreatefromjpeg($filename);
                        break;
                    case 
    "JPEG":
                        
    $resc imagecreatefromjpeg($filename);
                        break;
                    case 
    "GIF":
                        
    $resc imagecreatefromgif($filename);
                        break;
                    case 
    "PNG":
                        
    $resc imagecreatefrompng($filename);
                        break;
                }
                return 
    $resc;
            }
            
            
    /**
             *    Resize an image to a specified size
             *
             *    @param string $filename                The path to the file
             *    @param integer $width                The new width
             *    @param integer $height                The new height
             *    @param boolean $autoFit                Fit the image to a dynamic width or a dynamic height, which is smallest
             *    @param boolean $fixedSize            Fixed width and height, image is scaled to fit
             *    @param string $align                Set the horizontal aligning to left, center or right
             *    @param string $valign                Set the vertical aligning to top, middle or bottom
             */
            
    function Resize ($filename$width$height$autoFit=false$fixedSize=false$align="center"$valign="middle")
            {
                
    $filename realpath($filename);
                list(
    $sourceWidth$sourceHeight) = getimagesize($filename);
                
    $input ImageCreateFromType($filename);
                
    $destinationX $destinationY 0;
                
    $destinationWidth $destinationHeight 0;
                
                
    /**
                 *    FixedSize means that the provided width and height are respected, no matter what
                 *    AutoFit means that the source image is scaled to fit specific measurements
                 */
                
    if ( $fixedSize ){
                    if ( 
    $autoFit ){
                        
    #    For not having white borders, fit to the smallest width or height
                        
    if ( ($width/$sourceWidth)/($height/$sourceHeight) < ){
                            
    $destinationWidth = ( $height $sourceHeight ) * $sourceWidth;
                            
    $destinationHeight $height;
                            switch ( 
    strtolower($align) ){
                                case 
    'left':
                                    
    $destinationX 0;
                                    break;
                                case 
    'center':
                                    
    $destinationX floor( ( $width $destinationWidth ) / );
                                    break;
                                case 
    'right':
                                    
    $destinationX $width $destinationWidth;
                                    break;
                                default:
                                    
    $destinationX floor( ( $width $destinationWidth ) / );
                                    break;
                            }
                            
    $destinationY 0;
                        }
                        else {
                            
    $destinationWidth $width;
                            
    $destinationHeight = ( $width $sourceWidth ) * $sourceHeight;
                            
    $destinationX 0;
                            switch ( 
    strtolower($valign) ){
                                case 
    'top':
                                    
    $destinationY 0;
                                    break;
                                case 
    'middle':
                                    
    $destinationY floor ( ( $height $destinationHeight ) / );
                                    break;
                                case 
    "bottom":
                                    
    $destinationY $height $destinationHeight;
                                    break;
                                default:
                                    
    $destinationY floor ( ( $height $destinationHeight ) / );
                                    break;
                            }
                        }
                    }
                    
    #    Stretch the image to fit to the provided size
                    
    else {
                        
    $destinationWidth $width;
                        
    $destinationHeight $height;
                        
    $destinationX 0;
                        
    $destinationY 0;
                    }
                }
                else {
                    
    #    Scale the output image to the maximum provided size
                    
    if ( $autoFit ){
                        if ( (
    $width/$sourceWidth)/($height/$sourceHeight) < ){
                            
    $destinationWidth $width;
                            
    $destinationHeight $height = ( $destinationWidth $sourceWidth ) * $sourceHeight;
                            
    $destinationX 0;
                            
    $destinationY 0;
                        }
                        else {
                            
    $destinationHeight $height;
                            
    $destinationWidth $width = ( $destinationHeight $sourceHeight) * $sourceWidth;
                            
    $destinationX 0;
                            
    $destinationY 0;
                        }
                    }
                    
    #    Create an image with a fixed size, fit the entire source image in it, and fill the unused background
                    
    else {
                        if ( (
    $width/$sourceWidth)/($height/$sourceHeight) < ){
                            
    $destinationWidth $width;
                            
    $destinationHeight = ( $destinationWidth $sourceWidth ) * $sourceHeight;
                            
    $destinationX 0;
                            switch ( 
    strtolower($valign) ){
                                case 
    'top':
                                    
    $destinationY 0;
                                    break;
                                case 
    'middle':
                                    
    $destinationY floor ( ($height $destinationHeight) / );
                                    break;
                                case 
    'bottom':
                                    
    $destinationY $height $destinationHeight;
                                    break;
                                default:
                                    
    $destinationY floor ( ($height $destinationHeight) / );
                                    break;
                            }
                        }
                        else {
                            
    $destinationHeight $height;
                            
    $destinationWidth = ( $destinationHeight $sourceHeight) * $sourceWidth;
                            switch ( 
    strtolower($align) ){
                                case 
    'left':
                                    
    $destinationX 0;
                                    break;
                                case 
    'center':
                                    
    $destinationX floor ( ($width $destinationWidth) / );
                                    break;
                                case 
    'right':
                                    
    $destinationX $width $destinationWidth;
                                    break;
                                default:
                                    
    $destinationX floor ( ($width $destinationWidth) / );
                                    break;
                            }
                            
    $destinationY 0;
                        }
                    }
                }
                            
                
    $output imagecreatetruecolor ($width$height);
                
    imagecopyresampled $output$input$destinationX$destinationY00$destinationWidth$destinationHeight$sourceWidth$sourceHeight);
                
    imagejpeg($output$filename100);
            } 
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Enthusiast mrsmiley's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't resize the image, copy it, then resize it again. It will wreck the image output quality no end.

    Conceptually that's what happens within GD, but its because its resizing an in memory copy of the image. So it maintains the output quality. I usually upload the file to a temporary name, use that to create two resized versions, then delete the temp copy

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See my simple and shortest class to upload images and resize/create thumbnail.
    PHP Code:
    class ImageUpload{
        
        var 
    $ArrImageType;
        var 
    $picpath;
        var 
    $thumbpath;
        var 
    $maxwidth;
        var 
    $thumbwidth;
        var 
    $thumbheight;
        
        function 
    __construct($picpath$maxwidth$thumbwidth 100$thumbheight 100$thumbpath ""){
            
    $this->maxwidth $maxwidth;
            
    $this->thumbwidth $thumbwidth;
            
    $this->thumbheight $thumbheight;
            
    $this->picpath $picpath;
            
    $this->thumbpath $thumbpath;
            
    $this->ArrImageType = array('image/gif''image/jpeg''image/jpg''image/pjpeg''image/png');
        }
        function 
    Create_ThumbNail($ImageType$rW$rH$imagename$do="thumbnail"){
            
    $full_picmain $this->picpath "/" $imagename;
            if(
    $do != "thumbnail")
                
    $tsrc $this->picpath "/$imagename";
            else
                
    $tsrc $this->thumbpath "/$imagename";
            
    # Starting of GIF thumb nail creation #
            
    if($ImageType == "image/gif"){
                
    $im imagecreatefromgif($full_picmain);
                
    $width imagesx($im);                # Original picture width is stored
                
    $height imagesy($im);               # Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
                
    $newimage imagecreatetruecolor($n_width,$n_height);
                
    imagecopyresized($newimage$im0000$n_width$n_height$width,$height);
                if(
    function_exists("imagegif")) {
                    
    header("Content-type: image/gif");
                    
    imagegif($newimage$tsrc);
                }
                elseif(
    function_exists("imagejpeg")) {
                    
    header("Content-type: image/jpeg");
                    
    imagejpeg($newimage$tsrc);
                }
                
    chmod("$tsrc"0777);
            }
            
    # starting of JPG thumb nail creation #
            
    if($ImageType == "image/pjpeg" || $ImageType == "image/jpeg" || $ImageType == "image/jpg"){
                
    $im imagecreatefromjpeg($full_picmain);
                
    $width imagesx($im);              # Original picture width is stored
                
    $height imagesy($im);             # Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
            
                
    $newimage imagecreatetruecolor($n_width,$n_height);                 
                
    imagecopyresized($newimage$im0000$n_width$n_height$width$height);
                
    imagejpeg($newimage$tsrc);
                
    chmod("$tsrc"0777);
            }
            
    # starting of JPG thumb nail creation #
            
    if($ImageType == "image/png"){
                
    $im imagecreatefrompng($full_picmain); 
                
    $width imagesx($im);              # Original picture width is stored
                
    $height imagesy($im);             # Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
    $newimage imagecreatetruecolor($n_width,$n_height);                 
                
    imagecopyresized($newimage$im0000$n_width$n_height$width$height);
                
    imagepng($newimage$tsrc);
                
    chmod("$tsrc"0777);
            }        
        }
        function 
    Upload($PicFile){
            if(
    is_uploaded_file($PicFile['tmp_name'])){
                if(
    $PicFile['size'] < 1048576 && in_array($PicFile['type'], $this->ArrImageType)){
                    
    $imgExt $this->GetImageExtention($PicFile['type']);
                    
    $PicName substr(md5(time()), rand(026), 6) . "." $imgExt;
                    
    $PicFullPathName $this->picpath "/" $PicName;
                    if(
    move_uploaded_file($PicFile['tmp_name'], $PicFullPathName)){
                        
    $this->Create_ThumbNail($PicFile['type'], 10080$PicName"thumbnail"); # Create thumbnail image 
                        
    list($width$height) = getimagesize($PicFullPathName);
                        if(
    $width $this->maxwidth){ # default size.
                            
    $ratio = (($width $this->maxwidth) * 100) / $width;
                            
    $newheight $height - ($ratio $height 100);
                            
    $this->Create_ThumbNail($PicFile['type'], $this->maxwidth$newheight$PicName"resize"); # resizing the image
                        
    }
                        
    mysql_query("UPDATE tblproductimages SET ProductImagePath='$PicName' WHERE PictureID='$newpicid'") or die(mysql_error());
                    }
                }
            }
    # is picture uploaded
        
    }
        
    # Get image extention #
        
    function GetImageExtention($type){
            if(
    $type == "image/jpeg" || $type == "image/jpg" || $type == "image/pjpeg")
                return 
    "jpg";
            elseif(
    $type "image/png")
                return 
    "png";
            elseif(
    $type == "image/gif")
                return 
    "gif";
        }
    }
    $picpath './uploads';
    $thumbpath './uploads/thumbnails';
    $maxwidth 600;
    $thumbwidth 100;
    $thumbheight 100;

    $obj = new ImageUpload($picpath$maxwidth$thumbwidth$thumbheight$thumbpath);
    $PicFile $_FILES['PrdImg'];
    $obj->Upload($PicFile); 
    I think I dont have to describe about how to use this. If you did not understand, please let me know.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    See my simple and shortest class to upload images and resize/create thumbnail.
    PHP Code:
    class ImageUpload{
        
        var 
    $ArrImageType;
        var 
    $picpath;
        var 
    $thumbpath;
        var 
    $maxwidth;
        var 
    $thumbwidth;
        var 
    $thumbheight;
        
        function 
    __construct($picpath$maxwidth$thumbwidth 100$thumbheight 100$thumbpath ""){
            
    $this->maxwidth $maxwidth;
            
    $this->thumbwidth $thumbwidth;
            
    $this->thumbheight $thumbheight;
            
    $this->picpath $picpath;
            
    $this->thumbpath $thumbpath;
            
    $this->ArrImageType = array('image/gif''image/jpeg''image/jpg''image/pjpeg''image/png');
        }
        function 
    Create_ThumbNail($ImageType$rW$rH$imagename$do="thumbnail"){
            
    $full_picmain $this->picpath "/" $imagename;
            if(
    $do != "thumbnail")
                
    $tsrc $this->picpath "/$imagename";
            else
                
    $tsrc $this->thumbpath "/$imagename";
            
    # Starting of GIF thumb nail creation #
            
    if($ImageType == "image/gif"){
                
    $im imagecreatefromgif($full_picmain);
                
    $width imagesx($im);                # Original picture width is stored
                
    $height imagesy($im);               # Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
                
    $newimage imagecreatetruecolor($n_width,$n_height);
                
    imagecopyresized($newimage$im0000$n_width$n_height$width,$height);
                if(
    function_exists("imagegif")) {
                    
    header("Content-type: image/gif");
                    
    imagegif($newimage$tsrc);
                }
                elseif(
    function_exists("imagejpeg")) {
                    
    header("Content-type: image/jpeg");
                    
    imagejpeg($newimage$tsrc);
                }
                
    chmod("$tsrc"0777);
            }
            
    # starting of JPG thumb nail creation #
            
    if($ImageType == "image/pjpeg" || $ImageType == "image/jpeg" || $ImageType == "image/jpg"){
                
    $im imagecreatefromjpeg($full_picmain);
                
    $width imagesx($im);              # Original picture width is stored
                
    $height imagesy($im);             # Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
            
                
    $newimage imagecreatetruecolor($n_width,$n_height);                 
                
    imagecopyresized($newimage$im0000$n_width$n_height$width$height);
                
    imagejpeg($newimage$tsrc);
                
    chmod("$tsrc"0777);
            }
            
    # starting of JPG thumb nail creation #
            
    if($ImageType == "image/png"){
                
    $im imagecreatefrompng($full_picmain); 
                
    $width imagesx($im);              # Original picture width is stored
                
    $height imagesy($im);             # Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
    $newimage imagecreatetruecolor($n_width,$n_height);                 
                
    imagecopyresized($newimage$im0000$n_width$n_height$width$height);
                
    imagepng($newimage$tsrc);
                
    chmod("$tsrc"0777);
            }        
        }
        function 
    Upload($PicFile){
            if(
    is_uploaded_file($PicFile['tmp_name'])){
                if(
    $PicFile['size'] < 1048576 && in_array($PicFile['type'], $this->ArrImageType)){
                    
    $imgExt $this->GetImageExtention($PicFile['type']);
                    
    $PicName substr(md5(time()), rand(026), 6) . "." $imgExt;
                    
    $PicFullPathName $this->picpath "/" $PicName;
                    if(
    move_uploaded_file($PicFile['tmp_name'], $PicFullPathName)){
                        
    $this->Create_ThumbNail($PicFile['type'], 10080$PicName"thumbnail"); # Create thumbnail image 
                        
    list($width$height) = getimagesize($PicFullPathName);
                        if(
    $width $this->maxwidth){ # default size.
                            
    $ratio = (($width $this->maxwidth) * 100) / $width;
                            
    $newheight $height - ($ratio $height 100);
                            
    $this->Create_ThumbNail($PicFile['type'], $this->maxwidth$newheight$PicName"resize"); # resizing the image
                        
    }
                        
    mysql_query("UPDATE tblproductimages SET ProductImagePath='$PicName' WHERE PictureID='$newpicid'") or die(mysql_error());
                    }
                }
            }
    # is picture uploaded
        
    }
        
    # Get image extention #
        
    function GetImageExtention($type){
            if(
    $type == "image/jpeg" || $type == "image/jpg" || $type == "image/pjpeg")
                return 
    "jpg";
            elseif(
    $type "image/png")
                return 
    "png";
            elseif(
    $type == "image/gif")
                return 
    "gif";
        }
    }
    $picpath './uploads';
    $thumbpath './uploads/thumbnails';
    $maxwidth 600;
    $thumbwidth 100;
    $thumbheight 100;

    $obj = new ImageUpload($picpath$maxwidth$thumbwidth$thumbheight$thumbpath);
    $PicFile $_FILES['PrdImg'];
    $obj->Upload($PicFile); 
    I think I dont have to describe about how to use this. If you did not understand, please let me know.
    Looks great! Although, how would I go about resizing the main image too?

    Thanks
    Richard

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For you purpose you can make the changes on the Upload() function of the class. Just call the resize link not the thumbnails like:
    PHP Code:
    $this->Create_ThumbNail($PicFile['type'], 10080$PicName"thumbnail"); # Create thumbnail image 
    # instead of this statement you can just call like this:
    list($width$height) = getimagesize($PicFullPathName);

                        if(
    $width $this->maxwidth){ # default size.

                            
    $ratio = (($width $this->maxwidth) * 100) / $width;

                            
    $newheight $height - ($ratio $height 100);

                            
    $this->Create_ThumbNail($PicFile['type'], $this->maxwidth$newheight$PicName"resize"); # resizing the image

                        

    That means if the uploaded is image is larger than your maximum size, then call this function to resize.

    Does that make sense?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

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