Jump to content

Admin Controller what file is it calling?


mike131
 Share

Recommended Posts

Hi I am trying to figure out where TinyMCE or Processwire makes the call to resize images in a blog post.

The URL i see for the resize call is /admin/page/image/resize?id=1331&file=1331,screen_shot_2014-12-05_at_1_11_11_pm.png&width=825&height=671

I would like to know where I can look to see the actual function that is called from this url.

Please let me know if this is unclear.

Thanks in advance!

Link to comment
Share on other sites

I don't think this is it.

I think it is ProcessPageEditImageSelect.module. I see the ___executeResize function returning the response that I see from the url.

I want to try to hook after this function in order to take that file and upload it to to cloud.

$this->addHookAfter('ProcessPageEditImageSelect::executeResize', $this, 'sizeCloud');

When i do this, the resize call's response is now:

{"error":true,"message":"Method ProcessPageEditImageSelect::size does not exist or is not callable in this context"}

Any ideas on what could be the issue?

EDIT: The sizeCloud function (which is located in a third party module): 

/* Resizes image and sends to cloud */
    public function sizeCloud($event) {
        $image = $event->object;
        $width = $event->arguments(0);
        $height = $event->arguments(1);
        $options = is_array($event->arguments(2)) ? $event->arguments(2) : array(); // optional

        $resizedImage = $image->size($width, $height, $options);

        $filename = $resizedImage->filename;

        // Add to S3

        $s3path = $image->page->id . "/" . $resizedImage->name;
        $pathToFile = $filename;

        //var_dump($pathToFile);
        //return;

        $s3args = array(
            'Bucket' => $this->s3_bucket,
            'Key' => $s3path,
            'SourceFile' => $pathToFile,
            'ACL' => $this->select_acl,
            'Metadata' => array(
                'original_url' => $resizedImage->url,
                'info' => 'Uploaded via the AmazonS3Cloudfront PW Module'
            )
        );

        if(is_int($this->cache)) {
            $s3args['CacheControl'] = 'max-age=' . $this->cache;
        }

        try {
            $result = $this->client->putObject($s3args);
        }
        catch(Exception $e) {
            throw new WireException("Error: File not added to S3: {$e->getMessage()}");
        }

        // Return the resized object
        return $resizedImage;
    }
Link to comment
Share on other sites

I think it must be something in your sizeCloud method, because my quick testing with the ProcessPageEditImageSelect::executeResize hook seems to be working fine. Sorry about pointing you to the wrong function by the way.

It is to do with your:

$resizedImage = $image->size($width, $height, $options);

Which means that $image is not the object you are looking for. Let me go on my way - to investigate a little more :)

  • Like 1
Link to comment
Share on other sites

Ok, I should have just looked at the source for that function in the first place :)

This is what you need:

$image = $event->object->getPageimage();
$width = (int) $this->input->get->width; 
$height = (int) $this->input->get->height; 
Link to comment
Share on other sites

Ok, I should have just looked at the source for that function in the first place :)

This is what you need:

$image = $event->object->getPageimage();
$width = (int) $this->input->get->width; 
$height = (int) $this->input->get->height; 

Will try it out and report back.

Thank you!

Link to comment
Share on other sites

So it almost works.

It is uploading the resized image, however the image the blog post is looking for is not the same as what gets saved.

The image that gets saved is screen_shot_2014-12-05_at_1_11_11_pm.540x0.png

The image that the blog tries to use is screen_shot_2014-12-05_at_1_11_11_pm.540x0-is.png

It seems to append -is to the filename. I am not sure where this is coming from and dont want to just append '-is' in the filenames, I would rather try to get this working properly.

Link to comment
Share on other sites

If I read it right, Blog want to get those, but why? Where is the code that do that? I don't know Blog (module / profile?).

Suffixes are introduced since PW 2.4.15. Suffixes can be added to a variation name with the options:

// if the original images name is basename.ext, the call for a width with a suffix "is"

$options = array("suffix" => "is");
$image->width(200, $options);

// results in this variation name: basename.200x0-is.ext

So, the question is: Why, where or how does Blog trigger to create a variations name with suffix "is"?

Pageimage::size builds the variationnames according to width, height, and the optional options cropping & suffix. Pointing me to the code would be of help.

EDIT:

Ah, now I saw that you hook into a process and "emulate" it by yourself: $this->input->get->width;

Have a look to the original function and see if it defines / set a suffix named "is". If so, you need to add it to your emulation too.

This will be fairly simple, if it is a static suffix.

EDIT2:

The line seems to be here: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/modules/Process/ProcessPageEditImageSelect/ProcessPageEditImageSelect.module#L342

Edited by horst
Link to comment
Share on other sites

Ok, that -is suffix is also new to me, at least that it is implemented now. :)

I remember we (Ryan, Nico, me) have had a talk 6 month ago that it would be good if image variations belonging to RTEs get flagged somehow. Without a flag there is / was a great chance that they get deleted with a call for $image->removeVariations(). Whereas all API created image variations in templates get recreated on demand after such a call, RTE-belonging images don't get recreated and the pages output was / is broken. Therefore it is a good implementation to flag them.

