2

I have a site I'm working on where images are inserted into the body of blog nodes and I'm wanting to return these images in a view. I'm using computed field to successfully return the url of the images in the view, but I would like to apply an imagecache style to this before displaying it using

<img src = "[computed_field]">

Does anyone know the best way to do this? I've been searching around and have not been able to successfully do this yet. Thanks!

3
  • You may want to refine your requirements. The "best way" depends on all your needs. For example, one solution here could be to use the image_style_url function in your computed field so that the image URL is already set to your desired style, however you've not specified whether you need the original URL as well. – Shawn Conn Oct 14 '15 at 22:02
  • Aha! I was trying something like that, but am a novice PHP coder. My computed field function currently looks like this preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $entity->body[LANGUAGE_NONE][0]['value'], $matches); $entity_field[0]['value'] = $matches['1']['0']; Any chance you can help me add in the image_style_url code to that function? For now I just cropped the images with CSS which works fairly well, but I'd love to wrap my head around this. Thank you for your help! – phandolin Oct 14 '15 at 23:31
  • See example in answer. – Shawn Conn Oct 15 '15 at 20:12
1

It sounds like you should handle this in your computed field's PHP since you're already parsing out the image there.

//Assuming $entity is the parsed node in question 
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $entity->body[LANGUAGE_NONE][0]['value'], $matches);

//If an image match has been found, build a URI relative to public file base
//Assume image URLs are something like:
// "/sites/default/my_files/some_dir/whatever.jpg"
if(isset($matches[1][0])) {
  $public_dir = variable_get('file_public_path','sites/default/files');
  $uri = file_build_uri(str_replace("/$public_dir/","",urldecode($matches[1][0])));
  $entity_field[0]['value'] = image_style_url('thumbnail',$uri);
}
0

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.