Florian Lorétan is a web developer, a musician and an open-source advocate.
I was recently faced with the same problem in two different projects: forcing the creation of an image derivative with imagecache so that it is available in the submit handler of the same form in which the image was uploaded. By default, imagecache generates images on demand triggered by an HTTP request, which is ideal for most cases. However, this design doesn't make it possible to trigger the generation of images programmatically, which is why I created the following code snippet.
<?php
function imagecache_generate($presetname, $filepath) {
if (!$preset = imagecache_preset_by_name($presetname)) {
return;
}
$dst = imagecache_create_path($presetname, $filepath);
if (!file_exists($dst)) {
imagecache_build_derivative($preset['actions'], $filepath, $dst);
}
return $dst;
}
?>This snippet is based on _imagecache_cache(), but instead of transferring an image in response to an HTTP request, it returns the path to the generated image.
Note that you do not need to do this unless you are doing some special processing with your image (in my case: including it in a generated PDF document or passing it as a parameter to a command-line executable which generates "magic eye" images).
Tags: drupal planet, images, imagecache, drupal, code snippet
Comments
hello! i'm interested in this
hello!
i'm interested in this manipulation.
Could you give more info on where you put this script and how to trigger it?
thanks!
Custom module
You need to put this function in a custom module, and call it from your own code before doing any actions that depend on the resized image being present. Of course, imagecache needs to be enabled, so you probably want to add it as a dependency to your custom module.
thanks
thanks for your help.
What i'm trying to do is to create a rule (rules module) to auto generate imagecache derivatives upon content update.
My rule is called, the "imagecache_build_derivative" func is called too, but for some reason i don't see the image being created on my server.
Guess it still needs some debugging...
Thanks a lot for sharing this
Thanks a lot for sharing this very useful tips :-)
Awesome!
Great tip ... exactly what I was looking for!
Post new comment