The experimental ImageMagick code in the function imageapi_imagemagick_image_definecanvas() appears not to handle the "Resize canvas under image (possibly cropping)" option. I've used the -extent ImageMagick option to implement this. I have no idea if it works in all instances, but it works for me when I want to force images to a fixed size without changing the aspect ratio. The code also supports transparency.

My implementation is:

function imageapi_imagemagick_image_definecanvas(&$image, $action = array()) {
  $targetsize = $action['targetsize'];
  $RGB = $action['RGB'];
  $backgroundcolor = $RGB['HEX'] != '' ? '#'. $RGB['HEX'] : 'None';
  
  # TODO needs work.
  #
  $background = " -background '{$backgroundcolor}' ";
  
  $crop = " -crop   '{$targetsize['width']}x{$targetsize['height']}-{$targetsize['left']}-{$targetsize['top']}!' ";

  $draw = " -draw '";
  $draw .= " fill '{$RGB['HEX']}' polygon 0,0 {$targetsize['width']},0 {$targetsize['width']},{$targetsize['height']} 0,{$targetsize['height']} ";
  $draw .= " ' ";
  #$draw .= " -composite ";
  $draw .= " +repage ";

  $extent = " -extent '{$targetsize['width']}x{$targetsize['height']}-{$targetsize['left']}-{$targetsize['top']}!'";

  if ($action['under']) {
    $compose = " $background $crop $extent ";
  } else {
    $compose = " $background $crop $draw ";
  }

  $image->ops[] = $compose;
  return TRUE; 
}

Comments

dman’s picture

sounds good

tajabosc’s picture

StatusFileSize
new1.67 KB

@Jorrit I tried your code with success using imagemagick, so i made a patch to the module to facilitate further testing.

The file to be patched is canvasactions.inc

Daniele

dman’s picture

Status: Needs review » Fixed

Cool, works for me. This is now in DRUPAL-6--2-0 dev

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jami3z’s picture

Status: Closed (fixed) » Active

Sorry to reopen, didn't know if to create a new issue but this does not work for me using 6.x-2.x-dev (which has the patch applied). I have tried enabling/disabling the module a few times and clearing caches but the warning "Define Canvas not currently supported by using imagemagick. This effect requires GD image toolkit only." still shows and doesnt matter what I set the background HEX to the preview image always shows black.

I have the following installed
- imagecache 6.x-2.x-dev
- imagecache_actions 6.x-2.x-dev
- imageapi 6.x-1.8 (using ImageMagick)

jami3z’s picture

I also have tried the #2 patch on a seperate installed Imagecache_actions 6.x-1.7 version and it did not work for me. Are there any other version requirements for imagecache or imageapi for this to work?

tajabosc’s picture

@jamienotweet

I have the following modules enabled:

-ImageAPI 6.x-1.8
-ImageAPI ImageMagick 6.x-1.8
-ImageCache 6.x-2.0-beta10
-ImageCache UI 6.x-2.0-beta10
-Imagecache Canvas Actions 6.x-1.7

Haven't tested with -dev versions of ImageCache and Imagecache Actions

Andrey Zakharov’s picture

dunno why you are using draw-fill-rectacle construction but there is a command in ImageMagick to define canvas:

/**
 * Draw a color (or transparency) behind an image
 * 
 * $targetsize is an array expected to contain a width,height and a left,top
 * offset.
 */
function imageapi_imagemagick_image_definecanvas(&$image, $action = array()) {
  $targetsize = $action['targetsize'];
  $RGB = $action['RGB'];

  $crop = " -size {$targetsize['width']}x{$targetsize['height']} " .
    "xc:'#{$RGB['HEX']}' -crop '{$targetsize['width']}x{$targetsize['height']}-{$targetsize['left']}-{$targetsize['top']}!' ";
  $draw .= " -composite ";

  $compose = " $crop $draw ";
  $image->ops[] = $compose;
  return TRUE; 
  
}

convert -size 100x100 xc:black test.png do the trick

dman’s picture

I've been testing these, and none of them are working quite right for me so far.

@Andrey Zakharov
The 'fill' was being used in cases where $action['under'] is false. In those cases the expected result is a solid canvas over top of the image. That at least made sense, although it's seldom used in examples so far, it is handy as an early step in compositing.

Your version does a better job with the color, but still seems to fail to do the normal 'make the canvas bigger by padding it with a background' effect we expect. just -crop does not upsize for me.

I'm starting to think there are issues with imagemagick versions, because I just can't get the last two parameters to -extent to have any of their documented effect (offset positioning). -extent '300x350-10-10'
I have ImageMagick 6.2.8 06/11/08 on the OSX desktop, and apt-get is claiming it's the latest version - which I doubt.
Upgrading through other means - recompiling piles of stuff - seems to be hard as it triggers rebuild of a dozen other important dependencies that make me nervous.

dman’s picture

Version: 6.x-1.7 » 6.x-2.x-dev

HUGE fixes. Took me all weekend, but I now have close-to-satisfactory updated support for imagemagick!!
define canvas, overlay and more now work.
in -dev 2.x

Jorrit’s picture

When will there be a release of the 6.x-2.x branch?

fietserwin’s picture

Version: 6.x-2.x-dev » 7.x-1.x-dev
Status: Active » Fixed

Fixed in 7.x-1.x-dev.

I ended up using the -extent option, which looked the best fit to me.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mikeytown2’s picture

Status: Closed (fixed) » Reviewed & tested by the community
StatusFileSize
new2.65 KB

Re-rolled patch in #2 for 6.x-1.x-dev

fietserwin’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
nelslynn’s picture

Any chance to commit this patch to the Recommended releases version? The issue remains using 7.x-1.4

osopolar’s picture

Re-rolled patch in #14 for 6.x-1.x-dev

Couldn't get interdiff working on this. I left out following changes:

  1. +++ b/canvasactions.inc
    @@ -22,7 +22,7 @@
     function canvasactions_definecanvas_form($action) {
       if (imageapi_default_toolkit() != 'imageapi_gd') {
    -    drupal_set_message('Define Canvas not currently supported by using imagemagick. This effect requires GD image toolkit only.', 'warning');
    +//     drupal_set_message('Define Canvas not currently supported by using imagemagick. This effect requires GD image toolkit only.', 'warning');
       }
    

    No need for the if statement with no instructions.

  2. +++ b/canvasactions.inc
    @@ -295,26 +295,34 @@ function imageapi_gd_image_definecanvas(&$image, $action = array()) {
      * $targetsize is an array expected to contain a width,height and a left,top
      * offset.
    + *
    + * Patched with code at issue http://drupal.org/node/844386
      */
     function imageapi_imagemagick_image_definecanvas(&$image, $action = array()) {
    

    No need for this comment as git message points to the issue.

osopolar’s picture

Status: Reviewed & tested by the community » Needs review

Please review again.

fietserwin’s picture

Ignore #19 ... nothing was committed.

fietserwin’s picture

Status: Needs review » Closed (won't fix)

D6 EOL. This module's D6 issues already haven't received any attention for over a year. Closing them all unconditionally now.