codeawn/image_compress

支持回调的图片压缩

该包的规范仓库似乎已不存在,因此该包已被冻结。

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

从文件上传表单压缩

\Codeawn\image::uploadCompress($input, $args, $quality, $unlink);
  • $input : (string) 输入文件名属性,例如使用[]表示多个文件,例如 attribute[]

  • $args : 目标文件夹,包括 / 例如 upload/回调函数

  • $quality : (int) 压缩图片的质量

  • $unlink : (bool) 删除文件源

从字符串或数组压缩

\Codeawn\image::imageCompress($input, $args, $quality, $unlink);
  • $input : 文件名,包括路径或数组,例如 ["folder/file1.jpg","folder/file2.jpg"]处理多个文件

示例

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 .."; 
}