Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"composer/composer": "^1.10.22 || ^2.0.13",
"facade/ignition-contracts": "^1.0",
"guzzlehttp/guzzle": "^6.3 || ^7.0",
"james-heinrich/getid3": "^1.9",
"laravel/framework": "^6.20.14 || ^7.30.4 || ^8.24.0",
"laravel/helpers": "^1.1",
"league/commonmark": "^1.5",
Expand Down
18 changes: 12 additions & 6 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Statamic\Assets;

use Facades\Statamic\Assets\Dimensions;
use Facades\Statamic\Assets\Attributes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Statamic\Contracts\Assets\Asset as AssetContract;
Expand Down Expand Up @@ -159,14 +159,15 @@ public function generateMeta()
$meta = ['data' => $this->data->all()];

if ($this->exists()) {
$dimensions = Dimensions::asset($this)->get();
$attributes = Attributes::asset($this)->get();

$meta = array_merge($meta, [
'size' => $this->disk()->size($this->path()),
'last_modified' => $this->disk()->lastModified($this->path()),
'width' => $dimensions[0],
'height' => $dimensions[1],
'width' => Arr::get($attributes, 'width'),
'height' => Arr::get($attributes, 'height'),
'mime_type' => $this->disk()->mimeType($this->path()),
'duration' => Arr::get($attributes, 'duration'),
]);
}

