Skip to content

Instantly share code, notes, and snippets.

@danielme85
Created August 3, 2017 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielme85/5f899b36e29e7192762f8b406fc3997f to your computer and use it in GitHub Desktop.
Save danielme85/5f899b36e29e7192762f8b406fc3997f to your computer and use it in GitHub Desktop.
A Laravel Model that stores images and sends to TinyPNG API for optimization
<?php
namespace App\Models;
/**
* Created by PhpStorm.
* User: dmellum
* Date: 7/25/17
* Time: 6:04 PM
*/
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Log;
use Storage;
class Media extends Model
{
protected $table = 'media';
/**
* Save file based on form data and store attributes to this model.
*
* @param Request $request
* @return bool
*/
function saveImage(Request $request) {
$file = $request->file('file');
$this->original_filename = $file->getClientOriginalName();
$this->size = $file->getSize();
$this->path = $file->store('media/img/original', ['disk' => 'public']);
$this->filename = basename($this->path);
$this->type = 'image';
$this->mime_type = Storage::disk('public')->mimeType($this->path);
if ($this->path and $this->filename) {
if($this->save()) {
return true;
}
}
return false;
}
/**
* Send image to be optimized by TinyPNG, store result and set model attributes.
*
* @return bool
*/
function optimizeImage() {
\Tinify\setKey(env('tiny_key'));
$file = Storage::disk('public')->get($this->path);
if ($file) {
//optimize original size
try {
$newfile = \Tinify\fromBuffer($file);
$comp = $newfile->toBuffer();
if ($comp and $this->filename) {
$savepath = "media/img/optimized/$this->filename";
if(Storage::disk('public')->put($savepath, $comp)) {
$this->optimized_size = Storage::disk('public')->size($savepath);
$this->optimized_path = $savepath;
}
}
}catch (\Tinify\AccountException $e) {
Log::alert($e->getMessage());
return false;
}
//optimize to smaller
try {
$newfile = \Tinify\fromBuffer($file);
$resized = $newfile->resize(array(
"method" => "scale",
"width" => 400
));
$comp = $resized->toBuffer();
if ($comp and $this->filename) {
$savepath = "media/img/optimized/small/$this->filename";
if(Storage::disk('public')->put($savepath, $comp)) {
$this->optimized_small_size = Storage::disk('public')->size($savepath);
$this->optimized_small_path = $savepath;
if ($this->save()) {
return true;
}
}
}
}catch (\Tinify\AccountException $e) {
Log::alert($e->getMessage());
return false;
}
if ($this->save()) {
return true;
}
}
return false;
}
/**
* Get image, default to original if optimized not there
* @return string
*/
public function getImagePath() {
return Storage::disk('public')->url($this->optimized_path ?? $this->path);
}
/**
* Get small image, default to original if optimized not there
* @return string
*/
public function getSmallImagePath() {
return Storage::disk('public')->url($this->optimized_small_path ?? $this->optimized_path ?? $this->path);
}
/**
* File size mutator
* @param $size
* @return string
*/
public function getSizeAttribute($size) {
return $this->bytesToHuman($size);
}
/**
* File size mutator
* @param $size
* @return string
*/
public function getOptimizedSizeAttribute($size) {
return $this->bytesToHuman($size);
}
/**
* File size mutator
* @param $size
* @return string
*/
public function getOptimizedSmallSizeAttribute($size) {
return $this->bytesToHuman($size);
}
/**
* @param $bytes
* @return string
*/
private function bytesToHuman($bytes)
{
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
for ($i = 0; $bytes > 1024; $i++) {
$bytes /= 1024;
}
return round($bytes, 2) . ' ' . $units[$i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment