Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Image Memory Fail - solution (sort of)

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

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

Page: 1
Go to End
Author Topic: 209 Views
  • Futureweb
    Avatar
    Community Member
    232 Posts

    Image Memory Fail - solution (sort of) Link to this post

    Hi,

    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
          increase_time_limit_to(300);

          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->setGD($img);
                      break;
                   }
                }
             }
          }
          
          $this->quality = self::$default_quality;
          parent::__construct();
       }

    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?

    209 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.