Expand Down Expand Up @@ -548,7 +549,7 @@ public function move($folder, $filename = null)
*/
public function dimensions()
{
if (! $this->isImage() && ! $this->isSvg()) {
if (! $this->hasDimensions()) {
return [null, null];
}

Expand Down Expand Up @@ -600,7 +601,7 @@ public function orientation()
*/
public function ratio()
{
if (! $this->isImage() && ! $this->isSvg()) {
if (! $this->hasDimensions()) {
return null;
}

Expand Down Expand Up @@ -785,4 +786,9 @@ public function shallowAugmentedArrayKeys()
{
return ['id', 'url', 'permalink', 'api_url'];
}

private function hasDimensions()
{
return $this->isImage() || $this->isSvg() || $this->isVideo();
}
}
173 changes: 173 additions & 0 deletions src/Assets/Attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

namespace Statamic\Assets;

use Facades\Statamic\Assets\ExtractInfo;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\MountManager;
use Statamic\Imaging\ImageGenerator;
use Statamic\Support\Arr;

class Attributes
{
/**
* @var Asset
*/
private $asset;

/**
* @param $generator ImageGenerator
*/
public function __construct(ImageGenerator $generator)
{
$this->generator = $generator;
}

public function asset(Asset $asset)
{
$this->asset = $asset;

return $this;
}

/**
* Get the attributes of an asset.
*
* @return array
*/
public function get()
{
if ($this->asset->isAudio()) {
return $this->getAudioAttributes();
}

if ($this->asset->isImage()) {
return $this->getImageAttributes();
}

if ($this->asset->isSvg()) {
return $this->getSvgAttributes();
}

if ($this->asset->isVideo()) {
return $this->getVideoAttributes();
}

return [];
}

/**
* Get the attributes of a sound.
*
* @return array
*/
private function getAudioAttributes()
{
$id3 = ExtractInfo::fromAsset($this->asset);

$length = Arr::get($id3, 'playtime_seconds', 0);

return ['duration' => $length];
}

/**
* Get the attributes of an image.
*
* @return array
*/
private function getImageAttributes()
{
// Since assets may be located on external platforms like Amazon S3, we can't simply
// grab the attributes. So we'll copy it locally and read the attributes from there.
$manager = new MountManager([
'source' => $this->asset->disk()->filesystem()->getDriver(),
'cache' => $cache = $this->getCacheFlysystem(),
]);

$cachePath = "{$this->asset->containerId()}/{$this->asset->path()}";

if ($manager->has($destination = "cache://{$cachePath}")) {
$manager->delete($destination);
}

$manager->copy("source://{$this->asset->path()}", $destination);

try {
[$width, $height] = getimagesize($cache->getAdapter()->getPathPrefix().$cachePath);
$size = compact('width', 'height');
} catch (\Exception $e) {
$size = [];
} finally {
$cache->delete($cachePath);
}

return $size;
}

/**
* Get the attributes of an SVG.
*
* @return array
*/
private function getSvgAttributes()
{
// Since assets may be located on external platforms like Amazon S3, we can't simply
// grab the attributes. So we'll copy it locally and read the attributes from there.
$manager = new MountManager([
'source' => $this->asset->disk()->filesystem()->getDriver(),
'cache' => $cache = $this->getCacheFlysystem(),
]);

$cachePath = "{$this->asset->containerId()}/{$this->asset->path()}";

if ($manager->has($destination = "cache://{$cachePath}")) {
$manager->delete($destination);
}

$manager->copy("source://{$this->asset->path()}", $destination);

$svg = simplexml_load_file($cache->getAdapter()->getPathPrefix().$cachePath);

$cache->delete($cachePath);

if ($svg['width'] && $svg['height']
&& is_numeric((string) $svg['width'])
&& is_numeric((string) $svg['height'])) {
return ['width' => (float) $svg['width'], 'height' => (float) $svg['height']];
} elseif ($svg['viewBox']) {
[,,$width, $height] = preg_split('/[\s,]+/', $svg['viewBox'] ?: '');

return compact('width', 'height');
}

return ['width' => 300, 'height' => 150];
}

/**
* Get the attributes of a video.
*
* @return array
*/
private function getVideoAttributes()
{
$id3 = ExtractInfo::fromAsset($this->asset);

return [
'width' => Arr::get($id3, 'video.resolution_x'),
'height' => Arr::get($id3, 'video.resolution_y'),
'duration' => Arr::get($id3, 'playtime_seconds'),
];
}

private function getCacheFlysystem()
{
$disk = 'attributes-cache';

config(["filesystems.disks.{$disk}" => [
'driver' => 'local',
'root' => storage_path('statamic/attributes-cache'),
]]);

return Storage::disk($disk)->getDriver();
}
}
136 changes: 6 additions & 130 deletions src/Assets/Dimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,149 +2,25 @@

namespace Statamic\Assets;

use Illuminate\Support\Facades\Storage;
use League\Flysystem\MountManager;
use Statamic\Imaging\ImageGenerator;

class Dimensions
/**
* @deprecated
*/
class Dimensions extends Attributes
{
/**
* @var Asset
*/
private $asset;

/**
* @param $generator ImageGenerator
*/
public function __construct(ImageGenerator $generator)
{
$this->generator = $generator;
}

public function asset(Asset $asset)
{
$this->asset = $asset;

return $this;
}

/**
* Get the dimensions of an asset.
*
* @return array
*/
public function get()
{
if ($this->asset->isImage()) {
return $this->getImageDimensions();
} elseif ($this->asset->isSvg()) {
return $this->getSvgDimensions();
}
$attrs = parent::get();

return [null, null];
return [$attrs['width'] ?? null, $attrs['height'] ?? null];
}

/**
* Get the width of the asset.
*
* @return int
*/
public function width()
{
return array_get($this->get(), 0);
}

/**
* Get the height of the asset.
*
* @return int
*/
public function height()
{
return array_get($this->get(), 1);
}

/**
* Get the dimensions of an image.
*
* @return array
*/
private function getImageDimensions()
{
// Since assets may be located on external platforms like Amazon S3, we can't simply
// grab the dimensions. So we'll copy it locally and read the dimensions from there.
$manager = new MountManager([
'source' => $this->asset->disk()->filesystem()->getDriver(),
'cache' => $cache = $this->getCacheFlysystem(),
]);

$cachePath = "{$this->asset->containerId()}/{$this->asset->path()}";

if ($manager->has($destination = "cache://{$cachePath}")) {
$manager->delete($destination);
}

$manager->copy("source://{$this->asset->path()}", $destination);

try {
$size = getimagesize($cache->getAdapter()->getPathPrefix().$cachePath);
} catch (\Exception $e) {
$size = [0, 0];
} finally {
$cache->delete($cachePath);
}

return $size ? array_splice($size, 0, 2) : [0, 0];
}

/**
* Get the dimensions of an SVG.
*
* @return array
*/
private function getSvgDimensions()
{
// Since assets may be located on external platforms like Amazon S3, we can't simply
// grab the dimensions. So we'll copy it locally and read the dimensions from there.
$manager = new MountManager([
'source' => $this->asset->disk()->filesystem()->getDriver(),
'cache' => $cache = $this->getCacheFlysystem(),
]);

$cachePath = "{$this->asset->containerId()}/{$this->asset->path()}";

if ($manager->has($destination = "cache://{$cachePath}")) {
$manager->delete($destination);
}

$manager->copy("source://{$this->asset->path()}", $destination);

$svg = simplexml_load_file($cache->getAdapter()->getPathPrefix().$cachePath);

$cache->delete($cachePath);

if ($svg['width'] && $svg['height']
&& is_numeric((string) $svg['width'])
&& is_numeric((string) $svg['height'])) {
return [(float) $svg['width'], (float) $svg['height']];
} elseif ($svg['viewBox']) {
$viewBox = preg_split('/[\s,]+/', $svg['viewBox'] ?: '');

return [$viewBox[2], $viewBox[3]];
}

return [300, 150];
}

private function getCacheFlysystem()
{
$disk = 'dimensions-cache';

config(["filesystems.disks.{$disk}" => [
'driver' => 'local',
'root' => storage_path('statamic/dimensions-cache'),
]]);

return Storage::disk($disk)->getDriver();
}
}
Loading