0

How would you compress the image size before having it be uploaded to the folder. I have my code down below of what I have so far, how would i compress the image? Thanks!

$temp = explode(".", $_FILES["Image"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
echo $newfilename;

if (!!$_FILES['Image']['tmp_name']) {
    $info = explode('.', strtolower($_FILES['Image']['name'])); 
    if (in_array( end($info), $allow)) 
        if(move_uploaded_file( $_FILES['Image']['tmp_name'], $todir . $newfilename  ) )        {
          echo "success";
        }
    }
    else {
      echo "error";
    }

I found this code, but I'm not sure how I'd implement it with mine.

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);
  • PHP is server side, so the files have to be uploaded to server first, then PHP can manipulate the file. If you mean compress before move to upload folder: https://stackoverflow.com/questions/11418594/which-is-the-best-php-method-to-reduce-the-image-size-without-losing-quality – catcon Jul 10 '19 at 05:25
  • alternatively you can use [php intervention](http://image.intervention.io/) its easy to use you can compress,crop,filter etc. with this library. – danish-khan-I Jul 10 '19 at 05:28
  • @catcon the issue with that is i dont know how to implement that with how my code is set up. I need help understanding how to compress it and then insert it or vice versa – AustinVD1998 Jul 10 '19 at 07:02

1 Answers1

-1

Here you will get the answer of your question.

1) compress image without losing quality

2) PHP reference functions for compress - image GD lib

  • Please don't just post links. You should add the actual solution to the question in the answer itself and only add the links as reference. Also, if the first link (which is a SO link) answers the question, it should be closed as a duplicate, not answered. – M. Eriksson Jul 10 '19 at 05:38