G
G
ganbatte2019-08-08 10:55:03
PHP
ganbatte, 2019-08-08 10:55:03

How to automatically compress an image in php, in a local server. What funds exist?

The site will load images, in order to speed up the site, you need to implement automatic image compression.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton, 2019-08-08
@ganbatte

ImageMagick

A
Anton R., 2019-08-08
@anton_reut

There are libraries that resize and crop images, for example, I have this:

function imageResize($file_path, $new_width){
  
  //Получаем ширину и высоту исходника
  list($w, $h) = getimagesize($file_path);
  
  //Получаем коэфицент соотношения сторон
  $proportions = $h / $w;
  
  $new_w = $new_width;
  $new_h = $new_w * $proportions; // Получаем высоту уменьшенной картинки пропорционально новой ширине
  
  $thumb = imagecreatetruecolor($new_w, $new_h);
  
  $source = imagecreatefromjpeg($file_path);
  
  imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
  
  imagejpeg($thumb, $_FILES['image']['tmp_name']);
  return $_FILES['image']['tmp_name'];
  imagedestroy($thumb);
}

The script takes a picture from a temporary folder, reduces it and returns it there, and then we upload it to the right place after renaming it.

U
UksusoFF, 2017-12-18
@UksusoFF

You can try something like: https://github.com/bestmomo/nice-artisan

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question