I'd backported the image api changes that were committed to core in #373613: Create "Image" Objects, Operate on Images By Resource to the ImageAPI module. Over in #422836-6: ImageAPI GD2 6.x-1.5 ignores crop background color setting evolvingweb pointed out that you can use the crop to enlarge an image and expose the background which would default to black. We need to add a $background parameter to the image_crop() function to allow the user to specify the color and match up with image_rotate().

CommentFileSizeAuthor
#6 image_crop_bg.patch17.44 KBdrewish
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Stefan Nagtegaal’s picture

This is a theme dependant color imo, and should be used like that.
If you want to do this right, the only thing you could do is to re-generate the image and have a transparent background which needs:
- your image to be converted to png to allow transluscency (gif isn't reliable when rotating your image if != 90, 180, 270 °C)

Would it make sense to make a theme dependant constant like:

define theme('IMAGE_BACKGROUND_COLOR', 'transparent');

I'm not sure about this and we need to reach a concensus about this before I am going to start coding on this, which is pretty straight forward.

Stefan Nagtegaal’s picture

Status: Active » Postponed (maintainer needs more info)

i think this should be the right status for this...

drewish’s picture

Status: Postponed (maintainer needs more info) » Active

I disagree, I think it's a preset dependent setting. Some of my presets I want to have a white background, others gray and others transparent. I shouldn't have to pick one for the entire site.

drewish’s picture

After spending a little time working on a patch for this I'm understanding what you're saying. But I think we actually need to take a slightly different tactic and specify color and transparency separately, e.g.

function image_crop(stdClass $image, $x, $y, $width, $height) {

becomes:

function image_crop(stdClass $image, $x, $y, $width, $height, $transparent = TRUE, $background = 0xffffff) {

and

function image_rotate(stdClass $image, $degrees, $background = NULL) {

becomes:

function image_rotate(stdClass $image, $degrees, $transparent = TRUE, $background = 0xffffff) {

This way the background color can be specified without needing to be concerned about the file format. If it supports transparency then you get transparent backgrounds and if not you get the specified color. Update: or in the case of GIF files, you can specify which color will be used for transparency if one isn't already in use.

dergachev’s picture

I spent some time trying to figure this out but imagecopyresampled was just too much of a beast.
Fortunately, the fantastic module Imagecache Actions provides a few additional actions that do what I need here.
Specifically, the "Define Canvas" action is just a negative crop with a color picker.

See http://drupal.org/project/imagecache_actions

Perhaps at least some of those actions should follow Imagecache into core?

drewish’s picture

FileSize
17.44 KB

Man this code is really tricky to get right. Here's my broken but in progress patch that at least works for rotation. Cropping still needs a ton of work.

The code is borked enough that the unit tests aren't helpful since you can't see what's wrong. So I've been using this snip it in the PHP execute block to test it:

$bg = 0xff00ff;
foreach (array(true =>'transparent', false => 'solid') as $transparent => $type) {
  $img = "$type <br>";
  foreach (array('jpg', 'png', 'gif') as $ext) {
#dsm($ext);
    $input = 'sites/default/files/simpletest/image-test.' . $ext;
    $output = 'sites/default/files/image-test.' . $ext;
    $image = image_load($input, 'gd');
    image_crop($image, -20, -10, 80, 40, $bg, $transparent);
#    image_gd_rotate($image, 30, $bg, $transparent);
    image_save($image, $output);
    $img .= "<span style='background-color: #ccc'> $ext:" . theme('image', file_create_path($output)) ."</span>";
  }
  drupal_set_message($img);
}
hanoii’s picture

Title: Specify background color for crops that enlarge the canvas » Separate background color and transparency options, add them to crops that enlarge the canvas
Status: Active » Needs work

Very interested in this issue as I am experiencing the black background behavior in D5 with version 1.5, not with 1.4.

dman’s picture

Any reason why you are not using imageapi_hex2rgba() ?

+  $rgb = array();
+  for ($i = 16; $i >= 0; $i -= 8) {
+    $rgb[] = (($background >> $i) & 0xFF);
   }

This triggered me to write up my suggestion for improvements to color passing over at : #471816: API suggestion - use keyed color names, not ints in imageapi_hex2rgba()

drewish’s picture

well imageapi_hex2rgba() isn't in core and i benchmarked it and the for loop is actually slower than just doing it in an array.

drewish’s picture

Component: base system » image system
jantoine’s picture

subscribe