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.

Connect With Other SilverStripe Members /

For all SilverStripe-related topics that don't fit into any of the categories above.

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

Chaining of Image mnioulation methods


Reply


2 Posts   1006 Views

Avatar
Andre

Community Member, 126 Posts

19 January 2013 at 10:00am

Hi there, I wrote a nice extension to the Image Object, which allows me to merge Images together, or blur them (by a gaussian algorithm), or also set the Backgroundcolor on a SetSize Method. (will put it on Github soon)

What I now was missing was the ability, to chain the methods.

Like, I want to resize an Image and blur it (to make a small Preview).

The Method Looks like this:

$MyObject->FullImage()->SetWidth(150)->Blur()

To achieve this, there is a small Piece of Code that needs to be added to the Image.php File.

Here is the original Code:

/**
    * Return an image object representing the image in the given format.
    * This image will be generated using generateFormattedImage().
    * The generated image is cached, to flush the cache append ?flush=1 to your URL.
    * @param string $format The name of the format.
    * @param string $arg1 An argument to pass to the generate function.
    * @param string $arg2 A second argument to pass to the generate function.
    * @return Image_Cached
    */
   function getFormattedImage($format, $arg1 = null, $arg2 = null) {
      if($this->ID && $this->Filename && Director::fileExists($this->Filename)) {
         $cacheFile = $this->cacheFilename($format, $arg1, $arg2);

         if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) {
            $this->generateFormattedImage($format, $arg1, $arg2);
         }
         
         $cached = new Image_Cached($cacheFile);
         // Pass through the title so the templates can use it
         $cached->Title = $this->Title;
         return $cached;
      }
   }

and Here are the Lines for ParentID and ID added, to make chaining available.

   function getFormattedImage($format, $arg1 = null, $arg2 = null) {
      if($this->ID && $this->Filename && Director::fileExists($this->Filename)) {
         $cacheFile = $this->cacheFilename($format, $arg1, $arg2);

         if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) {
            $this->generateFormattedImage($format, $arg1, $arg2);
         }
         
         $cached = new Image_Cached($cacheFile);
         // Pass through the title so the templates can use it
         $cached->Title = $this->Title;
$cached->ID = $this->ID;
$cached->ParentID = $this->ParentID;
         return $cached;
      }
   }

Is it possible, to add this to the Framework (in my case it's still 2.4.x) permanently?

Regards

Andre

Avatar
Willr

Forum Moderator, 5513 Posts

24 January 2013 at 8:27pm

Patches more than welcome!

It is changing the API for it would probably end up in the dev branch (3.2). Depending on the backwards compatibility and what it could break but post-2.4 branch is also getting minor enhancements and improvements for 2.4.