If I'm going to be using imagecache for half of my images, I'd really prefer to use it for all of them, including images uploaded by the image module. Even though image.module does have its own derivatives, my theme templates are getting overly complicated and I prefer imagecache's derivatives anyway.

It was my understanding that all imagecache needed was a path to the original image file. So, for example, in my node-image.tpl.php shouldn't I be able to do something like this? :

<?php theme('imagecache', 'large', $node->images['_original'] ?>

Of course this doesn't work.

Comments

zach harkey’s picture

Believe I may have answered my own question. It appears you just can't use the the imagecache theme functions.

This seems to work:

<img src="<?php file_create_url($node->images['_original']) ?>" alt="" />

(I say 'seems to' because if past experience tells me anything it is that later today, i'll find a way to bork this.)

zach harkey’s picture

Errr. disregard last post, which completely left out the "imagecache/preset" part of the path and therefore wasn't dooing anything at all. The problem remains.

dopry’s picture

Can you give me the input and output of theme('imagecache',...)? It would really help with debugging your problem.

zach harkey’s picture

Wait, I got it to work by using the theme_imagecache_display() function.

<?php print theme('imagecache_display', $node, 'thumbnail', file_create_url($node->images['_original']), array('class' => 'thumbnail')) ?>

Is this the best way to achieve this? I'm still confused by the similiarities between theme_imagecache() and theme_imagecache_display().

Is there any problem with using imagecache to display all of my image.module images as well? (i.e. is it ok to replace all instances of image_display() with imagecache_display() in my node-image.tpl.php?)

dopry’s picture

theme_imagecache_display was stolen directly from image.module while I was working on the theme_imagecache function. It expects a complete url to an image. You should probably not use it. It will disappear, and is identical to theme_image.

theme_imagecache is the function you should be using. Please provide the input and output to theme_imagecache for your particular situation. Otherwise I cannot help you further.

zach harkey’s picture

Ok, here are some of the things I've tried. In each case, the first line is the code in my node-image.tpl.php, and the second line is the parsed output. Thanks very much for your help.

<?php print theme('imagecache', 'thumbnail', $node->images['_original']) ?>
<img src="http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/imagecache/thumbnail/images/showcase_0005.jpg" class="thumbnail" />
<?php print theme('imagecache', 'thumbnail', file_create_url($node->images['_original'])) ?>
<img src="http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/imagecache/thumbnail/http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/images/showcase_0005.jpg" />
<?php print theme('imagecache', 'thumbnail', file_create_path($node->images['_original'])) ?>
<img src="http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/imagecache/thumbnail/sites/staging.jadaloveless.com/files/images/showcase_0005.jpg" />
<?php print theme('imagecache', 'thumbnail', file_directory_path() .'/'. ($node->images['_original'])) ?>
<img src="http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/imagecache/thumbnail/sites/staging.jadaloveless.com/files/images/showcase_0005.jpg" />
<img src="<?php print file_create_path('/imagecache/thumbnail/'. $node->images['_original']) ?>" alt="" />
<img src="sites/staging.jadaloveless.com/files//imagecache/thumbnail/images/showcase_0005.jpg" alt="" />
<img src="<?php print file_directory_path() .'/imagecache/thumbnail/'. file_create_path($node->images['_original']); ?>" alt="" />
<img src="sites/staging.jadaloveless.com/files/imagecache/thumbnail/sites/staging.jadaloveless.com/files/images/showcase_0005.jpg" alt="" />
<img src="<?php print file_directory_path() .'/imagecache/thumbnail/'. file_create_url($node->images['_original']); ?>" alt="" />
<img src="sites/staging.jadaloveless.com/files/imagecache/thumbnail/http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/images/showcase_0005.jpg" alt="" />
<img src="/<?php print file_create_path('/imagecache/thumbnail/'. $node->images['_original']) ?>" alt="" />
<img src="/sites/staging.jadaloveless.com/files//imagecache/thumbnail/images/showcase_0005.jpg" alt="" />
<img src="/<?php print file_directory_path() .'/imagecache/thumbnail/'. file_create_path($node->images['_original']); ?>" alt="" />
<img src="/sites/staging.jadaloveless.com/files/imagecache/thumbnail/sites/staging.jadaloveless.com/files/images/showcase_0005.jpg" alt="" />
<img src="/<?php print file_directory_path() .'/imagecache/thumbnail/'. file_create_url($node->images['_original']); ?>" alt="" />
<img src="/sites/staging.jadaloveless.com/files/imagecache/thumbnail/http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/images/showcase_0005.jpg" alt="" />
<img src="<?php print file_create_url(file_directory_path() .'/imagecache/thumbnail/'. file_create_path($node->images['_original'])) ?>" alt="" />
<img src="http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/imagecache/thumbnail/sites/staging.jadaloveless.com/files/images/showcase_0005.jpg" alt="" />
<img src="<?php print file_create_url(file_directory_path() .'/imagecache/thumbnail/'. file_create_url($node->images['_original'])) ?>" alt="" />
<img src="http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/imagecache/thumbnail/http://staging.jadaloveless.com/sites/staging.jadaloveless.com/files/images/showcase_0005.jpg" alt="" />
dopry’s picture

The output of the first one would have been plenty...

Which version of drupal are you using and what is the CVS version of imagecache on the admin/imagecache page?

Is there a .htaccess file in sites, sites/staging.jadaloveless.com, or sites/staging.jadaloveless.com/files/ ?

zach harkey’s picture

The output of the first one would have been plenty...

Sorry.

Which version of drupal are you using and what is the CVS version of imagecache on the admin/imagecache page?

Drupal 4.7.4
imagecache.module,v 1.9.2.11

Is there a .htaccess file in sites, sites/staging.jadaloveless.com, or sites/staging.jadaloveless.com/files/ ?

No, no, yes

The one in sites/staging.jadaloveless.com/files/ reads as follows:

SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
Options  +FollowSymLinks 
<IfModule mod_rewrite.c>
  ## RewriteEngine off
</IfModule>
zach harkey’s picture

Ok, I've been experimenting off and on with different variations on this theme for the last week. Every once in while I am able to get imagecache to work in my image.tpl.php, but before I can actually figure out what I did to get it working, it stops working again.

For a while I was getting it to work as long as I uploaded images one at a time with the drupal form (node/add/image), but images uploaded with the image_pub.module weren't working. A short while later even these images weren't working.

For the record, I have the following line in image.tpl.php:

<?php print theme('imagecache', 'icon', $node->images['_original']) ?>

which outputs the following html when parsed (e.g. node/65 )

<img src="http://example.com/sites/example.com/files/imagecache/icon/images/img-1454.jpg" alt="" />

Unfortunately this produces a broken image icon since there is no image at this url.

However, if I remove the 'imagecache/icon/" portion of the url, the original image is perfectly accessible at its actual url (e.g. http://example.com/sites/example.com/files/images/img-1454.jpg).

Incidentally, it also produces the following errors in my apache log:

[client 71.199.145.72] PHP Warning:  getimagesize(sites/example.com/files/imagecache/preview/images/img-1454.jpg): failed to open stream: No such file or directory in /file/system/path/to/drupal47/modules/imagecache/imagecache.module on line 139, referer: http://example.com/node/65
[client 71.199.145.72] PHP Warning:  filesize(): Stat failed for sites/example.com/files/imagecache/preview/images/img-1454.jpg (errno=2 - No such file or directory) in /file/system/path/to/drupal47/modules/imagecache/imagecache.module on line 142, referer: http://example.com/node/65
[client 71.199.145.72] PHP Warning:  fopen(sites/example.com/files/imagecache/preview/images/img-1454.jpg): failed to open stream: No such file or directory in /file/system/path/to/drupal47/includes/file.inc on line 561, referer: http://example.com/node/65

Is anyone else able to put a line like this in their image.tpl.php file and get it to work?

<?php print theme('imagecache', 'example_preset_name', $node->images['_original'] ?>

zach harkey’s picture

Interesting development.

If I base my imagecache derivative off of the _original image, I get the errors mentioned in my last post, but if I use the default 'preview' derivative, created by image.module, everything works!?

In node-image.tpl.php this does not work:
<?php print theme('imagecache', 'icon', $node->images['_original']); ?>

but this does:

<?php print theme('imagecache', 'icon', $node->images['preview']); ?>

dopry’s picture

sounds like something else wrong might be happening... looking at the paths and URL's...

a request for http://example.com/sites/example.com/files/imagecache/icon/images/img-14....
will be looking for 'images/img-1454.jpg'.
that shouldn't result in imagecache looking for 'preview/images/img-1454.jpg'.

you should try reloading urls a couple of times and emptying your browser cache when reloading pages. You should also delete the imagecache tree between tests while initially configuring imagecache and check to see if your derivative image is generated each time you call the url.

zach harkey’s picture

Status: Active » Closed (works as designed)

I finally sorted this all out. Imagecache seems to work perfectly with images created by the image module.

In my particular case the problem was related to my using the php getimagesize() function in order to get the width x height of the image because I wanted it to display inside a containing <li> element. Since the <li> is a block level element, and I wanted it to float, I had to specify a width to avoid browser bugs.

Unfortunately, if the derivative had not yet been generated, as was often the case, then getimagesize() would fail and throw the above errors. Of course, this only becomes obvious when you do as dopry suggested and delete the entire imagecache directory between every test . This cannot be overstated.

The imagecache derivative is only created through the URL call, file_create_path() won't do it, you have to use file_create_url().

Luckily getimagesize() will accept a url instead of a file path, so for example, if I'm themeing a view that returns an array of image nodes: $nodes, I can do something like the following:

  $output = '<ul>';
  foreach ($nodes as $node) {
    $node = node_load(array('nid' => $node->nid));
    $imagecache_path =  file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $node->images['_original']);
    // Convert all spaces to '%20' or getimagesize() will choke.
    $imagecache_path = str_replace(' ', '%20', $imagecache_path);
    // Get image dimensions
    $imagesize = getimagesize($imagecache_path);
    $width = $imagesize[0];
    $height = $imagesize[1];
    $output .= '<li style="width: '. $width .'px; height: '. $height .'px;">'. theme('imagecache', $namespace, $imagecache_path) .'</li>';
  }
  $output .= '</ul>';

Because my list items have specific width/height dimensions I can now use CSS to display the all the images inline.