embed variant types into Media model
This commit is contained in:
@@ -105,17 +105,7 @@ class StoreUploadedFileJob implements ShouldQueue
|
||||
$this->media->save();
|
||||
|
||||
// Generate additional image sizes
|
||||
$sizes = [
|
||||
'thumb' => [150, 150],
|
||||
'small' => [300, 225],
|
||||
'medium' => [768, 576],
|
||||
'large' => [1024, 768],
|
||||
'xlarge' => [1536, 1152],
|
||||
'xxlarge' => [2048, 1536],
|
||||
'scaled' => [2560, 1920]
|
||||
];
|
||||
|
||||
$variants = [];
|
||||
$sizes = Media::getTypeVariants('image');
|
||||
|
||||
$originalImage = Image::make($this->uploadedFilePath);
|
||||
|
||||
@@ -123,7 +113,7 @@ class StoreUploadedFileJob implements ShouldQueue
|
||||
$this->media->dimensions = implode('x', $dimensions);
|
||||
|
||||
foreach ($sizes as $variantName => $size) {
|
||||
$postfix = "{$size[0]}x{$size[1]}";
|
||||
$postfix = "{$size['width']}x{$size['height']}";
|
||||
if ($variantName === 'scaled') {
|
||||
$postfix = 'scaled';
|
||||
}
|
||||
@@ -136,7 +126,7 @@ class StoreUploadedFileJob implements ShouldQueue
|
||||
|
||||
if (Storage::disk($storageDisk)->exists($newFilename) === false || $this->replaceExisting === true) {
|
||||
// Get the largest available variant
|
||||
if ($dimensions[0] >= $size[0] && $dimensions[1] >= $size[1]) {
|
||||
if ($dimensions[0] >= $size['width'] && $dimensions[1] >= $size['height']) {
|
||||
// Store the variant in the variants array
|
||||
$variants[$variantName] = $newFilename;
|
||||
|
||||
@@ -144,12 +134,12 @@ class StoreUploadedFileJob implements ShouldQueue
|
||||
$image = clone $originalImage;
|
||||
|
||||
$imageSize = $image->getSize();
|
||||
if ($imageSize->getWidth() > $size[0] || $imageSize->getHeight() > $size[1]) {
|
||||
$image->resize($size[0], $size[1], function ($constraint) {
|
||||
if ($imageSize->getWidth() > $size['width'] || $imageSize->getHeight() > $size['height']) {
|
||||
$image->resize($size['width'], $size['height'], function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
$constraint->upsize();
|
||||
});
|
||||
$image->resizeCanvas($size[0], $size[1], 'center', false, '#FFFFFF');
|
||||
$image->resizeCanvas($size['width'], $size['height'], 'center', false, '#FFFFFF');
|
||||
}
|
||||
|
||||
// Optimize and store image
|
||||
|
||||
@@ -48,7 +48,6 @@ class Media extends Model
|
||||
'description',
|
||||
'name',
|
||||
'size',
|
||||
'mime_type',
|
||||
'status',
|
||||
];
|
||||
|
||||
@@ -81,6 +80,23 @@ class Media extends Model
|
||||
*/
|
||||
protected static $storageFileListCache = [];
|
||||
|
||||
/**
|
||||
* The variant types.
|
||||
*
|
||||
* @var int[][][]
|
||||
*/
|
||||
protected static $variantTypes = [
|
||||
'image' => [
|
||||
'thumb' => ['width' => 150, 'height' => 150],
|
||||
'small' => ['width' => 300, 'height' => 225],
|
||||
'medium' => ['width' => 768, 'height' => 576],
|
||||
'large' => ['width' => 1024, 'height' => 768],
|
||||
'xlarge' => ['width' => 1536, 'height' => 1152],
|
||||
'xxlarge' => ['width' => 2048, 'height' => 1536],
|
||||
'scaled' => ['width' => 2560, 'height' => 1920]
|
||||
]
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* Model Boot
|
||||
@@ -114,6 +130,21 @@ class Media extends Model
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Type Variants.
|
||||
*
|
||||
* @param string $type The variant type to get.
|
||||
* @return array The variant data.
|
||||
*/
|
||||
public static function getTypeVariants(string $type)
|
||||
{
|
||||
if (isset(self::$variantTypes[$type]) === true) {
|
||||
return self::$variantTypes[$type];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Variants Get Mutator.
|
||||
*
|
||||
@@ -144,6 +175,85 @@ class Media extends Model
|
||||
$this->attributes['variants'] = json_encode(($value ?? []));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get previous variant.
|
||||
*
|
||||
* @param string $type The variant type.
|
||||
* @param string $variant The initial variant.
|
||||
* @return string The previous variant name (or '').
|
||||
*/
|
||||
public function getPreviousVariant(string $type, string $variant)
|
||||
{
|
||||
if (isset(self::$variantTypes[$type]) === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$variants = self::$variantTypes[$type];
|
||||
$keys = array_keys($variants);
|
||||
|
||||
$currentIndex = array_search($variant, $keys);
|
||||
if ($currentIndex === false || $currentIndex === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $keys[($currentIndex - 1)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next variant.
|
||||
*
|
||||
* @param string $type The variant type.
|
||||
* @param string $variant The initial variant.
|
||||
* @return string The next variant name (or '').
|
||||
*/
|
||||
public function getNextVariant(string $type, string $variant)
|
||||
{
|
||||
if (isset(self::$variantTypes[$type]) === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$variants = self::$variantTypes[$type];
|
||||
$keys = array_keys($variants);
|
||||
|
||||
$currentIndex = array_search($variant, $keys);
|
||||
if ($currentIndex === false || $currentIndex === (count($keys) - 1)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $keys[($currentIndex + 1)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get variant URL.
|
||||
*
|
||||
* @param string $variant The variant to find.
|
||||
* @param boolean $returnNearest Return the nearest variant if request is not found.
|
||||
* @return string The URL.
|
||||
*/
|
||||
public function getVariantURL(string $variant, bool $returnNearest = true)
|
||||
{
|
||||
$variants = $this->variants;
|
||||
if (isset($variants[$variant]) === true) {
|
||||
return self::getUrlPath() . $variants[$variant];
|
||||
}
|
||||
|
||||
if ($returnNearest === true) {
|
||||
$variantType = explode('/', $this->mime_type)[0];
|
||||
$previousVariant = $variant;
|
||||
while (empty($previousVariant) === false) {
|
||||
$previousVariant = $this->getPreviousVariant($variantType, $previousVariant);
|
||||
if (empty($previousVariant) === false && isset($variants[$previousVariant]) === true) {
|
||||
return self::getUrlPath() . $variants[$previousVariant];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Delete file and associated files with the modal.
|
||||
*
|
||||
@@ -199,6 +309,17 @@ class Media extends Model
|
||||
}//end if
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlPath()
|
||||
{
|
||||
$url = config("filesystems.disks.$this->storage.url");
|
||||
return "$url/";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file URL
|
||||
*
|
||||
@@ -207,8 +328,7 @@ class Media extends Model
|
||||
public function getUrlAttribute()
|
||||
{
|
||||
if (isset($this->attributes['name']) === true) {
|
||||
$url = config("filesystems.disks.$this->storage.url");
|
||||
return "$url/$this->name";
|
||||
return self::getUrlPath() . $this->name;
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
Reference in New Issue
Block a user