I would like to add a thumbnail format that scales any image to fit a specified width.
So, instead of typing "120x120" I would type "120x0".

And this is how it would resize images:
- 240x100 would become 120x50
- 60x60 would become 120x120
- 90x30 would become 120x40

you get the idea.
The same does long exist in imagecache, but not (yet) in imce.


donquixote’s picture

Title: Width-only scale » Width-only scale (and preserve aspect ratio)
donquixote’s picture

Status: Needs review » Active

And here we go, the solution. No time to produce a patch atm.
In inc/imce.page.inc, function imce_resize_image()

function imce_resize_image($filename, &$imce, $width, $height, $copy = TRUE, $dest = FALSE, $op = 'resize') {
  $dirpath = file_directory_path() . ($imce['dir'] == '.' ? '' : '/'. $imce['dir']);
  $filepath = $dirpath .'/'. $filename;

  //check if the file is an image
  if (!$imce['files'][$filename]['width'] || !$img = imce_image_info($filepath)) {
    drupal_set_message(t('%filename is not an image.', array('%filename' => utf8_encode($filename))), 'error', FALSE);
    return FALSE;
  // new stuff begins here.
  if (!$height && $width) {
    // auto-adjust height to fit aspect ratio, if no explicit height is given or it is set to zero.
    $height = round($width * $img['height'] / $img['width']);
  elseif (!$width && $height) {
    // auto-adjust width to fit aspect ratio, if no explicit width is given or it is set to zero.
    $width = round($height * $img['width'] / $img['height']);
  else if (!$width && !$height) {
    // neither width nor height given - we have a problem.
    // TODO: Show an error message, but don't break the ajax request.

EDIT: Changed the code a bit.

donquixote’s picture

Status: Active » Needs review

I would say, just commit the above code, and later think about better documentation on the configuration form.

donquixote’s picture

Status: Active » Needs review

The current behavior for "120x0" or "0x120" or "120x" results in an ajax error. So, I think it is a good idea to do something meaningful instead.

Anonymous’s picture

Preservation of aspect ratio for thumbnails would be a useful addition to this project.

ufku’s picture

Please check out "Default method for creating thumbnails" under common settings at admin/settings/imce. (admin/config/media/imce in D7)

durum’s picture


joelrotelli’s picture

Priority: Normal » Major

Is the code above has been ported to the project ? Cause it will be very usefull !

Perignon’s picture

Version: master » 7.x-1.x-dev
Issue summary: View changes

Want to bump this issue up because I think this would be really nice to have. Going to change to the 1.x-dev branch to get

Perignon’s picture

Actually i think this has been implemented via JS in the UI. I just tried doing some resizing to find that IMCE auto populated the other dimension with a proportional number.

KoshaK’s picture

The same question was raised already for Version 6 few years ago. Very strange that this future still not implemented. Will wait for this future as I'm really need it.