0

I'm working on a module. It uses an API to serve images. One of it's features is calling for a custom resolution image by GET request. So for example

myapi.com/img4324342?w=22&h=55.

I'm currently abusing the style function from the Image module.

I call hook_preprocess_image() and if it detects my effect (my custom style that has my custom effect) it has to return a modified path. But the path get's malformed when I apply a style. I just want to have the original URI so I can use the information inside to construct a REST call. How to prevent that the URI changes after style a.k.a. how to return the original URI as Path?

Background:

It's a brand portal and I'm an intern making the drupal plugin. The company I work for doesn't want the images to be served from the Drupal hosting, only provided by the API. But they ALSO want that adding a different banner for example to every article is still done the same way you normally do: by adding a image style and adding it as a field to the content type

The $vars variabele from hook_preprocess_image() in json looks like this:

{
 "path":"?itok=D1zdawxe",
 "alt":null,
 "style_name":"thumbnail",
 "width":null,
 "height":null,
 "title":null,
 "attributes":[],
 "theme_hook_original":"image",
 "theme_hook_suggestions":[]
}

after applying any effect.

This means that when I don't apply any effect my Path (my Uri) is myscheme://img23423423432 and when I do apply ANY effect it get's malformed to random gibberish like ?itok=D1zdawxe

9
  • What does the path change from and to, and why? Do you mean, for example, that public://image.jpg changes to public://styles/foo/image.jpg? If so, that's what's supposed to happen. To get around it, simply copy the image file you've just created to the path you want to serve it from. You would have to have the original URI to even get the new style URI, so it's not clear why you're asking how to get that - you already have it, surely? You should post the relevant code to resolve all these ambiguities, your current explanation isn't detailed enough to understand what you're doing
    – Clive
    Commented Oct 7, 2015 at 12:48
  • it changes from myscheme://image423423 to ?itok=D1zdawxe
    – Thomas
    Commented Oct 7, 2015 at 13:02
  • As a result of....what? Where's the code that makes that happen? What are you doing to invoke the image style creation? I know you're extremely reluctant to share any of your code for some reason, but you're not likely to get any help unless you do. People don't have time to prepare an answer based on guesses, they need real information to go on
    – Clive
    Commented Oct 7, 2015 at 13:15
  • I added it to the question as well, but as result of the application of an effect, doesn't matter which one.
    – Thomas
    Commented Oct 7, 2015 at 13:17
  • 1
    Getting a bit closer I think - you should probably be implementing hook_preprocess_image_style instead (that comes before theme_image() in this case) - see what vars you have available in that hook
    – Clive
    Commented Oct 7, 2015 at 13:30

1 Answer 1

0

I've found a kinda nasty solution:

hook_preprocess_image_style(&$variables) {
    $variables['uri'] = $variables['path'];
}

hook_preprocess_image(&$vars) {
    if(array_key_exists('uri', $vars)) {
        $vars['path'] = $vars['uri'];
    }

    if(!isset($vars['style_name'])){
        return FALSE;
    }

    $image_styles = image_styles();

    foreach ($image_styles as $image_style_key => $image_style) {
        if ($vars['style_name'] == $image_style_key) {
            foreach ($image_style['effects'] as $effect_key => $effect) {
                if ($effect['name'] == 'myEffect') {
                    $vars['path'] = generateAPIURL($vars['path'], $effect['data']); //get baseURL from path and URL parameters from data
                    break;
                 }
            }
        }
    }
    return FALSE;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.