Using Imagecache on external images

Filed under:

Recently we rebuilt a site for a client that was using Flickr to host a whole slew of product images for his website. Now I know what you’re thinking. Hosting images for you website somewhere other than your Drupal install seems a little bit silly, your website now depends on your server being up and it also depends on Flickr’s servers being up. That being said, Flickr does provide quite an extensive API for accessing your images and it’s made even easier with Dan Coulter’s phpFlickr class.

Aside from this though, we wanted to be able push all of the product images through Imagecache in order that we could get consistently sized images for our page layouts.

For those of you that don’t know, Imagecache is a wonderful Drupal module that allows you to define image processing presets (ie scale, crop, colour shift plus lots more). The module then takes your image, processes it according to your preset, and then stores this new copy on the server ready to placed on the page when it is called. This allows you to do some neat things with images without having to upload multiple versions of the same image. You can assign an image an Imagecache preset in various places in the config or you can call it in your code like this:

  1. print theme('imagecache', 'preset_namespace', $image_filepath, $alt, $title, $attributes);

However, herein lies the problem for us trying to use this on images that we have pulled in from Flickr. Imagecache does not allow you to run a preset on an external image, the image has to be inside your Drupal install. So this is where the fun starts.

We decided to write a function that would go out to Flickr and pull in our images and store them in a folder on our server. And it looks a little something like this:

(Read the inline comments for a basic explaination of what the code does)

  1. function marmalade_helper_download_from_flickr($flickr_image_id) {
  2.   $flickr = new phpFlickr('6a332a7d86cea24360a71dde09c22c71');
  3.   $sizes = $flickr->photos_getSizes($flickr_image_id);
  4.   $largest_available_size = array_pop($sizes);
  5.   //Check if the containing folder exists -- if not, create it.
  6.   $dir = file_create_path() .'/flickr-originals';
  7.         if (!file_check_directory($dir)){
  8.                 mkdir($dir, 0775, true);
  9.         }
  10.   // Get the image from Flickr
  11.   $url = $largest_available_size['source'];
  12.   //set up the local file name
  13.   $ext = '.' . 'jpg'; // This should get everything after the last full-stop in the above $url, but hardcoding for now
  14.   $src = file_create_path() . '/flickr-originals/' . $flickr_image_id . $ext;
  15.         $result = drupal_http_request($url);
  16.         $code = floor($result->code / 100) * 100;
  17.         $types = array('image/jpeg', 'image/png', 'image/gif');
  18.         if ($result->data && $code != 400 && $code != 500 && in_array($result->Content-Type, $types)) {
  19.                 $src = file_save_data($result->data, $src);
  20.                 return TRUE;
  21.   }
  22.         else {
  23.                 //if we are unsuccessful then log a message in the watchdog
  24.                 watchdog('flickr', 'The image '. $url . ' could not be retrieved');
  25.                 return FALSE;
  26.         }
  27. }

We then wrote a little wrapper function for the Imagecache call which basically checks if the image exists locally, if it doesn’t then it calls the marmalade_helper_download_from_flickr() to go and get it from Flickr. Once the image is down from Flickr, or if it was already there, it runs the Imagecache theme call.

  1. function marmalade_helper_flickr_imagecache($flickr_image_id, $imagecache_preset) {
  2.   $local_image_url = file_directory_path() . '/flickr-originals/' . $flickr_image_id . '.jpg';
  3.   if (!file_exists($local_image_url)) {
  4.     impact_helper_download_from_flickr($flickr_image_id);
  5.   }
  6.   if (file_exists($local_image_url)) {
  7.     return theme('imagecache',$imagecache_preset, $local_image_url);
  8.   }
  9.   else {
  10.     return '<span>Image could not be found</span>';
  11.   }
  12. }

We were then able to print an image on the page by calling this function. All we have to do is provide it with a Flickr image id and the Imagecache preset we want to use.

  1. $image = impact_helper_flickr_imagecache($photo_id, 'product_page_large');
  2. print $image;

I hope this helps someone :) Leave a comment and let us know what you think!!

Comments

Add new comment

Rapidshare downloads

Highly riveting blog. Highly

Highly riveting blog. Highly riveting and professionally penned post. I will come back very soon.
total gym 1700

