PHP-使用我的代码减小图像大小

时间:2018-07-05 23:08:54

标签: php

我已经创建了用于上传图片文件的代码。

但是我需要减少它们的兆字节数……我该怎么做?

有人可以解释一下并帮助我创建一个好的压缩代码吗?

               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 个答案:

答案 0 :(得分:0)

您可以在PHP中遵循此代码进行压缩。 getimagesize()函数用于查找任何给定图像文件的大小,并返回尺寸和文件类型。

 <?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);