codeawn / image_compress
Compress image with callback support
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 2 114
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.3.0
- ext-gd: *
Requires (Dev)
- psy/psysh: 0.7.*
This package is auto-updated.
Last update: 2021-01-27 12:19:18 UTC
README
composer require codeawn/image_compress
Compress from file upload form
\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
Compress from string or array
\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
Example
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 .."; }