Comment #142
Problem/Motivation
When creating an Image Style that applies the image_crop effect with one or both dimensions exceeding the source dimensions, the new area is rendered in black. Site administrators should be able to configure the color of the new area being created. This should be a per-style configuration, NOT a global one. Site administrators may have styles that integrates with light layouts and there, they want white background for cropped image. Other style may be necessary for dark pages and there they may want heavy-gray background color.
Proposed resolution
Adding a background color at effect level for image_crop
.
Remaining tasks
N/A
User interface changes
When configuring a Crop (image_crop
) effect at admin/config/media/image-styles/manage/{image_style}/add/image_rotate
or admin/config/media/image-styles/manage/{image_style}/effects/{image_effect}
admins should be able to provide a color, in hex string format, for the additional area created by upscaling crops.
API changes
Note: These API changes assure full backward compatibility.
core/includes/image.inc
image_crop()
has 2 additional optional arguments:
$background_color
, defaults toNULL
.$background_alfa
, defaults to0
.
image_rotate()
was unified to the same logic.
Before:
function image_crop($image, $x, $y, $width, $height)
function image_rotate($image, $degrees, $background = NULL)
Now:
function image_crop($image, $x, $y, $width, $background_color = NULL, $background_alpha = 0)
function image_rotate($image, $degrees, $background_color = NULL, $background_alpha = 0)
\Drupal\system\Plugin\ImageToolkitInterface
Applied to \Drupal\system\Plugin\ImageToolkit\GDToolkit
implementation too.
Crop & rotate public methods have adapted their argument list in the same way.
New \Drupal\image\ImageEffectBackgroundColorBase
abstract
Now CropImageEffect
and RotateImageEffect
are extending the new ImageEffectBackgroundColorBase
:
abstract class ImageEffectBackgroundColorBase extends ImageEffectBase implements ConfigurableImageEffectInterface {
...
}
This new base class adds some code reusing for effects with background color.
Original report by FiReaNG3L
ImageAPI.module sounds very interesting; I recently had problems with imagecache / image.inc putting a black background on images when scaling / cropping to a definite size (say 150x150 pixels) when the image is too wide or tall to fit the square.
Is there a way to customize the background to another color (say, white) with image API?
I tried to modify image.inc myself but the best I could achieve is a background change that worked, but bleeded in the image if black was present.
Thanks for all the awesome work on imagecache / imageAPI!