I would like to add an "Image Style" that resizes an image by percentage instead of a fixed pixel. In Drupal 6, Image Cache allowed you to do this (http://drupal.org/node/163561).

I know Image Cache is now in the core... so would I have to alter the core? That's something I really want to avoid. Write a module? I have PHP experience but have never written a module and don't really know where to start. It seems like this wouldn't be too big of a fix but I'm not really sure where to go...

Comments

WorldFallz’s picture

Since it didn't make it to the core d7 version, seems like the http://drupal.org/project/imagecache_actions module would be a good place for it. Examining the code should provide you with what you need to know to modify an existing action to do what you want.

kostal’s picture

Thanks, I did see that but wasn't sure about it. I'll do some experimenting and report back.

kostal’s picture

I've tried using the Image Action module and it crashes the system when I try to enable the "write your own php" or when I actually try to apply one of the effects to an image style. Maybe it's best I just use Drupal 6, since I know it can do everything I need. But dang I was really digging 7.

ferrangil’s picture

I am also wondering why the % resize option is not available on D7 core neither in imagecache_actions.
Really needing this...

Max_Headroom’s picture

Any update on this?
Really needed.

Quentin Campbell

julien.reulos’s picture

Imagecache Actions module should be the place to go, but I had no luck with the custom action configuration... I can't figure out if the problem is coming from the module for D7 that doesn't support %, core ImageCache module, the GD2 library, or something else.

Anyway I switched my image toolkit from GD2 to ImageMagick, and install the ImageMagick Raw Effect module, that adds a new effect option to the Image Styles page so you can create your own raw imagemagick command.

In my case, the raw imagemagik command I use is :
-thumbnail 10%

Documentation about resize commands:
http://www.imagemagick.org/script/command-line-options.php?#resize
http://www.imagemagick.org/script/command-line-options.php?#thumbnail
http://www.imagemagick.org/script/command-line-processing.php?ImageMagic....

One problem you could have switching from GD2 to ImageMagick is that the Imagecache Actions effects are not well supported (at this time), as explained in the main project page module.

ujin’s picture

Hello all
you can just set up imagecache_action module and in the image style select manual action, and add code like this

image_gd_resize($image,$image->info['width']*0.5,$image->info['height']*0.8);
return $image;

and you will receive something like percentage image style.

Hope that would help to anybody.

julien.reulos’s picture

Thanks you ! Works like a charm.

Anonymous’s picture

Hi, i get this error, but i dont understand this.
Other all styles, round, etc... it´s ok.
Any suggestion?

Very thk!

Call to undefined function imagecache_actions_get_image_context() in C:\xampp\htdocs\sellosalepuz5\sites\all\modules\imagecache_actions\customactions\imagecache_customactions.module on line 125

Anonymous’s picture

Well, sorry i find the docs for custom actions.

After i post the results.

thk

dubs’s picture

Hi lgrtm,

I had this problem too. To make this work I added a couple of lines to imagecache_customactions: -

<?php
  // Get context about the image.
+  // Load utility.inc from the imagecache_actions module.
+  module_load_include('inc', 'imagecache_actions', 'utility');
  $GLOBALS['image_context'] = imagecache_actions_get_image_context($image, $data);
?>

Good luck with your project...

David

Memoskins’s picture

Hi,

Looking to solve the same problem: percentege image style.
I just follow all the instructions on the Readme File:
1) Installed ImageCache actions module
2) run update.php
3) create a new image style
4) add a custom action effect

But when I want to insert the code you posted inside the custom action effect option, the window to insert the PHP code is gray with a "return TRUE;" code inside. I can't modify it to insert your code!

P.D. Are the "soft dependencies" needed to run the code?

any clues?

Best regards

1140’s picture

You have to enable the 'PHP filter' module. After that you can paste it there.

ujin’s picture

Hi, Using new version you can, for example, to make static height and flexible width almost same code but return true like:

$width = $image->info['width']/$image->info['height']*127;
image_gd_resize($image,$width,127);
return true;
LTech’s picture

Any idea how to make the image a percentage of the screen width so it looks good with responsive design and will change automatically without having to set any breakpoints?

mastoll’s picture

@LTech, this is exactly what I came looking for. Have you found a resolution?
We could of course do this in the css, but there may be a better way wrt responsivness.

tcalin’s picture

sobi3ch’s picture

Just want to mention if you want to create 100%x100% (I've need it on image preview when adding/editing node) then just create EMPTY image style (without any actions!) with name something like 'Original (100%x100%)'. It will left image in original state.