0

I want to get the url to a specific image from image cache. I have the image file name (my_image.jpg), file path (site/default/files/my_image.jpg). From this value I want to get the url to a certain image cache folder (sites/default/files/imagecache/240x135/my_image.jpg). I cannot use any drupal methods/modules to get this because I am using external scripts to directly access the back-end database, from where I can get the image name, actual image path, but couldn't find the table where the url to image cache is stored.

How can I do this?

1

You can always load Drupal API in your custom php All you have to do is copy the code from your index.php to your custom one or write

/** bootstrap drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
| improve this answer | |
  • That's missing at least one line... – Clive Jan 31 '13 at 14:43
  • @Clive What do you mean ? all we need is the first two lines of this file [api.drupal.org/api/drupal/index.php/6] – Pan Chrono Jan 31 '13 at 14:47
  • That link is a 404...you're missing define('DRUPAL_ROOT', getcwd());, in my experience that's required – Clive Jan 31 '13 at 14:51
  • Only just noticed the '6' tag, maybe you don't need that line for D6. Definitely do for D7 though :) – Clive Jan 31 '13 at 14:51
  • Thank you very much, it's really helpful. I am accepting this one as accepted as this inclusion of drupal will allow me to do further manipulation in my scripts! – MD Sayem Ahmed Feb 1 '13 at 10:29
3

You can use the imagecache_create_url() function:

$path = imagecache_create_url($presetname, $filepath);
  • $presetname: String specifying an ImageCache preset name.
  • $filepath: String specifying the path to the image file.
| improve this answer | |
  • +1, yeah, it will work no doubt. The problem is, I want to get the url in a stand-alone php script. It has no connection whatsoever with the drupal modules. It's accessing the back-end database directly. – MD Sayem Ahmed Jan 31 '13 at 13:13
  • From the documentation that you've sent me, I see that they basically append the preset name with the filename. If this is the case, then probably I can build it myself. But I don't know what to do if the image is not in image cache. – MD Sayem Ahmed Jan 31 '13 at 13:15
  • Yes it should be pretty easy to reproduce that function outside of Drupal it's not particularly complicated – Clive Jan 31 '13 at 13:16
  • 1
    It allows other modules to alter the URL before it's finalised. Whether or not you need to re-implement that depends on what you've got installed really – Clive Jan 31 '13 at 14:43
  • 1
    @SayemAhmed No worries, the idea is you tick the answer that helped you the most. If there are other answers that also helped you can upvote them, or not, totally up to you :) – Clive Feb 1 '13 at 10:31
1

The way imagecache and drupal work is based upon the behavior that drupal is called into play only when a file is requested that doesn't actually exist. For example, if a user requests

/sites/default/files/imagecache/WIDTHxHEIGHT/foo.jpg

and it doesn't exist, drupal is booted up and a menu handler is called within imagecache which grabs the original foo.jpg and applies the WIDTHxHEIGHT preset and then stores the file back in place. Then the next time someone requests it, it does exist and the webserver happily sends it to the requesting user without getting drupal involved.

So... to your question, if you are using public files, eg:

/sites/default/files/foo.jpg

and have an imagecache preset of WIDTHxHEIGHT, the url of the file will be:

/sites/default/files/imagecache/WIDTHxHEIGHT/foo.jpg

eg, it is stuffing imagecache/PRESET_NAME after the basepath of your public file directory and before the file in question. If you had an original file:

/sites/default/files/avatars/USERNAME/bar.jpg

it would turn into

/sites/default/files/imagecache/WIDTHxHEIGHT/avatars/USERNAME/bar.jpg

Hopefully you see the pattern emerging. And thus nothing is needed to be kept in the database to do all of this.

Imagecache DOES however use permissions to allow/disallow users to use each specific preset, so, you can use them to stop users from requesting files that will never be used on your site by them manually surfing around to presets.

Hope this sheds some light on it all.

| improve this answer | |
  • +1, thank you very much for your answer, it's really helpful! Only if I could accept more than one answer........ – MD Sayem Ahmed Feb 1 '13 at 10:27

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.