Imagecache: dynamic image manipulation
Imagecache is a module that lets you make different sized alternatives of the same images. It requires an image manipulation library such as GD2 or ImageMagick and requires clean urls to be enabled. You can use imagecache with any image uploaded to Drupal, so you can use it with Image module as well as normally uploaded images using the Upload module, but the most common way is to use it with CCK and Imagefield.
View a screencast on using Imagefield and Imagecache.
One useful example is in a community site with many users. Users could upload a user picture that is for example, a 240 pixel square. This may work great for user profile pages, but say you want to make some views of users in specific groups - 240 x 240 pixels may be too big for a view that lists of dozens of people. On these views you may want to make the pictures smaller - 120 x 120 pixels for example. Image cache gives you a way to accomplish this without having to manually resize each image.
Another example that we're going to demonstrate in detail is when you want to scale images down for a teaser and display them in their full size when viewing the whole content. Before proceeding, make sure you have installed and enabled Imagecache, CCK and Imagefield. Also create the content type with the Imagefield you'll want to resize. If you just want to edit a template manually to resize an image, you should scroll down to the end.
First login to Administer -> Site Building -> Image cache (this used to be Administer -> Site Configuration -> Image cache in older versions of the module). Then label a new namespace preset: for example "small" (no quotes) and click on the 'Create preset' button.

Then choose an action: scale, resize, or crop. Scale works well when you want to keep the aspect ratio, you only need to enter one of the dimensions in this case. Resize will allow you to produce images of any arbitrary size, even if that results in a distorted image. Crop will allow you to display only part of the image, I'll use the Drupal logo as an example:
Scaled to 50%:

Resized to 60x20:

Cropped to 50%x50%, both offsets set to top:

For this example, scale will suffice, so select it and click on the 'Update preset' button:

After that, give the preset a width and height, you can use a percentage to get sizes relative to the original image's size or enter an absolute value in pixels. In the first example example we would use 50% or 120 to make a 120 pixel square image. As the second example is more generic and you might be dealing with images of several sizes, the best choice would be to specify a maximum value for both width and weight the image displayed in the teaser.
Let's suppose you want to scale the maximum size of the image to be 48x48. To do that, you just need to fill out the width and weight and set it to scale to fit inside dimensions, unless you don't care if one of the dimensions may actually end up greater than the specified value. Don't forget to click on the 'Update preset' button after setting the needed values.

Now that we've successfully created our preset namespace, we need to edit the desired CCK field to automatically resize the images. To accomplish that, first go to Administer -> Content Management -> Content Types. After that, click on the configure link for the content type that contains the content type that has the field you want to configure.

Then just select display fields and set Teaser to the desired preset and click on 'Submit' (in this case "small", you could also edit full if you wanted the images to always be resized, even when browsing the full content):

Voilà , now all images in a "testimage" field will be automatically scaled down to a maximum of 48x48. Below is the final result.
Preview:

Full view:

