0

I made code to add a custom effect using hook_image_effect_info() that does not alter the image for real. I'd like to serve an image from my own API instead, that has calls like myapi.com/img2123?w=25&h=88&watermark=true. I don't think that the code for a placeholder effect is relevant is it?

I'd like to use this because that way if I add an Image field to a content type I can choose a banner for example on every 'Article' content type.also I need to pass the parameter returned by the dimensions callback (set in the form callback of hook_image_effect_info()) to the GET request as an URL parameter.

I was thinking: could I use some node altering hook to change the uri that is parsed somewhere else in the program?

If you need some additional info, just ask. Thanks in advance.

1

The only way to abuse image styles in the way you described would be to issue a redirect when your image effect callback gets invoked. This is inefficient and will only work if no derivative has been created yet.

A better way to dynamically replace image paths would be via hook_preprocess_image() (see theme_image() for available variables):

/**
 * Implements hook_preprocess_HOOK().
 */
function mymodule_preprocess_image(&$vars) {
  if($vars['path'] === 'arbitrary/condition') {
    $vars['path'] = url('http://my.api/', array('query' => array(
      'width' => $vars['width'],
      'height' => $vars['height'],
    )));
  }
}
  • Is there a way to keep my Path/URI the same after "applying" an effect? Now it changes it to '?itok=gAsj7I_f' mumbo-jumbo. Then my solution is complete. – Thomas Oct 7 '15 at 9:56
  • If you're simply replacing the path, you can just strip the token, since you're no longer using Drupal to create image derivatives. You could alternatively implement a custom image effect that queries your API for the watermark bitmap data before applying it to the image. In that case you don't need to alter the path. Just make sure to add your custom effect to your image styles. – Fabian Iwand Oct 7 '15 at 10:07
  • Let me rephrase that: if I choose no style on my image it's path is an URI to my API. I use this uri to get the full URL to my API. As soon as I apply any style (not even my style, for example crop too) it will change my Path and break the image. How can I force the effect/style to not alter my Path (URI)? – Thomas Oct 7 '15 at 10:14
  • Don't use an image style and render the original image (default) instead? I still don't fully understand the purpose of your API. – Fabian Iwand Oct 7 '15 at 10:31
  • 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. – Thomas Oct 7 '15 at 10:41
1

If you are getting the image from another server/site you don't need an image field. If the link isn't going to change, you can store it in a text field and build a link like this

your_module_node_view($node, $view_mode, $langcode) {
    $url = $node->field_cool_image[LANGUAGE_NONE][0]['value'];
    //redefine field_cool_image to display your image
    $node['content']['field_cool_image'][0] = array(
        '#markup' = '<img src="' . $url . '" alt="my cool image" />'
    );

}

otherwise build the link when the node view is built.

your_module_node_view($node, $view_mode, $langcode){
    //whatever method you use to build a url
    $url = my_build_url($node);
    $node->['content']['my_cool_img'][0] = array(
        '#markup' = '<img src="' . $url . '" alt="my cool image" />'
    );
}
  • Thank you for anwering as well. In my case it just doesn't work, because I really want to use Image fields. I'm writing an module in which people can choose their own images and dimensions via the api. As soon as I get 15 point you'll deserve an upvote too. – Thomas Oct 7 '15 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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