0

I've researched for the previous questions such as this for answers but I have no luck. My code is working fine, but it uploads the original file size and I want to compress the file size (e.g. from 600kb to at least 200kb) without changing the original height and width. I've tried the answers given from the same issue but I'm getting a thumbnail size instead (8.0kb) and the image uploaded becomes a plain black image.

Please help me how to do that using the image upload code I currently have. Many thanks in advance!

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

// this assumes that the upload form calls the form file field "file"
$name  = $_FILES["file"]["name"];
$type  = $_FILES["file"]["type"];
$size  = $_FILES["file"]["size"];
$tmp   = $_FILES["file"]["tmp_name"];
$error = $_FILES["file"]["error"];
$savepath = "gallery/";
$filelocation = $savepath.$name;
$newfilename = $savepath.$ign;

// Check image file dimensions first and then file size
list($width, $height) = getimagesize($tmp);

if($width > "800" || $height > "600") {
    echo "Error: Image size must be a maximum of 800 x 600 pixels.";
}
else if ($size > 500000) {
    echo "Error: Image file must be a maximum of 500KB only.";
}

// This won't upload if there was an error or if the file exists, hence the check
if (!file_exists($filelocation) && $error == 0) {
    $album = escape_sql(CleanUp($_POST['album']));
    $caption = escape_sql(CleanUp($_POST['caption']));
    $privacy = escape_sql(CleanUp($_POST['privacy']));

    // echo "The file $filename exists";
    // This will overwrite even if the file exists
    $temp = explode(".", $_FILES["file"]["name"]);
    $newfilename = "gallery/".$album."/".$ign.".".$extension;
    move_uploaded_file($tmp,$newfilename);

    date_default_timezone_set('Asia/Manila');
    $date = date('Y-m-d');
    $timestamp = strtotime($date);

    mysqli_query($connect, "INSERT INTO `gallery` (`id`,`ign`,`album`,`privacy`,`caption`,`filename`,`timestamp`) VALUES ('','$user','$album','$privacy','$caption','$newfilename','$date')");

    echo '<h1>My Photos &#10097; <i>Uploaded</i></h1>';
    echo '<p>Your photo has been uploaded successfully to the '.$album.' album.</p>';
}
else {
    unlink($filelocation);
    move_uploaded_file($tmp, $filelocation);

    echo '<h1>My Photos &#10097; <i>Error</i></h1>';
    echo '<p>An error has occurred and your photo was not uploaded to the gallery.';
}
Aki
  • 85
  • 1
  • 8

1 Answers1

0

ffmpeg usually works fine for me, even with default settings,

function compressImage(string $imageBinary):string{
$tmph=tmpfile();
$tmpf=stream_get_meta_data($tmph)['uri'];
file_put_contents($tmpf,$imageBinary);
system("ffmpeg -y -i ". escapeshellarg($tmpf). " " . escapeshellarg($tmpf.".jpg"));
$ret=file_get_contents($tmpf.".jpg");
fclose($tmph); // deletes the file, thanks to tmpfile()
unlink($tmpf.".jpg");
return $ret;
}
hanshenrik
  • 19,904
  • 4
  • 43
  • 89