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
API: Prevent large images from repeatedly crashing PHP on resize #2859
Merged
simonwelsh
merged 1 commit into
silverstripe:master
from
kinglozzer:gd-resize-crash-mast
Jul 16, 2014
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Wipe the cache of failed image manipulations. When {@link GDBackend} attempts to resample an image, it will write | ||
* the attempted manipulation to the cache and remove it from the cache if the resample is successful. The objective | ||
* of the cache is to prevent fatal errors (for example from exceeded memory limits) from occurring more than once. | ||
* | ||
* @package framework | ||
* @subpackage filesystem | ||
*/ | ||
class CleanImageManipulationCache extends BuildTask { | ||
|
||
protected $title = 'Clean Image Manipulation Cache'; | ||
|
||
protected $description = 'Clean the failed image manipulation cache. Use this to allow SilverStripe to attempt | ||
to resample images that have previously failed to resample (for example if memory limits were exceeded).'; | ||
|
||
/** | ||
* Check that the user has appropriate permissions to execute this task | ||
*/ | ||
public function init() { | ||
if(!Director::is_cli() && !Director::isDev() && !Permission::check('ADMIN')) { | ||
return Security::permissionFailure(); | ||
} | ||
|
||
parent::init(); | ||
} | ||
|
||
/** | ||
* Clear out the image manipulation cache | ||
* @param SS_HTTPRequest $request | ||
*/ | ||
public function run($request) { | ||
$failedManipulations = 0; | ||
$processedImages = 0; | ||
$images = DataObject::get('Image'); | ||
|
||
if($images && Image::get_backend() == "GDBackend") { | ||
$cache = SS_Cache::factory('GDBackend_Manipulations'); | ||
|
||
foreach($images as $image) { | ||
$path = $image->getFullPath(); | ||
|
||
if (file_exists($path)) { | ||
$key = md5(implode('_', array($path, filemtime($path)))); | ||
|
||
if ($manipulations = unserialize($cache->load($key))) { | ||
$failedManipulations += count($manipulations); | ||
$processedImages++; | ||
$cache->remove($key); | ||
} | ||
} | ||
} | ||
} | ||
|
||
echo "Cleared $failedManipulations failed manipulations from | ||
$processedImages Image objects stored in the Database."; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ class GDTest extends SapphireTest { | |
'png32' => 'test_png32.png' | ||
); | ||
|
||
public function tearDown() { | ||
$cache = SS_Cache::factory('GDBackend_Manipulations'); | ||
$cache->clean(Zend_Cache::CLEANING_MODE_ALL); | ||
} | ||
|
||
/** | ||
* Loads all images into an associative array of GD objects. | ||
* Optionally applies an operation to each GD | ||
|
@@ -135,4 +140,76 @@ function testGreyscale() { | |
$samplesPNG32 = $this->sampleAreas($images['png32']); | ||
$this->assertGreyscale($samplesPNG32, 8); | ||
} | ||
} | ||
|
||
/** | ||
* Tests that GD doesn't attempt to load images when they're deemed unavailable | ||
* @return void | ||
*/ | ||
public function testImageSkippedWhenUnavailable() { | ||
$fullPath = realpath(dirname(__FILE__) . '/gdtest/test_jpg.jpg'); | ||
$gd = new GDBackend_ImageUnavailable($fullPath); | ||
|
||
/* Ensure no image resource is created if the image is unavailable */ | ||
$this->assertNull($gd->getImageResource()); | ||
} | ||
|
||
/** | ||
* Tests the integrity of the manipulation cache when an error occurs | ||
* @return void | ||
*/ | ||
public function testCacheIntegrity() { | ||
$fullPath = realpath(dirname(__FILE__) . '/gdtest/test_jpg.jpg'); | ||
|
||
try { | ||
$gdFailure = new GDBackend_Failure($fullPath, array('SetWidth', 123)); | ||
$this->fail('GDBackend_Failure should throw an exception when setting image resource'); | ||
} catch (GDBackend_Failure_Exception $e) { | ||
$cache = SS_Cache::factory('GDBackend_Manipulations'); | ||
$key = md5(implode('_', array($fullPath, filemtime($fullPath)))); | ||
|
||
$data = unserialize($cache->load($key)); | ||
|
||
$this->assertArrayHasKey('SetWidth|123', $data); | ||
$this->assertTrue($data['SetWidth|123']); | ||
} | ||
} | ||
|
||
/** | ||
* Test that GD::failedResample() returns true for the current image | ||
* manipulation only if it previously failed | ||
* @return void | ||
*/ | ||
public function testFailedResample() { | ||
$fullPath = realpath(dirname(__FILE__) . '/gdtest/test_jpg.jpg'); | ||
|
||
try { | ||
$gdFailure = new GDBackend_Failure($fullPath, array('SetWidth-failed', 123)); | ||
$this->fail('GDBackend_Failure should throw an exception when setting image resource'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise. |
||
} catch (GDBackend_Failure_Exception $e) { | ||
$gd = new GDBackend($fullPath, array('SetWidth', 123)); | ||
$this->assertTrue($gd->failedResample($fullPath, 'SetWidth-failed|123')); | ||
$this->assertFalse($gd->failedResample($fullPath, 'SetWidth-not-failed|123')); | ||
} | ||
} | ||
|
||
} | ||
|
||
class GDBackend_ImageUnavailable extends GDBackend implements TestOnly { | ||
|
||
public function imageAvailable($filename, $manipulation) { | ||
return false; | ||
} | ||
|
||
} | ||
|
||
class GDBackend_Failure extends GDBackend implements TestOnly { | ||
|
||
public function setImageResource($resource) { | ||
throw new GDBackend_Failure_Exception('GD failed to load image'); | ||
} | ||
|
||
} | ||
|
||
class GDBackend_Failure_Exception extends Exception { | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should have a $this->failure() to make sure an exception's thrown.