composer require codeawn/image_compress
\Codeawn\image::uploadCompress($input, $args, $quality, $unlink);
-
$input : (string) input file name attribute, use [] for multiple file e.g
attribute[]
-
$args : destination folder include
/
e.gupload/
orcallback function
-
$quality : (int) quality of compressed images
-
$unlink : (booelan) remove file source
\Codeawn\image::imageCompress($input, $args, $quality, $unlink);
- $input : file name include path or array e.g
["folder/file1.jpg","folder/file2.jpg"]
to process multiple file
HTML
<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="file"name="gambar"/>
<input type="submit"name="upload"value="upload">
</form>
PHP
if(isset($_POST['upload'])) {
try{ if(\Codeawn\image::uploadCompress(
"gambar", function ($file,$name) {
// backup original files
copy($file, "backup/".$name);
// show current progress
echo"processing : ".$name."<br>";
// build new name based date & time
$date = date("Ymd_his");
$fname = $date."_".$name;
// replace space with underscore
$fname = str_replace(" ","_",$fname);
// since compressed using imagejpeg function so rename non .jpg extension to .jpg
$fname = preg_replace('/\.(png|jpeg|gif)$/', '.jpg', $fname);
// return string filename with path
return "uploaded/".$fname;
// return false; for cancel process
}, 80, true
)===false){
throw new \Codeawn\FailException("upload failed","bad request!");
} else{
// this will show when no exception
echo"Success <br>";
}
}
catch(Codeawn\FailException $e) { echo $e->getTitle()," ",$e->getMessage();
}
} else{ echo"waiting upload ..";
}