hide

W e _ a r e _ v e r y _ s o r r y _ f o r _ a n y _ i n c o n v i n i e n c e _ c a u s e d _ t o _ y o u _ b y _ o u r _ m e s s a g e . W e _ w e r e _ s e e k i n g _ f o r _ o l d _ " n o b o d y ' s " _ w e b s i t e s . _ I f _ i t ' s _ a _ m i s t a k e _ a n d _ w e _ d i s t u r b e d _ y o u , _ p l e a s e _ d e l e t e _ o u r _ m e s s a g e , _ a n d _ w e ' l l _ n e v e r _ r e t u r n _ h e r e . O n e _ m o r e _ t i m e , _ s o r r y _ f o r _ a n y _ t r o u b l e s _ t h a t _ w e ' v e _ c a u s e d _ b y _ o u r _ a c t i v i t y . cheap cialis

Teaching Assistant diploma |

No matter what it takes you

No matter what it takes you to get on top of the world ... just do it damn it.

associate degree management | master degree Information Technology | bachelors degree international business

I will recommend my friends

I will recommend my friends to read this.I will bookmark your blog and have my children check up here often.I am quite sure they will learn lots of new stuff here than anybody else!....There is also a lot of visual symbolism that is sometimes subtle, sometimes not so subtle..
meilleurs bonus du casino

Thanks for this very useful

Thanks for this very useful info you have provided us. I will bookmark this for future reference and refer it to my friends. More power to your blog. Mesothelioma victims

Mesothelioma victims

Amazing posts you got in your blog. Will visit again.

Mesothelioma victims

This is really a well laid

This is really a well laid out website. I like how you have presented your information with excellent detail. Keep up the great work here. concussion symptoms

Nice post. I felt your post

Nice post. I felt your post is truly entertaining. I will come back soon.
dremel multi max

Took me time to read all the

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained!
Mike teen drug treatment center

Nice post at all.It peaks my

Nice post at all.It peaks my curiosity and i really enjoy reading it.

small business loan

I need your help. I am not

I need your help. I am not using the thematic theme and am using some other theme. I want to have a page which will list all categories along with their icons. How can I achieve that?
http://www.giochidelbingo.it/

You really make it seem so

You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it! As a rule I download everything I need (all the books, articles and tutorials) by http://www.picktorrent.com but I'm really glad to find your site. keep up the good work!

So far, I managed to go

So far, I managed to go though only some of posts you discuss here, but I find them very interesting and informative. Just want say thank you for the information you have shared. Regards James from php flash chat

Re Writable

These lines of codes are so easy to re write, as easy as getting a debt settlement program.

You blog posts very much

You blog posts very much online canadian pharmacy

Common opinion

I like your blog very much! Could you please in one of your articles make a review of torrents search engines? As for me, I use picktorrent - search engine on torrent files . They seem to be popular now, don't they?

Typo??

Line 4 of function marmalade_helper_flickr_imagecache

-> impact_helper_download_from_flickr($flickr_image_id);

I think should read:

-> marmalade_helper_download_from_flickr($flickr_image_id);

Not a big deal but it might throw some of your readers off.

Thanx for grat site

I really like your post. Does it copyright protected?

Hi, good post. I have been

Hi, good post. I have been wondering about this issue,so thanks for posting. I’ll definitely be coming back to your site.

This is really a well laid

This is really a well laid out website. I like how you have presented your information with excellent detail. Keep up the great work here.

Good stuff and deja vu!

This reminded me of a very similar situation last year where I tried to get the "local copy" functionality for Image Cache's sake into the Embedded Media Field module, which also utilises Flickr (amongst other things) as a possible source.

Hard to beat Flickr as an image manager, eh? Nice solution!

Useful!

Hi guys! This is very useful, as I was looking into such functionality. I'm going to go through the code carefully, and was thinking of integrating it in the node creation/posting process, maybe with a CCK field, which takes a Flickr photo URL and then applies your magic on it!

Soroush < http://sorou.sh >

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is just to stop automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Interested?

We're committed to forming long-term relationships with our clients, so you can be sure that we will be around in the future to support your site and grow with your needs.

get a free quote