Jump to content

Override Pageimage::size? or hookAfter?


mike131
 Share

Recommended Posts

Hi all,

I am currently using a module called AmazonS3Cloudfront.

An issue that I am having is that the templates use ->width() to dynamically resize images.

I see that size method is hookable, so I can change width calls to use size($width, 0) instead.

I am a little confused on the $event parameter that is passed into the hook.

Currently I am using.

$this->addHook('Pageimage::size', $this, 'resizeTest');

and in resizeTest, i get the $event, but not too sure what is actually being passed.

My end goal is to either rewrite size() or hookafter size(), so that I can take the file generated from the resize and place it into my s3 bucket.

Any pointers on figuring out this $event parameter?

Thank you!

Link to comment
Share on other sites

Also, just a question of understanding.

Does

$this->addHook('Pageimage::size'$this'resizeTest');

actually override the Pageimage::size function?

EDIT: Ideally, i would just want to hookAfter, to receive the file so I can then upload it to s3.

Link to comment
Share on other sites

What do you want to do?

You want add / copy / submit all final files of a resize manipulation (one of width, height, size) additionally to a cloud?

This can be done by simply get the filename after a manipulation.

For this you may add your own wrapper to the pageimage functions. There is also no need to change width or height to size, because width and height are only wrappers for size. Finally size is called for all pageimage manipulations.

You can add a simple hook to pageimage like this:

wire()->addHook("Pageimage::size2cloud", null, function($event) {
    $image = $event->object;        // the event object is a pageimage
    $width = $event->arguments(0);  // first argument to pageimage::size is the width
    $height = $event->arguments(1); // yes, second is height
    $options = is_array($event->arguments(2)) ? $event->arguments(2) : array(); // and optional an options array can be passedd

    // now do the manipulation and catch the resulting variation object
    $imageVariation = $image->size($width, $height, $options);

    // get the filename for your cloud stuff
    $filename = $imageVariation->filename;

    // do your stuff here
    ...

//    // return the variation pageimage object
//    return $imageVariation;

    // what do you need to return here?

});

// in your templates now you call the images like
$image->size2cloud( 200, 0);

// or
$image->size2cloud( 200, 200, array("crop"=>false, "quality"=>70, "sharpening"=>medium));

// or 
$image->size2cloud( 0, 200);

This can be set into a template file, e.g. if you have something like a inifile that is included into all template files. Or, if do not use this technique, you build a module from the function.

Edited by horst
I'm not sure what the method need to return, because I do not know the cloud module
  • Like 1
Link to comment
Share on other sites

Ok so i think this will work.

Is there a way for me to access which "page" the image belongs too?

I know I can just search "file/1231/resizedimage.1280x0.jpg" for the 1231 portion, but I would like to use built in functions if they exist.

Thank you!

Link to comment
Share on other sites

You may refer to all that is related to pageimages in the docs and the cheatsheet.

You can everything derive from the pageimage object!

For example go to the cheatsheet and select the advanced button, then filter for files (images are also instances of files), and voila, you will see that files have ->page.

So you can use $image->page->id for the id, and everything else what belongs to page. Have a look at the cheatsheet for page. (use the advance button and / or click onto the properties and methods and then onto the more links) :)

Edited by horst
Link to comment
Share on other sites

I'm not sure if I understand you right. But if you cannot add the hook on template layer, you need to create a module:

<?php

class SizeToCloud extends WireData implements Module {

    static public function getModuleInfo() {
        return array(
            'title'    => 'Size To Cloud',
            'summary'  => __('This module handles relations between pageimages and S3 cloud', __FILE__),
            'version'  => '0.0.1',
            'author'   => '',
            'href'     => '',
            'singular' => true,
            'autoload' => true,
            // 'requires' => array('PHP>=5.3.8', 'ProcessWire>=2.5.0')
            );
    }

    public function init() {
        $this->addHook('Pageimage::size2cloud', $this, 'size2cloud');
    }

    public function size2cloud($event) {
        $image = $event->object; // the event object is a pageimage
        $width = $event->arguments(0); // first argument to pageimage::size is the width
        $height = $event->arguments(1); // yes, second is height
        $options = is_array($event->arguments(2)) ? $event->arguments(2) : array(); // and optional an options array can be passedd

        // now do the manipulation and catch the resulting variation object
        $imageVariation = $image->size($width, $height, $options);

        // get the filename for your cloud stuff
        $filename = $imageVariation->filename;

        // do your stuff here
        ...

        // // return the variation pageimage object
        // return $imageVariation;

        // what do you need to return here?
    }

}

This example code should be saved as a file named SizeToCloud.module. It should be cpoied under site/modules/SizeToCloud/SizeToCloud.module

Then refresh your modules section find it under new and install it. For more on Modules refer to the docs and to the HelloWorld.module what is a super good example module with all commented.

Edited by horst
corrected typo from copy / paste in addHook
  • Like 1
Link to comment
Share on other sites

I think there may be a slight misunderstanding.

In the Admin, there are a few fields setup, such as top_image. (Admin > Setup > Fields). This field shows up as Inputfield type image.

I am currently working to expand the AmazonS3Cloudfront module to be able to take the file that is resized and upload it to the cloud.

In the template for instance, I have:

$page->image->size2cloud(1280, 0); // This works

$page->top_image->size2cloud(1280,0) // Does not work.

I was wondering if there was a class to add the size2cloud hook to, so that ANYTIME size is called, I can take over with the size2cloud function.

I hope this makes sense. If it doesnt please let me know.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...