0

So, I have the following image compression function but I am not sure if it is doing what it is supposed to do.

It is in php file:

function compress_image($src, $dest , $quality) 
{
$upload_dir = wp_upload_dir();
$info = getimagesize($src);
if ($info['mime'] == 'image/jpeg') 
{
    $image = imagecreatefromjpeg($src);
}
elseif ($info['mime'] == 'image/gif') 
{
    $image = imagecreatefromgif($src);
}
elseif ($info['mime'] == 'image/png') 
{
    $image = imagecreatefrompng($src);
}
else
{
    die('Unknown image file format');
}
if (!file_exists($upload_dir['path'] . '/compress')) {
    mkdir($upload_dir['path'] . '/compress', 0777, true);
}     
imagejpeg($image, $dest, $quality);
}

Is it correctly written?

The result "compressed" image seems to be small (ex, 2MB to 500kb), but I am not sure if the compression is done on the client side (on their phone or computer while being uploaded but before uploaded to the server), or on the server side.

My goal is to compress the image before being uploaded so it is quicker in uploading process.

What do you think?

Steve Kim
  • 4,396
  • 8
  • 39
  • 82
  • If your looking to compress the image before you upload then this wont work as PHP is a server side scripting language, you will need to use something client side to compress the image and then upload it. – llanato Nov 20 '15 at 22:55
  • Yeah, that's what I was thinking and something was not right. :P Is it complicated to compress the image on client side on js? – Steve Kim Nov 20 '15 at 22:56
  • 2
    Have a look at here it might help you, http://stackoverflow.com/questions/14672746/how-to-compress-an-image-via-javascript-in-the-browser – llanato Nov 20 '15 at 23:00

0 Answers0