File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/KommaApp/Kms/Core/Attributes/KmsImages.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma Mediadesign
*/
namespace KommaApp\Kms\Core\Attributes;
use KommaApp\Images\Models\Image;
use View;
use File;
use Gregwar\Image\Image as ImageCropper;
class KmsImages extends KmsAttribute
{
public $label;
public $placeholder;
public $subfolder;
public $maxImages;
protected $fullPath;
protected $path;
function __construct($key, $value, array $options = [], array $errors = [], $siteId = null, $languageId = null, $section = null)
{
parent::__construct($key, $value, $options, $errors, $siteId, $languageId, $section);
$this->label = $this->getOption('label', $key);
$this->placeholder = $this->getOption('placeholder', $key);
$this->subfolder = $this->getOption('subfolder', null);
$this->maxImages = $this->getOption('maxImages', null);
$this->fullPath = $this->subfolder == null ? \Config::get('kms.paths.full_path_images') : \Config::get('kms.paths.full_path_images') . '/' . $this->subfolder;
$this->path = $this->subfolder == null ? \Config::get('kms.paths.path_images') : \Config::get('kms.paths.path_images') . '/' . $this->subfolder;
}
public function process()
{
$this->uploadImages();
}
public function render()
{
$this->uploadImages();
return View::make('kms/attributes.images', [
'attribute' => $this
]);
}
public function getValue($post = false)
{
//Check if valueKey is set
if ($post == false && $valueKey = $this->getOption('valueKey', null)) {
if (!$value = $this->arr->get($this->value, $valueKey)) $this->value;
return $value;
}
return $this->value;
}
public function getValueAsJson()
{
return $this->getValue() ? json_encode($this->getValue()) : '[]';
}
public function delete()
{
$this->deleteImages();
}
protected function deleteImages()
{
$images = (is_array($this->value)) ? $this->value : json_decode($this->value);
foreach ($images as $image) {
if (File::exists(public_path() . $image->original))
File::delete(public_path() . $image->original);
if (File::exists(public_path() . $image->large))
File::delete(public_path() . $image->large);
if (File::exists(public_path() . $image->medium))
File::delete(public_path() . $image->medium);
if (File::exists(public_path() . $image->small))
File::delete(public_path() . $image->small);
if (File::exists(public_path() . $image->thumb))
File::delete(public_path() . $image->thumb);
}
}
protected function uploadImages()
{
if ($this->value) {
$images = (is_array($this->value)) ? $this->value : json_decode($this->value);
if (!$images) {
$this->value = '';
return;
}
foreach ($images as $key => $image) {
// When the name is set, we can upload the photo
if (isset($image->name)) {
//Upload Image
$file = \Input::file($image->name);
$image = ImageCropper::open($file);
$originalFilename = str_slug(basename($file->getClientOriginalName(), $file->getClientOriginalExtension())) . '.' . $file->getClientOriginalExtension();
$fullPath = $this->subfolder == null ? \Config::get('kms.paths.full_path_images') : \Config::get('kms.paths.full_path_images') . '/' . $this->subfolder;
$path = $this->subfolder == null ? \Config::get('kms.paths.path_images') : \Config::get('kms.paths.path_images') . '/' . $this->subfolder;
// Save Original
/*
$filename = time() . '-' . $originalFilename;
$image->save($fullPath . '/' . $filename);
$images[$key]->original = $path .'/'.$filename;
*/
// Save Large
$images[$key]->large = $this->uploadImage('large', $file);
// Save Medium
$images[$key]->medium = $this->uploadImage('medium', $file);
// Save Small
$images[$key]->small = $this->uploadImage('small', $file);
// Save Thumb
$image = ImageCropper::open($file);
$filename = time() . '-thumb-' . $originalFilename;
$image->zoomCrop(128, 128)->save($fullPath . '/' . $filename, 'guess', 100);
$images[$key]->thumb = $path . '/' . $filename;
}
// Delete images on update
if (isset($image->id) && isset($image->delete)) {
$image = Image::Find($image->id);
//if(isset($image->original_image_url)) \File::delete(public_path().$image->original_image_url);
if (isset($image->large_image_url)) \File::delete(public_path() . $image->large_image_url);
if (isset($image->medium_image_url)) \File::delete(public_path() . $image->medium_image_url);
if (isset($image->small_image_url)) \File::delete(public_path() . $image->small_image_url);
if (isset($image->thumb_image_url)) \File::delete(public_path() . $image->thumb_image_url);
}
}
$this->value = $images;
}
}
protected function uploadImage($name, $file)
{
if (!isset($this->options['medium'])) return '';
$image = ImageCropper::open($file);
$originalFilename = str_slug(basename($file->getClientOriginalName(), $file->getClientOriginalExtension())) . '.' . $file->getClientOriginalExtension();
$filename = time() . '-' . $name . '-' . $originalFilename;
if ($this->options[$name]['method'] == 'fit') {
$image->cropResize($this->options[$name]['width'], $this->options[$name]['height']);
} else {
$image->zoomCrop($this->options[$name]['width'], $this->options[$name]['height']);
}
$image->save($this->fullPath . '/' . $filename, 'guess', 100);
return $this->path . '/' . $filename;
}
}