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.

3.0.0 2017-08-22 20:05 UTC

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.g upload/ or callback 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 .."; 
}