Skip to content

Commit

Permalink
Ignoring phar archives (see Gregwar#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregwar committed Sep 23, 2024
1 parent 03edd7f commit cdd1e00
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
9 changes: 5 additions & 4 deletions Adapter/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Gregwar\Image\Image;
use Gregwar\Image\ImageColor;
use Gregwar\Image\Utils\FileUtils;

class GD extends Common
{
Expand Down Expand Up @@ -609,7 +610,7 @@ public function saveJpeg($file, $quality)
*/
protected function openJpeg($file)
{
if (file_exists($file) && filesize($file)) {
if (FileUtils::safeExists($file) && filesize($file)) {
$this->resource = @imagecreatefromjpeg($file);
} else {
$this->resource = false;
Expand All @@ -621,7 +622,7 @@ protected function openJpeg($file)
*/
protected function openGif($file)
{
if (file_exists($file) && filesize($file)) {
if (FileUtils::safeExists($file) && filesize($file)) {
$this->resource = @imagecreatefromgif($file);
} else {
$this->resource = false;
Expand All @@ -633,7 +634,7 @@ protected function openGif($file)
*/
protected function openPng($file)
{
if (file_exists($file) && filesize($file)) {
if (FileUtils::safeExists($file) && filesize($file)) {
$this->resource = @imagecreatefrompng($file);
} else {
$this->resource = false;
Expand All @@ -645,7 +646,7 @@ protected function openPng($file)
*/
protected function openWebp($file)
{
if (file_exists($file) && filesize($file)) {
if (FileUtils::safeExists($file) && filesize($file)) {
$this->resource = @imagecreatefromwebp($file);
} else {
$this->resource = false;
Expand Down
3 changes: 2 additions & 1 deletion Source/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Gregwar\Image\Source;

use Gregwar\Image\Image;
use Gregwar\Image\Utils\FileUtils;

/**
* Open an image from a file.
Expand All @@ -28,7 +29,7 @@ public function correct()

public function guessType()
{
if (function_exists('exif_imagetype')) {
if (function_exists('exif_imagetype') && FileUtils::safeExists($this->file)) {
$type = @exif_imagetype($this->file);

if (false !== $type) {
Expand Down
22 changes: 22 additions & 0 deletions Utils/FileUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Gregwar\Image\Utils;

class FileUtils
{
/**
* Checks that the file exists and is not a phar archive
*/
public static function safeExists($file)
{
return file_exists($file) && !self::isPhar($file);
}

/**
* Checks if the file is a phar archive
*/
public static function isPhar($file)
{
return substr(strtolower($file), 0, 7) === 'phar://';
}
}

0 comments on commit cdd1e00

Please sign in to comment.