SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Resizing uploaded images

    I have a submit form where users can upload images. What I want is to resize the images so the width will be no more than 300px and to save the resized image ( I don't want to keep the original).

    How would I do that?

  2. #2
    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 this simple class which resizes the image for you as well as it creates 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');#,'application/msword','application/pdf');
        
    }
        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); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I will try it out


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
  •