Skip to content

Commit cdd1e00

Browse files
committed
Ignoring phar archives (see #193)
1 parent 03edd7f commit cdd1e00

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

Adapter/GD.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Gregwar\Image\Image;
66
use Gregwar\Image\ImageColor;
7+
use Gregwar\Image\Utils\FileUtils;
78

89
class GD extends Common
910
{
@@ -609,7 +610,7 @@ public function saveJpeg($file, $quality)
609610
*/
610611
protected function openJpeg($file)
611612
{
612-
if (file_exists($file) && filesize($file)) {
613+
if (FileUtils::safeExists($file) && filesize($file)) {
613614
$this->resource = @imagecreatefromjpeg($file);
614615
} else {
615616
$this->resource = false;
@@ -621,7 +622,7 @@ protected function openJpeg($file)
621622
*/
622623
protected function openGif($file)
623624
{
624-
if (file_exists($file) && filesize($file)) {
625+
if (FileUtils::safeExists($file) && filesize($file)) {
625626
$this->resource = @imagecreatefromgif($file);
626627
} else {
627628
$this->resource = false;
@@ -633,7 +634,7 @@ protected function openGif($file)
633634
*/
634635
protected function openPng($file)
635636
{
636-
if (file_exists($file) && filesize($file)) {
637+
if (FileUtils::safeExists($file) && filesize($file)) {
637638
$this->resource = @imagecreatefrompng($file);
638639
} else {
639640
$this->resource = false;
@@ -645,7 +646,7 @@ protected function openPng($file)
645646
*/
646647
protected function openWebp($file)
647648
{
648-
if (file_exists($file) && filesize($file)) {
649+
if (FileUtils::safeExists($file) && filesize($file)) {
649650
$this->resource = @imagecreatefromwebp($file);
650651
} else {
651652
$this->resource = false;

Source/File.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Gregwar\Image\Source;
44

55
use Gregwar\Image\Image;
6+
use Gregwar\Image\Utils\FileUtils;
67

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

2930
public function guessType()
3031
{
31-
if (function_exists('exif_imagetype')) {
32+
if (function_exists('exif_imagetype') && FileUtils::safeExists($this->file)) {
3233
$type = @exif_imagetype($this->file);
3334

3435
if (false !== $type) {

Utils/FileUtils.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Gregwar\Image\Utils;
4+
5+
class FileUtils
6+
{
7+
/**
8+
* Checks that the file exists and is not a phar archive
9+
*/
10+
public static function safeExists($file)
11+
{
12+
return file_exists($file) && !self::isPhar($file);
13+
}
14+
15+
/**
16+
* Checks if the file is a phar archive
17+
*/
18+
public static function isPhar($file)
19+
{
20+
return substr(strtolower($file), 0, 7) === 'phar://';
21+
}
22+
}

0 commit comments

Comments
 (0)