3

Is there a way to request a scaled image via imagecache by adding width/height parameters or a preset-reference to the image url within css? For example:

Obviously, to scale down an image in css, you can do the following:

HTML:

<figure id="logo"></figure>

CSS:

#logo {  
  background: url('path/to/image.jpg') no-repeat;  
  width: 50%;  
  height: 50%  
}

This will still load the full sized image and scale it down on the client side. I am wondering if something like the following is possible within CSS, so you dynamically request a scaled image via imagecache:

#logo {  
  background: url('path/to/image.jpg?width=50px&height=50px') no-repeat;  
}

or

#logo { 
  background: url('path/to/image.jpg?preset=existingimgpreset') no-repeat;  
}

Which will scale down the image on the server side and hopefully will return a cached version of it when possible.

  • This is really more suited for Stack Overflow. – Lester Peabody Oct 2 '12 at 22:03
  • If so: apologies, but why is that exactly? imagecache is a core-module of drupal and this question is directly related to it – grrrbytes Oct 2 '12 at 22:04
  • Yeah I've re-read it twice now, I retract my previous statement :) Cool thought, wondering what people will say. – Lester Peabody Oct 2 '12 at 22:07
3

The URLs imagecache produces are pretty reliable, and contain the name of the preset. Looking at the code in imagecache_create_url() you could build up a path in your CSS that looks something like this:

/sites/default/files/imagecache/preset-name/path/to/image.jpg

Assuming that your files are stored at /sites/default/files

  • This works perfectly and is actually pretty obvious when inspecting image url's in Drupal. Thanks for pointing that out! – grrrbytes Oct 2 '12 at 22:15
  • another question: it seems this only works for files in the /sites/default/files/* directory. I tried doing the same for some images in my theme-dir: sites/all/themes/mytheme/gfx/imagecache/preset/image.jpg (and different versions of this url), with no luck. Guess it doesn't work outside of sites/*/files? – grrrbytes Oct 2 '12 at 22:48
  • 1
    Yeah it uses file_directory_path() which returns the files folder...not sure there's any way to change that without editing the module directly – Clive Oct 2 '12 at 22:50

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.