-2

I've created a code to upload my image file.

But i need to reduce their MBytes… How can i do it?

Can someone explain to be and help me to create a good compression code?

               if(!empty($ads_file)) {

               $files = $_FILES['img_file'];
               $allowed = array('jpg', 'jpeg');

               foreach($files['name'] as $position => $file_name) {

                   $file_tmp = $files['tmp_name'][$position];
                   $file_size = $files['size'][$position];
                   $file_error = $files['error'][$position];

                   $file_ext = explode('.', $file_name);
                   $file_ext = strtolower(end($file_ext));

                   if(in_array($file_ext, $allowed)) {

                        if($file_error === 0) {

                           if($file_size <= 4097152) {

                               $file_name_new = uniqid('', true) . '.' . $file_ext;
                               $file_destination = 'im/pd/'. $file_name_new;

                               $colBanco=3;
                               $j=0;                                   

                               if ($j<$colBanco){
                                  $praBanco .="'".$file_destination."',";

                                  if(!empty($errors)) { 
                                        //don't sent to folder if there is errors.
                                  } else {
                                      move_uploaded_file($file_tmp, $file_destination);  
                                  }                                         
                               }

                               $j++;

                           } else {
                              $errors[] = "
                              <div class='alert warning'>
                                  <span class='closebtn'>&times;</span>  
                                  <strong><i class='fas fa-file-excel'></i></strong> Esta imagem é demasiado grande.
                              </div>";                               
                           }

                        } else {
                              $errors[] = "
                              <div class='alert warning'>
                                  <span class='closebtn'>&times;</span>  
                                  <strong><i class='fas fa-plug'></i></strong> Falha ao efetuar o upload. Tente novamente...
                              </div>";   
                        }

                   } else {
                         $errors[] = "
                         <div class='alert warning'>
                              <span class='closebtn'>&times;</span>  
                              <strong><i class='fas fa-file-excel'></i></strong> Ficheiro inv&aacute;lido, tente JPG/JPEG.
                         </div>";  
                   }
               }

          } 

1 Answers1

0

You can follow this code in PHP to do compression. The getimagesize() function is used to find the size of any given image file and return the dimensions along with the file type.

 <?php 
  function compress($source, $destination, $quality) {

    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg') 
      $image = imagecreatefromjpeg($source);

    elseif ($info['mime'] == 'image/gif') 
      $image = imagecreatefromgif($source);

    elseif ($info['mime'] == 'image/png') 
      $image = imagecreatefrompng($source);

    imagejpeg($image, $destination, $quality);

    return $destination;
 }

 $source_img = 'source.jpg';
 $destination_img = 'destination .jpg';

 $d = compress($source_img, $destination_img, 90);
Muhammad Usman
  • 1,403
  • 13
  • 24