diff --git a/src/EloquentImageMutatorTrait.php b/src/EloquentImageMutatorTrait.php index 6156685..94e78d3 100755 --- a/src/EloquentImageMutatorTrait.php +++ b/src/EloquentImageMutatorTrait.php @@ -16,7 +16,7 @@ public function getAttributeValue($key) { $value = $this->retrievePhotoFieldValue($key, $value); - if(!file_exists(public_path().$value->original->url) || empty($value->original->url)) { + if(empty($value->original->url)) { $value = ImageService::getImageObject(); $this->attributes[$key] = $value->toJson(); } @@ -106,6 +106,7 @@ public function retrievePhotoFieldValue($key, $value) public function setImageAttributeForUploadedFileObject($key, $value) { $imageFieldObject = ImageService::uploadImage($key, $value); + $this->attributes[$key] = $imageFieldObject->toJson(); } diff --git a/src/config/image.php b/src/config/image.php index df8e1e1..b8cc477 100755 --- a/src/config/image.php +++ b/src/config/image.php @@ -2,8 +2,8 @@ return array( 'library' => 'gd', - 'upload_dir' => 'uploads', - 'assets_upload_path' => 'storage/app/uploads', + 'upload_dir' => 'public/uploads', + 'assets_upload_path' => 'public/uploads', 'quality' => 85, 'default' => [ 'url' => 'http://placehold.it/150x150', @@ -18,4 +18,4 @@ ['640', '480', false, 85, 'medium'], ['800', '600', false, 85, 'large'] ] -); \ No newline at end of file +); diff --git a/src/dist/ImageFieldLocal.php b/src/dist/ImageFieldLocal.php index 8fddd34..a9c131f 100644 --- a/src/dist/ImageFieldLocal.php +++ b/src/dist/ImageFieldLocal.php @@ -15,14 +15,13 @@ public function __construct($dimensions=[]) foreach ($dimensions as $key => $value) { $this->{$value[4]} = new ImageFile(); } - $this->orignal = new ImageFile(); $this->original = new ImageFile(); return $this; } foreach ($dimensions as $key => $value) { - if($key == 'orignal'){ + if($key == 'original'){ $this->$key = new ImageFile(); $this->$key->url = !empty($value['url']) ? $value['url'] : null; @@ -61,4 +60,4 @@ public static function fromJson($jsonData=null) return new self(); } -} \ No newline at end of file +} diff --git a/src/dist/ImageService.php b/src/dist/ImageService.php index 76b8a26..daeb791 100644 --- a/src/dist/ImageService.php +++ b/src/dist/ImageService.php @@ -2,316 +2,332 @@ namespace SahusoftCom\EloquentImageMutator\Dist; -use SahusoftCom\EloquentImageMutator\Dist\ImageFieldLocal; +use Exception; +use Storage, Config, Log; class ImageService { - public $imagine=null; - - public static function getImagineObject() - { - $library = \Config::get('image.library', 'gd'); - - if ($library == 'imagick') - $imagine = new \Imagine\Imagick\Imagine(); - else if ($library == 'gmagick') - $imagine = new \Imagine\Gmagick\Imagine(); - else if ($library == 'gd') - $imagine = new \Imagine\Gd\Imagine(); - else - $imagine = new \Imagine\Gd\Imagine(); - - return $imagine; - } - - public static function getImageObject($jsonData=null) - { - return ImageFieldLocal::fromJson($jsonData); - } - - public static function copyImage($key, $value) - { - $fileObject = new \SplFileInfo(public_path().$value->original->url); - $destination = ImageService::getANewFileName($fileObject->getExtension()); - $destinationDirectory = ImageService::getUploadStoragePath().'/'.dirname($destination); - if(!\File::isDirectory($destinationDirectory)) - \File::makeDirectory($destinationDirectory, 0777, true); - - copy(public_path().$value->original->url, $destinationDirectory.'/'.basename($destination)); - $urn = ImageService::makeFromFile($destination, basename($destination)); - $allTheSizes = ImageService::getAllTheSizes($urn); - $arrayForDB = []; - - foreach ($allTheSizes as $keyTwo => $value) { - $arrayForDB[$keyTwo]['url'] = !empty($value['urn']) ? '/'.\Config::get('image.upload_dir').'/'.$value['urn'] : null; - $arrayForDB[$keyTwo]['height'] = !empty($value['height']) ? $value['height'] : null; - $arrayForDB[$keyTwo]['width'] = !empty($value['width']) ? $value['width'] : null; - } - $arrayForDB['original']['url'] = !empty($urn) ? '/'.\Config::get('image.upload_dir').'/'.$urn : null; - - $imageObj = new ImageFieldLocal($arrayForDB); - return $imageObj; - - } - - public static function uploadImage($key, $value) - { - $destination = ImageService::getANewFileName($value->getClientOriginalExtension()); - - $value->move(ImageService::getUploadStoragePath().'/'.dirname($destination), basename($destination)); - - $urn = ImageService::makeFromFile($destination, $value->getClientOriginalName()); - - $allTheSizes = ImageService::getAllTheSizes($urn); - $arrayForDB = []; - - foreach ($allTheSizes as $keyTwo => $value) { - $arrayForDB[$keyTwo]['url'] = !empty($value['urn']) ? '/'.\Config::get('image.upload_dir').'/'.$value['urn'] : null; - $arrayForDB[$keyTwo]['height'] = !empty($value['height']) ? $value['height'] : null; - $arrayForDB[$keyTwo]['width'] = !empty($value['width']) ? $value['width'] : null; - } - $arrayForDB['original']['url'] = !empty($urn) ? '/'.\Config::get('image.upload_dir').'/'.$urn : null; + public $imagine = null; + + public static function getImagineObject() + { + $library = Config::get('image.library', 'gd'); + + if ($library == 'imagick') { + $imagine = new \Imagine\Imagick\Imagine(); + } else { + if ($library == 'gmagick') { + $imagine = new \Imagine\Gmagick\Imagine(); + } else { + if ($library == 'gd') { + $imagine = new \Imagine\Gd\Imagine(); + } else { + $imagine = new \Imagine\Gd\Imagine(); + } + } + } + + return $imagine; + } + + public static function getImageObject($jsonData = null) + { + return ImageFieldLocal::fromJson($jsonData); + } + + public static function copyImage($key, $value) + { + $fileObject = new \SplFileInfo(Storage::get($value->original->url)); + $destination = ImageService::getANewFileName($fileObject->getExtension()); + $destinationDirectory = ImageService::getUploadStoragePath() . '/' . dirname($destination); + if (!Storage::exists($destinationDirectory)) { + Storage::makeDirectory($destinationDirectory); + } + + Storage::put($destinationDirectory . '/' . basename($destination), + file_get_contents(Storage::get($value->original->url)), 'public'); + + $urn = ImageService::makeFromFile($destination, basename($destination)); + $allTheSizes = ImageService::getAllTheSizes($urn); + $arrayForDB = []; + + foreach ($allTheSizes as $keyTwo => $value) { + $arrayForDB[$keyTwo]['url'] = !empty($value['urn']) ? '/' . Config::get('image.upload_dir') . '/' . $value['urn'] : null; + $arrayForDB[$keyTwo]['height'] = !empty($value['height']) ? $value['height'] : null; + $arrayForDB[$keyTwo]['width'] = !empty($value['width']) ? $value['width'] : null; + } + $arrayForDB['original']['url'] = !empty($urn) ? '/' . Config::get('image.upload_dir') . '/' . $urn : null; + + $imageObj = new ImageFieldLocal($arrayForDB); + return $imageObj; + + } + + /** + * @param $key + * @param \Illuminate\Http\UploadedFile | \Symfony\Component\HttpFoundation\File\UploadedFile $value + * @return \SahusoftCom\EloquentImageMutator\Dist\ImageFieldLocal + */ + public static function uploadImage($key, $value) + { + $destination = ImageService::getANewFileName($value->getClientOriginalExtension()); + + $file = Storage::putFileAs(ImageService::getUploadStoragePath() . '/' . dirname($destination), $value, + basename($destination), 'public'); + + $urn = ImageService::makeFromFile($destination, $value->getClientOriginalName()); + + $allTheSizes = ImageService::getAllTheSizes($urn); + $arrayForDB = []; + + foreach ($allTheSizes as $keyTwo => $value) { + $arrayForDB[$keyTwo]['url'] = !empty($value['urn']) ? Config::get('image.upload_dir') . '/' . $value['urn'] : null; + $arrayForDB[$keyTwo]['height'] = !empty($value['height']) ? $value['height'] : null; + $arrayForDB[$keyTwo]['width'] = !empty($value['width']) ? $value['width'] : null; + } + $arrayForDB['original']['url'] = !empty($urn) ? Config::get('image.upload_dir') . '/' . $urn : null; + + $imageObj = new ImageFieldLocal($arrayForDB); + return $imageObj; + } + + public static function downloadImage($key, $value) + { + $fileObject = new \SplFileInfo($value); + $destination = ImageService::getANewFileName(preg_replace('/\?.*/', '', $fileObject->getExtension())); + $destinationDirectory = ImageService::getUploadStoragePath() . '/' . dirname($destination); + if (!Storage::exists($destinationDirectory)) { + Storage::makeDirectory($destinationDirectory); + } + Storage::put($destinationDirectory . '/' . basename($destination), file_get_contents($value), 'public'); + $urn = ImageService::makeFromFile($destination, basename($destination)); + $allTheSizes = ImageService::getAllTheSizes($urn); + $arrayForDB = []; + + foreach ($allTheSizes as $keyTwo => $value) { + $arrayForDB[$keyTwo]['url'] = !empty($value['urn']) ? Config::get('image.upload_dir') . '/' . $value['urn'] : null; + $arrayForDB[$keyTwo]['height'] = !empty($value['height']) ? $value['height'] : null; + $arrayForDB[$keyTwo]['width'] = !empty($value['width']) ? $value['width'] : null; + } + $arrayForDB['original']['url'] = !empty($urn) ? Config::get('image.upload_dir') . '/' . $urn : null; + + $imageObj = new ImageFieldLocal($arrayForDB); + return $imageObj; + } + + public static function getANewFolder() + { + return 'user/' . date('Y/m/d'); + } + + public static function getUploadStoragePath() + { + return Config::get('image.assets_upload_path'); + } + + public static function getANewFileName($ext) + { + return self::getANewFolder() . '/' . str_random(16) . '.' . $ext; + } + + public static function makeFromFile($urn, $original_name = '', $title = '') + { +// if(!$original_name) +// $original_name = basename($urn); + + $absFile = self::getUploadStoragePath() . '/' . $urn; + + $tempValue = 0; + + self::createDimensions($absFile); + + return $urn; + } + + public static function createDimensions($url, $dimensions = array()) + { + $defaultDimensions = Config::get('image.dimensions'); + + if (is_array($defaultDimensions)) { + $dimensions = array_merge($defaultDimensions, $dimensions); + } + + foreach ($dimensions as $dimension) { + $width = (int)$dimension[0]; + $height = isset($dimension[1]) ? (int)$dimension[1] : $width; + $crop = isset($dimension[2]) ? (bool)$dimension[2] : false; + $quality = isset($dimension[3]) ? (int)$dimension[3] : Config::get('image.quality'); - $imageObj = new ImageFieldLocal($arrayForDB); - return $imageObj; - } - - public static function downloadImage($key, $value) - { - fileObject = new \SplFileInfo($value); - $destination = ImageService::getANewFileName(preg_replace('/\?.*/', '', $fileObject->getExtension())); - $destinationDirectory = ImageService::getUploadStoragePath().'/'.dirname($destination); - if(!\File::isDirectory($destinationDirectory)) - \File::makeDirectory($destinationDirectory, 0777, true); - - file_put_contents($destinationDirectory.'/'.basename($destination), file_get_contents($value)); - - if (empty($fileObject->getExtension())) { - $mime = mime_content_type($destinationDirectory.'/'.basename($destination)); - $mime = explode('/', $mime); - if ($mime[0] == 'image') { - rename($destinationDirectory.'/'.basename($destination), $destinationDirectory.'/'.basename($destination) . $mime[1]); - $destination = $destination . $mime[1]; - } - } - - $urn = ImageService::makeFromFile($destination, basename($destination)); - $allTheSizes = ImageService::getAllTheSizes($urn); - $arrayForDB = []; - - foreach ($allTheSizes as $keyTwo => $value) { - $arrayForDB[$keyTwo]['url'] = !empty($value['urn']) ? '/'.\Config::get('image.upload_dir').'/'.$value['urn'] : null; - $arrayForDB[$keyTwo]['height'] = !empty($value['height']) ? $value['height'] : null; - $arrayForDB[$keyTwo]['width'] = !empty($value['width']) ? $value['width'] : null; - } - $arrayForDB['original']['url'] = !empty($urn) ? '/'.\Config::get('image.upload_dir').'/'.$urn : null; - - $imageObj = new ImageFieldLocal($arrayForDB); - return $imageObj; - } - - public static function getANewFolder() - { - return 'user/'.date('Y/m/d/i/s'); - } - - public static function getUploadStoragePath() - { - return base_path().'/'.\Config::get('image.assets_upload_path'); - } - - public static function getANewFileName($ext) - { - return self::getANewFolder().'/'.str_random(16).'.'.$ext; - } - - public static function makeFromFile($urn, $original_name = '', $title='') - { - if(!$original_name) - $original_name = basename($urn); - - $absFile = self::getUploadStoragePath().'/'.$urn; + $dest = dirname($url) . '/' . $width . 'x' . $height . ($crop ? '_crop' : '') . '/' . basename($url); - $tempValue = 0; + $img = self::resize($url, $dest, $width, $height, $crop, $quality); + + } + } + + public static function getAllTheSizes($url, $getImageSize = true) + { + $dimensions = array(); + + $defaultDimensions = Config::get('image.dimensions'); + + if (is_array($defaultDimensions)) { + $dimensions = array_merge($defaultDimensions, $dimensions); + } - self::createDimensions($absFile); + $ret = array(); - return $urn; - } + foreach ($dimensions as $dimension) { + // Get dimmensions and quality + $width = (int)$dimension[0]; + $height = isset($dimension[1]) ? (int)$dimension[1] : $width; + $crop = isset($dimension[2]) ? (bool)$dimension[2] : false; + $quality = isset($dimension[3]) ? (int)$dimension[3] : Config::get('image.quality'); - public static function createDimensions($url, $dimensions = array()) - { - $defaultDimensions = \Config::get('image.dimensions'); + $info = pathinfo($url); - if (is_array($defaultDimensions)) $dimensions = array_merge($defaultDimensions, $dimensions); + // Directories and file names + $fileName = $info['basename']; + $sourceDirPath = self::getUploadStoragePath() . '/' . $info['dirname']; + $sourceFilePath = $sourceDirPath . '/' . $fileName; + $targetDirName = $width . 'x' . $height . ($crop ? '_crop' : ''); + $targetDirPath = $sourceDirPath . '/' . $targetDirName . '/'; + $targetFilePath = $targetDirPath . $fileName; + //$targetUrl = asset($info['dirname'].'/'.$targetDirName.'/'.$fileName); - foreach ($dimensions as $dimension) - { - $width = (int) $dimension[0]; - $height = isset($dimension[1]) ? (int) $dimension[1] : $width; - $crop = isset($dimension[2]) ? (bool) $dimension[2] : false; - $quality = isset($dimension[3]) ? (int) $dimension[3] : \Config::get('image.quality'); + $file = self::getUploadStoragePath() . '/' . $info['dirname'] . '/' . $targetDirName . '/' . $fileName; - $dest = dirname($url).'/'.$width.'x'.$height.($crop?'_crop':'').'/'.basename($url); + if ($getImageSize) { + if (!file_exists($file)) { + $width = 0; + $height = 0; + } else { - $img = self::resize($url, $dest, $width, $height, $crop, $quality); + list($width, $height) = getimagesize($file); + } + } - } - } + $ret[$dimension[4]] = [ + 'urn' => $info['dirname'] . '/' . $targetDirName . '/' . $fileName, + 'width' => $width, + 'height' => $height + ]; - public static function getAllTheSizes($url, $getImageSize = true) - { - $dimensions = array(); + } - $defaultDimensions = \Config::get('image.dimensions'); + return $ret; + } - if (is_array($defaultDimensions)) - $dimensions = array_merge($defaultDimensions, $dimensions); + public static function crop($x, $y, $width, $height) + { + $ext = \File::extension(self::urn); + $destinationImage = self::getANewFileName($ext, true); - $ret = array(); + $destinationAbsImage = self::getUploadStoragePath() . '/' . $destinationImage; - foreach ($dimensions as $dimension) { - // Get dimmensions and quality - $width = (int) $dimension[0]; - $height = isset($dimension[1]) ? (int) $dimension[1] : $width; - $crop = isset($dimension[2]) ? (bool) $dimension[2] : false; - $quality = isset($dimension[3]) ? (int) $dimension[3] : \Config::get('image.quality'); + $sourceAbsImage = self::getUploadStoragePath() . '/' . self::urn; + self::imageCrop($sourceAbsImage, $destinationAbsImage, $x, $y, $width, $height); - $info = pathinfo($url); + if (!\File::exists($destinationAbsImage)) { + throw new Exception("Image not cropped and saved"); + } - // Directories and file names - $fileName = $info['basename']; - $sourceDirPath = self::getUploadStoragePath().'/'.$info['dirname']; - $sourceFilePath = $sourceDirPath.'/'.$fileName; - $targetDirName = $width.'x'.$height.($crop ? '_crop' : ''); - $targetDirPath = $sourceDirPath.'/'.$targetDirName.'/'; - $targetFilePath = $targetDirPath.$fileName; - //$targetUrl = asset($info['dirname'].'/'.$targetDirName.'/'.$fileName); + $photo = self::makeFromFile($destinationImage, true, '', 'Image File'); - $file = self::getUploadStoragePath().'/'.$info['dirname'].'/'.$targetDirName.'/'.$fileName; + return $photo; + } - if($getImageSize) { - if(!file_exists($file)) { - $width = 0; - $height = 0; - } - else { + public static function imageCrop($source, $destination, $x = 0, $y = 0, $width = 1, $height = 1, $quality = 90) + { + if (!Storage::exists($source)) { + throw new Exception("[IMAGE SERVICE] Source file does not exist"); + } - list($width, $height) = getimagesize($file); - } - } + $destinationFolder = dirname($destination); - $ret[$dimension[4]] = [ - 'urn'=> $info['dirname'].'/'.$targetDirName.'/'.$fileName, - 'width' => $width, - 'height' => $height - ]; + if (!Storage::exists($destinationFolder)) { + Storage::makeDirectory($destinationFolder); + } - } + list($imgWidth, $imgHeight) = getimagesize($source); - return $ret; - } + $cropXPixels = $imgWidth * $x; + $cropYPixels = $imgHeight * $y; + $cropWidthPixels = $width * $imgWidth; + $cropHeightPixels = $height * $imgHeight; - public static function crop($x, $y, $width, $height) - { - $ext = \File::extension(self::urn); - $destinationImage = self::getANewFileName($ext,true); + $point = new \Imagine\Image\Point($cropXPixels, $cropYPixels); + $box = new \Imagine\Image\Box($cropWidthPixels, $cropHeightPixels); - $destinationAbsImage = self::getUploadStoragePath().'/'.$destinationImage; + if (empty($imagine)) { + $imagine = self::getImagineObject(); + } - $sourceAbsImage = self::getUploadStoragePath().'/'.self::urn; - self::imageCrop($sourceAbsImage,$destinationAbsImage,$x,$y,$width,$height); + try { + $image = $imagine->open(Storage::get($source)); + if(class_exists('\Imagine\Filter\Basic\Autorotate')){ + $filterAutorotate = new \Imagine\Filter\Basic\Autorotate(); + $filterAutorotate->apply($image); + } + $image_source = $image->crop($point, $box) + ->get(array_last(explode('.', $destination)), array('quality' => $quality)); + Storage::put($destination, $image_source, 'public'); - if(!\File::exists($destinationAbsImage)) { - throw new Exception("Image not cropped and saved"); - } + } catch (\Exception $e) { - $photo = self::makeFromFile($destinationImage,true,'','Image File'); + Log::error('[IMAGE SERVICE] Image crop Failed to crop image [' . $e->getMessage() . ']'); - return $photo; - } + } - public static function imageCrop($source, $destination, $x=0, $y=0, $width=1, $height=1, $quality=90) - { - if(!\File::exists($source)) - throw new Exception("[IMAGE SERVICE] Source file does not exist"); + return $destination; + } - $destinationFolder = dirname($destination); + public static function resize($source, $destination, $width = 100, $height = null, $crop = false, $quality = 90) + { + if (!Storage::exists($source)) { + throw new Exception("[IMAGE SERVICE] Source file does not exist"); + } - if(!\File::isDirectory($destinationFolder)) - \File::makeDirectory($destinationFolder, 0777, true); + $destinationFolder = dirname($destination); - list($imgWidth,$imgHeight) = getimagesize($source); + if (!Storage::exists($destinationFolder)) { + Storage::makeDirectory($destinationFolder); + } - $cropXPixels = $imgWidth*$x; - $cropYPixels = $imgHeight*$y; - $cropWidthPixels = $width * $imgWidth; - $cropHeightPixels = $height * $imgHeight; + // Set the size + $size = new \Imagine\Image\Box($width, $height); - $point = new \Imagine\Image\Point($cropXPixels,$cropYPixels); - $box = new \Imagine\Image\Box($cropWidthPixels,$cropHeightPixels); + // Now the mode + $mode = $crop ? \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND : \Imagine\Image\ImageInterface::THUMBNAIL_INSET; - if(empty($imagine)) - $imagine = self::getImagineObject(); + if (empty($imagine)) { + $imagine = self::getImagineObject(); + } + try { - try { + $image = $imagine->load(Storage::get($source)); + if(class_exists('\Imagine\Filter\Basic\Autorotate')){ + $filterAutorotate = new \Imagine\Filter\Basic\Autorotate(); + $filterAutorotate->apply($image); + } - $image = $imagine->open($source); - $filterAutorotate = new \Imagine\Filter\Basic\Autorotate(); - $filterAutorotate->apply($image); - $image->crop($point,$box) - ->save($destination, array('quality' => $quality)); + $image_source = $image->thumbnail($size, $mode) + ->get(array_last(explode('.', $destination)), array('quality' => $quality)); + Storage::put($destination, $image_source, 'public'); + } catch (\Exception $e) { - } catch (\Exception $e) { + Log::error('[IMAGE SERVICE] Image resize Failed to crop image [' . $e->getMessage() . ']'); - \Log::error('[IMAGE SERVICE] Image crop Failed to crop image [' . $e->getMessage() . ']'); + } - } + return $destination; + } - return $destination; - } - - public static function resize($source, $destination, $width = 100, $height = null, $crop = false, $quality = 90) - { - if(!\File::exists($source)) - throw new Exception("[IMAGE SERVICE] Source file does not exist"); - - $destinationFolder = dirname($destination); - - if(!\File::isDirectory($destinationFolder)) - \File::makeDirectory($destinationFolder, 0777, true); - - // Set the size - $size = new \Imagine\Image\Box($width, $height); - - // Now the mode - $mode = $crop ? \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND : \Imagine\Image\ImageInterface::THUMBNAIL_INSET; - - if(empty($imagine)) - $imagine = self::getImagineObject(); - - try { - - - $image = $imagine->open($source); - $filterAutorotate = new \Imagine\Filter\Basic\Autorotate(); - $filterAutorotate->apply($image); - $image->thumbnail($size, $mode) - ->save($destination, array('quality' => $quality)); - - } catch (\Exception $e) { - - \Log::error('[IMAGE SERVICE] Image resize Failed to crop image [' . $e->getMessage() . ']'); - - } - - return $destination; - } - - public static function delete($fullPath) - { - return unlink($fullPath); - } + public static function delete($fullPath) + { + return Storage::delete($fullPath); + } } +