Skip to content

Commit

Permalink
Improved image with storage support
Browse files Browse the repository at this point in the history
  • Loading branch information
libern committed May 24, 2018
1 parent 4ac437f commit 1beb2e9
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions src/Someline/Image/SomelineImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function getConfig($key, $default = null)
return config("someline-image.$key", $default);
}

/**
* @return \Illuminate\Filesystem\FilesystemAdapter
*/
public function getStorageDisk()
{
return \Storage::disk();
}

/**
* @return string
*/
Expand Down Expand Up @@ -66,7 +74,7 @@ private function isAlwaysOriginal()
* @param bool $isStorePNG
* @return SomelineImage
*/
public function handleUploadedFile(UploadedFile $file, $additionValidatorRule = '', $isAllowGIF = false, $maxSizeAllowed = 12829, $isStorePNG = false)
public function handleUploadedFile(UploadedFile $file, $additionValidatorRule = '', $isAllowGIF = false, $maxSizeAllowed = 12829, $isStorePNG = true)
{
// check valid
if (!$file->isValid()) {
Expand Down Expand Up @@ -97,12 +105,13 @@ public function handleUploadedFile(UploadedFile $file, $additionValidatorRule =
* @param bool $isStorePNG
* @return null|SomelineImage
*/
public function storeImageFromPath($path_with_name, $file_extension = null, $isAllowGIF = false, $isStorePNG = false)
public function storeImageFromPath($path_with_name, $file_extension = null, $isAllowGIF = false, $isStorePNG = true)
{
$storageDisk = $this->getStorageDisk();
$file_extension = $file_extension ?: pathinfo($path_with_name, PATHINFO_EXTENSION);
$is_file_gif = $file_extension == 'gif';
$is_file_png = $file_extension == 'png';
$file_origin_size = filesize($path_with_name);
$file_origin_size = File::size($path_with_name);
$is_animated_gif = $is_file_gif && $this->isAnimatedGif($path_with_name);


Expand Down Expand Up @@ -145,34 +154,38 @@ public function storeImageFromPath($path_with_name, $file_extension = null, $isA
$final_path_with_name = $storage_path . $final_file_name;

// check directory
if (!self::autoCreateDirectory($final_path_with_name)) {
if (!$this->autoCreateDirectoryInDisk($final_path_with_name)) {
throw new StoreImageException('Failed to make dir: ' . dirname($final_path_with_name));
}

// save
$isExists = File::exists($final_path_with_name);
$isExists = $storageDisk->exists($final_path_with_name);
$isSimilarExists = false;
$someline_image_hash = null;
$final_file_sha1 = null;
$final_file_size = null;
if ($isExists) {
$final_file_sha1 = $file_sha1;
$final_file_size = filesize($final_path_with_name);
$final_file_size = $storageDisk->size($final_path_with_name);
} else {
$someline_image_hash = SomelineImageHash::where(['file_sha1' => $file_sha1])->first();
$isExists = $isSimilarExists = $someline_image_hash ? true : false;
}

if (!$isExists) {
if (($isStoreOriginImage) || ($isAllowGIF && $is_allowed_animated_gif) || ($isStorePNG && $is_file_png)) {
if (File::copy($path_with_name, $final_path_with_name)) {
@chmod($final_path_with_name, 0666 & ~umask());
$contents = File::get($path_with_name);
if ($storageDisk->put($final_path_with_name, $contents, 'public')) {
try {
@chmod($final_path_with_name, 0666 & ~umask());
} catch (\Exception $e) {
}
} else {
throw new StoreImageException('Failed to move file to path: ' . $final_path_with_name);
}
$final_file_sha1 = sha1_file($final_path_with_name);
$final_file_size = filesize($final_path_with_name);
$isExists = File::exists($final_path_with_name);
$final_file_sha1 = sha1($storageDisk->get($final_path_with_name));
$final_file_size = $storageDisk->size($final_path_with_name);
$isExists = $storageDisk->exists($final_path_with_name);
} else {
$isExists = $this->saveImage($image, $exif,
$final_path_with_name, $final_file_sha1, $final_file_size);
Expand Down Expand Up @@ -224,6 +237,7 @@ public function storeImageFromMake($source, $file_sha1, $exif, $isAllowGIF = fal
throw new StoreImageException("SomelineImageService: Unable to make image [" + (string)$e + "]");
}

$storageDisk = $this->getStorageDisk();
$is_allowed_animated_gif = $isAllowGIF && $isGIF;

// save as jpg
Expand All @@ -243,19 +257,19 @@ public function storeImageFromMake($source, $file_sha1, $exif, $isAllowGIF = fal
$final_path_with_name = $storage_path . $final_file_name;

// check directory
if (!self::autoCreateDirectory($final_path_with_name)) {
if (!$this->autoCreateDirectoryInDisk($final_path_with_name)) {
throw new StoreImageException('Failed to make dir: ' . dirname($final_path_with_name));
}

// save
$isExists = File::exists($final_path_with_name);
$isExists = $storageDisk->exists($final_path_with_name);
$isSimilarExists = false;
$someline_image_hash = null;
$final_file_sha1 = null;
$final_file_size = null;
if ($isExists) {
$final_file_sha1 = $file_sha1;
$final_file_size = filesize($final_path_with_name);
$final_file_size = $storageDisk->size($final_path_with_name);
} else {
$someline_image_hash = SomelineImageHash::where(['file_sha1' => $file_sha1])->first();
$isExists = $isSimilarExists = $someline_image_hash ? true : false;
Expand All @@ -268,9 +282,9 @@ public function storeImageFromMake($source, $file_sha1, $exif, $isAllowGIF = fal
} else {
throw new StoreImageException('Failed to move file to path: ' . $final_path_with_name);
}
$final_file_sha1 = sha1_file($final_path_with_name);
$final_file_size = filesize($final_path_with_name);
$isExists = File::exists($final_path_with_name);
$final_file_sha1 = sha1($storageDisk->get($final_path_with_name));
$final_file_size = $storageDisk->size($final_path_with_name);
$isExists = $storageDisk->exists($final_path_with_name);
} else {
$isExists = $this->saveImage($image, $exif,
$final_path_with_name, $final_file_sha1, $final_file_size);
Expand Down Expand Up @@ -348,9 +362,10 @@ public function showImage($template_name, $image_name, array $image_templates =
$storage_path = $storage_path ?: $this->storagePath();
$image_templates = array_merge($this->getConfig('image_templates', []), $image_templates);
$file_path = $storage_path . $image_name;
$storageDisk = $this->getStorageDisk();

// check exists
$isExists = File::exists($file_path);
$isExists = $storageDisk->exists($file_path);
if (!$isExists) {
abort(404);
}
Expand All @@ -368,8 +383,8 @@ public function showImage($template_name, $image_name, array $image_templates =
return response()->download($file_path);
}

$file = File::get($file_path);
$type = File::mimeType($file_path);
$file = $storageDisk->get($file_path);
$type = $storageDisk->mimeType($file_path);

// if gif or original
if (str_contains($image_name, 'gif') || ($imageTemplate->isOriginal() && $imageTemplate->isEmptyOptions())) {
Expand All @@ -378,9 +393,9 @@ public function showImage($template_name, $image_name, array $image_templates =
} else {

// convert to image
$img = Image::cache(function ($image) use ($file_path, $imageTemplate) {
$img = Image::cache(function ($image) use ($file, $imageTemplate) {
/** @var \Intervention\Image\Image $image */
$image->make($file_path);
$image->make($file);

// resize
if (!$imageTemplate->isOriginal()) {
Expand Down Expand Up @@ -443,6 +458,7 @@ function ($constraint) use ($imageTemplate) {
public function saveImage($image, $exif, $final_path_with_name,
&$final_file_sha1 = null, &$final_file_size = null)
{
$storageDisk = $this->getStorageDisk();
// set correct orientation
if (!empty($exif) && is_array($exif)) {
if (isset($exif['Orientation']) && $exif['Orientation'] != 1) {
Expand Down Expand Up @@ -503,9 +519,9 @@ public function saveImage($image, $exif, $final_path_with_name,

// save as JPG
$image->save($final_path_with_name, $image_quality);
$final_file_sha1 = sha1_file($final_path_with_name);
$final_file_size = filesize($final_path_with_name);
$isSaved = File::exists($final_path_with_name);
$final_file_sha1 = sha1($storageDisk->get($final_path_with_name));
$final_file_size = $storageDisk->size($final_path_with_name);
$isSaved = $storageDisk->exists($final_path_with_name);
return $isSaved;
}

Expand Down Expand Up @@ -583,6 +599,20 @@ public static function isDataURLEncodedImageGif($encodedData)
}
}

/**
* @param $path_name
* @return bool
*/
public function autoCreateDirectoryInDisk($path_name)
{
$storageDisk = $this->getStorageDisk();
$dirname = dirname($path_name);
if (!$storageDisk->exists($dirname)) {
return $storageDisk->makeDirectory($dirname);
}
return true;
}

/**
* auto create directory if not exists
* @param $path_name
Expand Down

0 comments on commit 1beb2e9

Please sign in to comment.