Jump to:

382 Posts in 210 Topics by 196 members

Connect With Other SilverStripe Members

SilverStripe Forums » Connect With Other SilverStripe Members » Chaining of Image mnioulation methods

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

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

Page: 1
Go to End
Author Topic: 890 Views
  • Andre
    Avatar
    Community Member
    126 Posts

    Chaining of Image mnioulation methods Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Chaining of Image mnioulation methods Link to this post

    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.

    890 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.