Skip to content

Instantly share code, notes, and snippets.

@emsifa
Last active September 13, 2015 14:27
Show Gist options
  • Save emsifa/e0288d0f66317741aab6 to your computer and use it in GitHub Desktop.
Save emsifa/e0288d0f66317741aab6 to your computer and use it in GitHub Desktop.
Trait ImageManager, untuk generate image, serta mengambil url dan path dari gambar tsb
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mahasiswa;
class ContohController extends Controller
{
public function createMahasiswa(Request $request)
{
$path_uploaded_photo = $request->file('photo')->getRealPath();
// generate image
$photo = Mahasiswa::makeImagePhoto($path_uploaded_photo);
$thumb = Mahasiswa::makeImageThumb($path_uploaded_photo);
$data = $request->all();
$data['photo'] = $photo->basename;
$data['thumb'] = $photo->basename;
$mahasiswa = Mahasiswa::create($data);
// now you can get url image like this
$url_photo = $mahasiswa->urlImagePhoto();
$url_thumb = $mahasiswa->urlImageThumb();
// or getting filepath
$path_photo = $mahasiswa->pathImagePhoto();
$path_thumb = $mahasiswa->pathImageThumb();
// delete images?
$mahasiswa->deleteImagePhoto();
$mahasiswa->deleteImageThumb();
return "easy right?";
}
}
<?php namespace App\Traits;
trait ImageManager {
protected static $image_default_options = [
'location' => 'img',
'width' => null,
'height' => null,
'fit' => false,
'keep_aspect_ratio' => true,
'prevent_upsize' => true,
'filename' => null,
];
public function __call($method, $params)
{
if(preg_match('/^urlImage/', $method)) {
$key = preg_replace('/^urlImage/', '', $method);
$key = snake_case($key);
$settings = static::getImageSetting($key);
return asset($settings['location'].'/'.$this->{$key});
} elseif(preg_match('/^pathImage/', $method)) {
$key = preg_replace('/^pathImage/', '', $method);
$key = snake_case($key);
$settings = static::getImageSetting($key);
return public_path($settings['location'].'/'.$this->{$key});
} elseif(preg_match('/^deleteImage/', $method)) {
$key = preg_replace('/^deleteImage/', '', $method);
$image = $this->{"pathImage".$key}();
$delete = (file_exists($image) AND is_file($image))? unlink($image) : null;
if(isset($params[0]) AND true === $params[0]) {
$key = snake_case($key);
$this->{$key} = null;
$this->save();
}
return $delete;
} else {
return parent::__call($method, $params);
}
}
public static function __callStatic($method, $params)
{
if(preg_match('/^makeImage/', $method)) {
$key = preg_replace('/^makeImage/', '', $method);
$key = snake_case($key);
$settings = static::getImageSetting($key);
return static::resizeImage($params[0], $settings);
}
return parent::__callStatic($method, $params);
}
public function deleteImages($save = false)
{
$keys = array_keys(static::imageManagerSettings());
foreach($keys as $key) {
$delete_method = "deleteImage".ucfirst(camel_case($key));
$this->{$delete_method}($save);
}
}
public static function resizeImage($image_file, array $settings = [])
{
$settings = array_merge(static::$image_default_options, $settings);
$image = \Image::make($image_file);
$width = $settings['width'];
$height = $settings['height'];
$filename = $settings['filename'] ?: uniqid();
$location = $settings['location'];
$resize_method = $settings['fit']? 'fit' : 'resize';
$image->{$resize_method}($width, $height, function($c) use ($settings) {
if($settings['keep_aspect_ratio']) $c->aspectRatio();
if($settings['prevent_upsize']) $c->upsize();
});
switch ($image->mime) {
case 'image/gif':
$ext = 'gif';
break;
case 'image/png':
$ext = 'png';
break;
default:
$ext = 'jpg';
break;
}
$dest = public_path($location.'/'.$filename.'.'.$ext);
$image->save($dest, 100);
return $image;
}
public static function getImageSetting($key)
{
$key = snake_case($key);
$settings = static::imageManagerSettings();
if(!array_key_exists($key, $settings)) {
throw new \Exception("Undefined key '{$key}' in imageManagerSettings", 1);
}
return array_merge(static::$image_default_options, $settings[$key]);
}
public static function imageManagerSettings()
{
return static::$image_settings;
}
public static function removeImagesAndTruncate()
{
$records = static::all();
foreach($records as $record) {
$record->deleteImages();
}
static::truncate();
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Traits;
class Mahasiswa extends Model
{
use Traits\ImageManager;
protected $fillable = ['npm', 'name', 'gender', 'photo', 'thumb', 'etc'];
public static function imageManagerSettings()
{
return [
'photo' => [
'location' => 'img/photo',
'width' => 900,
'height' => null,
'fit' => false,
'keep_aspect_ratio' => true,
'prevent_upsize' => true,
'filename' => uniqid(),
],
'thumb' => [
'location' => 'img/photo/thumb',
'width' => 120,
'height' => 120,
'fit' => true,
'keep_aspect_ratio' => true,
'prevent_upsize' => true,
'filename' => uniqid(),
],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment