Restored guide

Dynamically Resize Image Dimensions Using jQuery

Archived 2008–2011 · 2 min read · Eric London's Drupal Blog

This restored note walks through a practical way to handle the subject below on the Drupal 6 generation of sites this blog was written for. The note is titled “Dynamically Resize Image Dimensions Using jQuery” and is written for Drupal 6.x unless a line below says otherwise.

What you are solving

Image work in this era means ImageCache presets, ImageField, and a derivative that is generated on demand. Generate heavy presets from Drush or a queue so a single anonymous page view does not exhaust the PHP memory limit. The moving parts in the historical title are: dynamically, resize, image, dimensions, using, jquery.

APIs and modules in play

  • Drupal.behaviors and jQuery inside the behavior context

A practical approach

  1. Step 1. Name the outcome in one sentence: Dynamically Resize Image Dimensions Using jQuery.
  2. Step 2. Reproduce it on a development copy with the same modules enabled.
  3. Step 3. Keep the change in a custom module or in the theme, and leave contributed code untouched.
  4. Step 4. Define the preset (scale, crop, or scale-and-crop) before you write code that assumes a derivative path.
  5. Step 5. Flush only the presets you changed, and generate them from CLI when the source images are large.

Example

The sample is illustrative. Rename the function, swap in your machine names, and run it on a development copy.

// Dynamically Resize Image Dimensions Using jQuery
function notes_dynamically_resize_image_dimensions_using_jquery_preset_url($filepath, $preset = 'thumbnail') {
  $preset = check_plain($preset);
  return file_create_url('imagecache/' . $preset . '/' . $filepath);
}

Check before you deploy

  • Confirm the change on a database copy, then again with the theme registry and page cache cleared.
  • Watch the PHP error log and the watchdog table for the first real request.
  • If the change alters markup, recheck the path with a keyboard and without JavaScript.

Restored guide. The original post was published by Eric London on this domain. This text is a new explanation of that subject, not a reproduction of the archived wording.