parent::__construct($fileSystem);
}
+ /**
+ * Get the storage that will be used for storing images.
+ * @param string $type
+ * @return \Illuminate\Contracts\Filesystem\Filesystem
+ */
+ protected function getStorage($type = '')
+ {
+ $storageType = config('filesystems.default');
+
+ // Override default location if set to local public to ensure not visible.
+ if ($type === 'system' && $storageType === 'local_secure') {
+ $storageType = 'local';
+ }
+
+ return $this->fileSystem->disk($storageType);
+ }
+
/**
* Saves a new image from an upload.
* @param UploadedFile $uploadedFile
{
$imageName = $imageName ? $imageName : basename($url);
$imageData = file_get_contents($url);
- if($imageData === false) throw new \Exception(trans('errors.cannot_get_image_from_url', ['url' => $url]));
+ if ($imageData === false) {
+ throw new \Exception(trans('errors.cannot_get_image_from_url', ['url' => $url]));
+ }
return $this->saveNew($imageName, $imageData, $type);
}
*/
private function saveNew($imageName, $imageData, $type, $uploadedTo = 0)
{
- $storage = $this->getStorage();
+ $storage = $this->getStorage($type);
$secureUploads = setting('app-secure-images');
$imageName = str_replace(' ', '-', $imageName);
- if ($secureUploads) $imageName = str_random(16) . '-' . $imageName;
+ if ($secureUploads) {
+ $imageName = str_random(16) . '-' . $imageName;
+ }
$imagePath = '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/';
return $image->path;
}
+ /**
+ * Checks if the image is a gif. Returns true if it is, else false.
+ * @param Image $image
+ * @return boolean
+ */
+ protected function isGif(Image $image)
+ {
+ return strtolower(pathinfo($this->getPath($image), PATHINFO_EXTENSION)) === 'gif';
+ }
+
/**
* Get the thumbnail for an image.
* If $keepRatio is true only the width will be used.
*/
public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
{
+ if ($keepRatio && $this->isGif($image)) {
+ return $this->getPublicUrl($this->getPath($image));
+ }
+
$thumbDirName = '/' . ($keepRatio ? 'scaled-' : 'thumbs-') . $width . '-' . $height . '/';
$imagePath = $this->getPath($image);
$thumbFilePath = dirname($imagePath) . $thumbDirName . basename($imagePath);
return $this->getPublicUrl($thumbFilePath);
}
- $storage = $this->getStorage();
+ $storage = $this->getStorage($image->type);
if ($storage->exists($thumbFilePath)) {
return $this->getPublicUrl($thumbFilePath);
}
* Destroys an Image object along with its files and thumbnails.
* @param Image $image
* @return bool
+ * @throws Exception
*/
public function destroyImage(Image $image)
{
// Cleanup of empty folders
foreach ($storage->directories($imageFolder) as $directory) {
- if ($this->isFolderEmpty($directory)) $storage->deleteDirectory($directory);
+ if ($this->isFolderEmpty($directory)) {
+ $storage->deleteDirectory($directory);
+ }
+ }
+ if ($this->isFolderEmpty($imageFolder)) {
+ $storage->deleteDirectory($imageFolder);
}
- if ($this->isFolderEmpty($imageFolder)) $storage->deleteDirectory($imageFolder);
$image->delete();
return true;
/**
* Save a gravatar image and set a the profile image for a user.
* @param User $user
- * @param int $size
+ * @param int $size
* @return mixed
+ * @throws Exception
*/
public function saveUserGravatar(User $user, $size = 500)
{
$basePath = ($this->storageUrl == false) ? baseUrl('/') : $this->storageUrl;
return rtrim($basePath, '/') . $filePath;
}
-
-
}