If you want to add manually an image and apply an imagecache preset on it, you need to add a code snippet to the desired .tpl.php file:
<?php
print theme('imagecache', $preset, $image['filepath'], $alt, $title, $attributes);
?>Below is an example in which I manually call Imagecache using the namespace small and printing file test.jpg on the root of the Drupal installation directory (the image path should always relative to it). The alt atribute is shown when the image cannot be displayed while the title attribute is meant to be displayed in a tooltip.
<?php
print theme('imagecache', 'small', 'test.jpg', 'just a test image', 'test image');
?>($alt, $title, $attributes are optional, $attributes intentionally omitted in the example, as it's not a very frequently used parameter).
Putting several of these practices together can lead to code such as this:
<?php if ($field_images[0]['view'] > '' ) : ?>
<?php foreach($field_images as $item) { ?>
<div class="images">
<?php print '<img src="/files/imagecache/medium/'.$item['filepath'].'" title="'.$item['title'].'" alt="'.$item['alt'].'">'; ?>
<?php print '<h3 class="img_title">'.$item['title'].'</h3>'; ?>
</div>
<?php } ?>
<?php endif; ?>
create presets programmatically
It's the new thing! Create stuff programmatically. In Drupal 6 this worked for me:
<?php
// Preset
$imagecachepreset = new stdClass ();
$imagecachepreset->presetname = 'MYPRESETNAME';
drupal_write_record('imagecache_preset', $imagecachepreset);
// Action
$imagecacheaction = new stdClass ();
$imagecacheaction->presetid = $imagecachepreset->presetid;
$imagecacheaction->module = 'imagecache';
$imagecacheaction->action = 'imagecache_scale_and_crop';
$imagecacheaction->data = array(
'width' => '200',
'height' => '200'
);
drupal_write_record('imagecache_action', $imagecacheaction);
?>
I just wanted to share this with everyone
This work "partially", with
This work "partially", with this code if admin go to Imagecage UI don't find preset (but if it add a preset this appair). This is a problem for me, but i correct it with:
<?php// Preset
$imagecachepreset = imagecache_preset_save(array('presetname' => 'MYPRESETNAME'));
// Action
$imagecacheaction = new stdClass ();
$imagecacheaction->presetid = $imagecachepreset['presetid'];
$imagecacheaction->module = 'imagecache';
$imagecacheaction->action = 'imagecache_scale_and_crop';
$imagecacheaction->data = array('width' => '200', 'height' => '200' );
drupal_write_record('imagecache_action', $imagecacheaction);
?>
Bye
Mavimo
____________
Drupal Italia official community webstite
My personal website (Drupal, CFD, OpenFOAM and so - Italian only)
Guide to write code for imagecache preset
Only italian language, but i think is also readable for dev (ther's a lot of code :P)
Bye
Mavimo
____________
Drupal Italia official community webstite
My personal website (Drupal, CFD, OpenFOAM and so - Italian only)
Uninstall Code
I used mavimo's code above in a module's _install hook, and it worked great. On the flip side, for the _uninstall hook I used:
<?php// Called in the _uninstall hook, to delete the imagecache preset(s) created in the _install hook:
imagecache_preset_delete(imagecache_preset_by_name('MYPRESETNAME'));
?>
(This applies to the 6.x version)
Just confirming the above
Just confirming the above works perfectly. Thank you!!
Should definitely be added to the page.
Thanks for this, i created a
Thanks, i created a new subpage for this at
http://drupal.org/node/558664
Creating a preset programmatically for 5.x-2.3
Here's an example from a module I'm building
$scale_and_crop = array('131x131','83x83','60x45');
foreach ( $scale_and_crop as $dimensions ) {
list($width,$height) = split('x',$dimensions);
$preset_id = db_next_id('{imagecache_preset}_presetid');
db_query('INSERT INTO {imagecache_preset} (presetid, presetname) VALUES (%d, \'%s\')', $preset_id, $dimensions);
$action_id = db_next_id('{imagecache_action}_actionid');
$data = array(
'width' => $width,
'height' => $height,
);
db_query("INSERT INTO {imagecache_action} (actionid, presetid, weight, action, data) VALUES (%d, %d, %d,'%s', '%s')", $action_id, $preset_id, 0,
'imagecache_scale_and_crop', serialize($data));
}
cache_clear_all('imagecache:presets', 'cache');
What would be awesome is if the preset table contained a "module" field, similar to that found in node types and taxonomy vocabularies. You could then select by module to remove them in hook_uninstall.
Correction
Image Cache namespaces are set at "Administer -> Site Building -> Image cache" (or admin/build/imagecache) not "Administer -> Site Configuration -> Image cache" as indicated about.
------
Con paciencia y calma,
sube un burro a una palma
Exactly
I've spended 20 min debugging output of "Administer -> Site Configuration".
Please, make corrections of the manual.
Corrected.
Thanks Feo and Carlos; I verified this and made the correction above.
--
Dave Chakrabarti
Co-founder | Project Manager
Nonprofitable.org - Drupal for nonprofits
(708) 919-1026
$attributes
The $attributes should be an array of additional attributes to the image. For example:
<?phpprint theme('imagecache', 'small', 'test.jpg', 'just a test image', 'test image', array('id'=>'small_image_id','class'=>'image','width'=>'100','height'=>'100'));
?>
Can this Work?
Hi,
can this work with one url link? e.g. mysite http://primeirapagina.info have thumbshots, how can i use imagecash to to resize?
Thank you