Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Image Memory Fail - solution (sort of)

Go to End



Community Member, 244 Posts

29 May 2013 at 3:13pm


I've always been very frustrated by the way SilverStripe dies quite catastrophically when someone uploads an image that is too 'big'.
I've had so many urgent calls from clients who've uploaded a large image, and can no longer access the admin area, and I have to search for any recently uploaded large image.

Limiting the upload size isn't really a solution, as the jpeg may be very compressed, but still have huge pixel dimensions - therefore require a lot of memory to resize.

So, the solution is to check pixel dimensions, and see if that's greater than the memory allocated to php.

The file in question is framework/filesystem/GD.php

I've added bit more into the "public function __construct($filename = null) " function:

public function __construct($filename = null) {
		// If we're working with image resampling, things could take a while.  Bump up the time-limit

		if($filename) {
			// We use getimagesize instead of extension checking, because sometimes extensions are wrong.
			list($width, $height, $type, $attr) = getimagesize($filename);
			$memoryRequired = $width * $height * 4; // 4 bytes per pixel
			$memoryAvailable = memory_get_usage();
			if ($memoryRequired > $memoryAvailable) {
				echo "<p>resizing <strong>". $filename. "</strong> requires more memory than is currently available. $memoryRequired / $memoryAvailable.</p>" ;
			} else {
				switch($type) {
					case 1: if(function_exists('imagecreatefromgif')) $this->setGD(imagecreatefromgif($filename)); break;
					case 2: if(function_exists('imagecreatefromjpeg')) $this->setGD(imagecreatefromjpeg($filename)); break;
					case 3: if(function_exists('imagecreatefrompng')) {
						$img = imagecreatefrompng($filename);
						imagesavealpha($img, true); // save alphablending setting (important)
		$this->quality = self::$default_quality;

The problem now is that the warning looks really messy - and swapping between pages ends up with odd code scattered around.
But at least the admin area doesn't break, and the client can deal with the problem without have to urgently phone me up.

My next mission is to make this error warning feed back in to SilverStripe admin somewhere. Any ideas anyone?