@mike131: I think you should add the same code to your emulation:

    $image = $event->object->getPageimage();
    $width = (int) $this->input->get->width; 
    $height = (int) $this->input->get->height; 

    $options = array();
    if ($width < $image->width) {
        // add a suffix if it is a resized version, don't add a suffix if it is the original unresized image
        $options["suffix"] = array("is");
    }

    $resizedImage = $image->size($width, $height, $options);

    $filename = $resizedImage->filename;

...
Edited by horst
  • Like 1
Link to comment
Share on other sites

Ok, that -is suffix is also new to me, at least that it is implemented now. :)

I remember we (Ryan, Nico, me) have had a talk 6 month ago that it would be good if image variations belonging to RTEs get flagged somehow. Without a flag there is / was a great chance that they get deleted with a call for $image->removeVariations(). Whereas all API created image variations in templates get recreated on demand after such a call, RTE-belonging images don't get recreated and the pages output was / is broken. Therefore it is a good implementation to flag them.

@mike131: I think you should add the same code to your emulation:

    $image = $event->object->getPageimage();
    $width = (int) $this->input->get->width; 
    $height = (int) $this->input->get->height; 

    $options = array();
    if ($width < $image->width) {
        // add a suffix if it is a resized version, don't add a suffix if it is the original unresized image
        $options["suffix"] = array("is");
    }

    $resizedImage = $image->size($width, $height, $options);

    $filename = $resizedImage->filename;

...

Hi horst, thank you for your help again.

I The function that I am using to hook after executeresize is:

/* Resizes image and sends to cloud */
    public function sizeBlogCloud($event) {
        $image = $event->object->getPageImage();
        $width = (int) $this->input->get->width;//$event->arguments(0);
        $height = (int) $this->input->get->height; //$event->arguments(1);
        $options = is_array($event->arguments(2)) ? $event->arguments(2) : array(); // optional
        
        //$resizedImage = $image->size($width, $height, array('suffix' => array('is')));
        if ($width < $image->width) {
            // add a suffix if it is a resized version, don't add a suffix if it is the original unresized image
            $suffix = isset($options["suffix"]) ? $options["suffix"] : array();
            $suffix = is_array($suffix) ? $suffix : array($suffix);
            $options["suffix"] = array_merge($suffix, array("is"));
        }
        
        $resized = $image->size($width, $height, $options);

        $filename = $resized->filename;

        // Add to S3

        $s3path = $image->page->id . "/" . $resized->name;
        $pathToFile = $filename;

        //var_dump($pathToFile);
        //return;

        $s3args = array(
            'Bucket' => $this->s3_bucket,
            'Key' => $s3path,
            'SourceFile' => $pathToFile,
            'ACL' => $this->select_acl,
            'Metadata' => array(
                'original_url' => $resized->url,
                'info' => 'Uploaded via the AmazonS3Cloudfront PW Module'
            )
        );

        if(is_int($this->cache)) {
            $s3args['CacheControl'] = 'max-age=' . $this->cache;
        }

        try {
            $result = $this->client->putObject($s3args);
        }
        catch(Exception $e) {
            throw new WireException("Error: File not added to S3: {$e->getMessage()}");
        }

        // Return the resized object
        return $resized;
    }

One issue I am seeing now is that the file that gets saved to the cloud looks like:

cache.example.com/files/1332/screen_shot_2014-11-18_at_12_38_09_pm.825x0-is.png

while the file that the blog body looks for is:

cache.example.com/files/1332/screen_shot_2014-11-18_at_12_38_09_pm.825x263-is.png

From what I can tell. ProcesswireBlog module saves the <img> tags in the blog body to the database, so I am using

$blog_body = str_replace('serverPathToFiles', 'cloudPathToFiles', $blog_body);

So there is a difference between what is saved and the resized filename.

Any ideas on why this could be? Am I calling size when it should really be a width call?

Link to comment
Share on other sites

Am I calling size when it should really be a width call?

exactly!

You can call size() with second param = 0 (zero), what is the same as calling width().

Pageimage::size build the names according to passed values for width, height and optional options for cropping and suffix:

basename.widthxheight-suffix.ext

and the code of the function for blog images should be look like that, because there is no $event->arguments(2) available like it could be when calling from templatecode:

    public function sizeBlogCloud($event) {
        $image = $event->object->getPageImage();
        $width = (int) $this->input->get->width;//$event->arguments(0);
        // $height = (int) $this->input->get->height; //$event->arguments(1);  // not needed here, or set it to 0
        
        $options = array();

        if ($width < $image->width) {
            // add a suffix if it is a resized version, don't add a suffix if it is the original unresized image
            $options["suffix"] = array("is");
        }
        
        $resized = $image->size($width, 0, $options);

Edited by horst
  • Like 3
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...