Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glide does handle unsupported file formats. #2122

Closed
wants to merge 3 commits into from
Closed
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
52 changes: 51 additions & 1 deletion src/Tags/Glide.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@
use Statamic\Facades\Asset;
use Statamic\Facades\Config;
use Statamic\Facades\Image;
use Statamic\Facades\Path;
use Statamic\Facades\URL;
use Statamic\Imaging\ImageGenerator;
use Statamic\Support\Str;

class Glide extends Tags
{
/**
* Allowed file formats for the gd image manipulation driver in combinatino with glide.
*
* Glide does use the intervention package under the hood.
* See http://image.intervention.io/getting_started/formats for mor information.
*/
const ALLOWED_FILE_FORMATS_GD = ['jpeg', 'jpg', 'png', 'gif', 'webp'];

/**
* Allowed file formats for the imagick image manipulation driver in combination with glide.
*
* Glide does use the intervention package under the hood.
* See http://image.intervention.io/getting_started/formats for mor information.
*/
const ALLOWED_FILE_FORMATS_IMAGICK = ['jpeg', 'jpg', 'png', 'gif', 'tif', 'bmp', 'psd', 'webp'];

/**
* Maps to {{ glide:[field] }}.
*
Expand Down Expand Up @@ -156,7 +173,9 @@ private function output($url)
private function generateGlideUrl($item)
{
try {
$url = $this->getManipulator($item)->build();
// In case the given item extension is not supported by glide
// and herby not resizable, the original path will be returned.
$url = $this->isResizable($item) ? $this->getManipulator($item)->build() : $this->normalizeItem($item);
} catch (\Exception $e) {
\Log::error($e->getMessage());

Expand Down Expand Up @@ -265,4 +284,35 @@ private function getServer()
{
return app(Server::class);
}

/**
* Checking against a whitelist of allowed file extensions.
*
* @param string $item
* @return bool
*/
private function isResizable($item)
{
return in_array(strtolower(Path::extension($item)), $this->allowedFileFormats());
}

/**
* The whitelist with allowed file extensiosn will be returned,
* depending on the chosen image manipulation driver.
*
* @throws \Exception
* @return array
*/
private function allowedFileFormats()
{
if (extension_loaded('gd')) {
return self::ALLOWED_FILE_FORMATS_GD;
}

if (extension_loaded('imagick')) {
return self::ALLOWED_FILE_FORMATS_IMAGICK;
}

throw new \Exception('To use glide, you need to have GD or Imagick installed.');
}
}