Note that there are some explanatory texts on larger screens.

plurals
  1. POcompressing image filesize on multiple file upload
    primarykey
    data
    text
    <p>I am using the following as the html input:</p> <pre><code>&lt;input type="file" name="files[]" multiple="multiple" accept="image/*"&gt; </code></pre> <p>then this to upload multiple instances of the above input and attempt to compress by changing the image quality after each file is checked for errors:</p> <pre><code>$valid_formats = array("jpg", "png", "gif", "zip", "bmp"); $max_file_size = 1024*300; //300 kb $path = "uploads/"; // Upload directory $count = 0; if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ // Loop $_FILES to execute all files foreach ($_FILES['files']['name'] as $f =&gt; $name) { if ($_FILES['files']['error'][$f] == 4) { // Compress the image files function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url); // save file imagejpeg($image, $destination_url, $quality); // return destination file return $destination_url; } compress_image($_FILES['files']['name'], NULL, 90); continue; // Skip file if any error found } if ($_FILES['files']['error'][$f] == 0) { if ($_FILES['files']['size'][$f] &gt; $max_file_size) { $message[] = "$name is too large!."; continue; // Skip large files } elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){ $message[] = "$name is not a valid format"; continue; // Skip invalid file formats } else{ // No error found! Move uploaded files if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) { $count++; // Number of successfully uploaded files } } } } } </code></pre> <p>The files are uploaded just fine, however the compression doesn't take place. How can I compress the image filesize upon upload or each file?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload