Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to resize images from one folder to new desitnation?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 616
    Comment on it

    Hello Reader's, 
    If you have a folder full of larger images and you want to resize them into a new folder then PHP offers you to do this in a quick and easy programming.
    Suppose you have uploaded the website images in upload folder and you want to create a thumbnail folder from those images then follow the steps as below:-

    Step1: Create a function by name createThumb

    function createThumb()
        {
          //Maximize script execution time
      ini_set('max_execution_time', 0);
    
      //Initial settings, Just specify Source and Destination Image folder.
      $ImagesDirectory    = 'assets/upload/'; //Source Image Directory End with Slash
      $DestImagesDirectory    = 'assets/thumb/'; //Destination Image Directory End with Slash
      $NewImageWidth      = 350; //New Width of Image
      $NewImageHeight     = 327; // New Height of Image
      $Quality        = 50; //Image Quality
    
      //Open Source Image directory, loop through each Image and resize it.
      if($dir = opendir($ImagesDirectory)){
          while(($file = readdir($dir))!== false){
    
              $imagePath = $ImagesDirectory.$file;
              $destPath = $DestImagesDirectory.$file;
              $checkValidImage = @getimagesize($imagePath);
    
              if(file_exists($imagePath) && $checkValidImage) //Continue only if 2 given parameters are true
              {
                  //Image looks valid, resize.
                  if($this->resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
                  {
                      echo $file.' resize Success!<br />';
                      /*
                      Now Image is resized, may be save information in database?
                      */
    
                  }else{
                      echo $file.' resize Failed!<br />';
                  }
              }
          }
          closedir($dir);
      }
    }
    
      

    In the code above you can see the variable as below

     $ImagesDirectory    = 'assets/upload/';

     This is to assign the source address folder from where your images will be and in code below you need to assign the new folder address where the output of thumbnail will upload.

    Now you have to create the new function for save the thumbnail from the images one by one and it's code will go like this:-

    function createThumb()
        {
          //Maximize script execution time
      ini_set('max_execution_time', 0);
    
      //Initial settings, Just specify Source and Destination Image folder.
      $ImagesDirectory    = 'assets/snooptravel_places/'; //Source Image Directory End with Slash
      $DestImagesDirectory    = 'assets/snooptravel_places/thumb/'; //Destination Image Directory End with Slash
      $NewImageWidth      = 350; //New Width of Image
      $NewImageHeight     = 327; // New Height of Image
      $Quality        = 50; //Image Quality
    
      //Open Source Image directory, loop through each Image and resize it.
      if($dir = opendir($ImagesDirectory)){
          while(($file = readdir($dir))!== false){
    
              $imagePath = $ImagesDirectory.$file;
              $destPath = $DestImagesDirectory.$file;
              $checkValidImage = @getimagesize($imagePath);
    
              if(file_exists($imagePath) && $checkValidImage) //Continue only if 2 given parameters are true
              {
                  //Image looks valid, resize.
                  if($this->resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
                  {
                      echo $file.' resize Success!<br />';
                      /*
                      Now Image is resized, may be save information in database?
                      */
    
                  }else{
                      echo $file.' resize Failed!<br />';
                  }
              }
          }
          closedir($dir);
      }
    }
    
        //Function that resizes image.
        public function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
        {
            list($iWidth,$iHeight,$type)    = getimagesize($SrcImage);
            $ImageScale             = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
            $NewWidth               = ceil($ImageScale*$iWidth);
            $NewHeight              = ceil($ImageScale*$iHeight);
            $NewCanves              = imagecreatetruecolor($NewWidth, $NewHeight);
    
            switch(strtolower(image_type_to_mime_type($type)))
            {
                case 'image/jpeg':
                case 'image/png':
                case 'image/gif':
                case 'image/JPG':
                case 'image/JPEG':
                    $NewImage = imagecreatefromjpeg($SrcImage);
                    break;
                default:
                    return false;
            }
    
            // Resize Image
            if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
            {
                // copy file
                if(imagejpeg($NewCanves,$DestImage,$Quality))
                {
                    imagedestroy($NewCanves);
                    return true;
                }
            }
        }              

    Now when you run this code on server, the page will start print the name of files which had been converted by PHP server. 

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: