1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-08-16 13:04:13 +02:00

improving error handling when opening (non)-recoverable images

This commit is contained in:
Mikael Roos
2014-08-21 02:21:32 +02:00
parent 2643d4954f
commit 1a6cef6580

View File

@@ -788,6 +788,37 @@ class CImage
/**
* Error message when failing to load somehow corrupt image.
*
* @return void
*
*/
public function failedToLoad()
{
header("HTTP/1.0 404 Not Found");
echo("Fatal error when opening image.<br>");
switch ($this->fileExtension) {
case 'jpg':
case 'jpeg':
$this->image = imagecreatefromjpeg($this->pathToImage);
break;
case 'gif':
$this->image = imagecreatefromgif($this->pathToImage);
break;
case 'png':
$this->image = imagecreatefrompng($this->pathToImage);
break;
}
exit();
}
/** /**
* Load image from disk. * Load image from disk.
* *
@@ -797,7 +828,7 @@ class CImage
* @return $this * @return $this
* *
*/ */
public function load($src = null, $dir = null) public function load($src = null, $dir = null)
{ {
if (isset($src)) { if (isset($src)) {
$this->setSource($src, $dir); $this->setSource($src, $dir);
@@ -805,21 +836,21 @@ class CImage
$this->log("Opening file as {$this->fileExtension}."); $this->log("Opening file as {$this->fileExtension}.");
switch ($this->fileExtension) { switch ($this->fileExtension) {
case 'jpg': case 'jpg':
case 'jpeg': case 'jpeg':
$this->image = @imagecreatefromjpeg($this->pathToImage); $this->image = @imagecreatefromjpeg($this->pathToImage);
$this->image or die("Fatal error when opening image."); $this->image or failedToLoad();
break; break;
case 'gif': case 'gif':
$this->image = @imagecreatefromgif($this->pathToImage); $this->image = @imagecreatefromgif($this->pathToImage);
$this->image or die("Fatal error when opening image."); $this->image or failedToLoad();
break; break;
case 'png': case 'png':
$this->image = @imagecreatefrompng($this->pathToImage); $this->image = @imagecreatefrompng($this->pathToImage);
$this->image or die("Fatal error when opening image."); $this->image or failedToLoad();
$type = $this->getPngType(); $type = $this->getPngType();
$hasFewColors = imagecolorstotal($this->image); $hasFewColors = imagecolorstotal($this->image);
@@ -830,10 +861,10 @@ class CImage
} }
$this->palette = true; $this->palette = true;
} }
break; break;
default: default:
$this->image = false; $this->image = false;
throw new Exception('No support for this file extension.'); throw new Exception('No support for this file extension.');
} }