Posted by radscientist on December 11, 2007 at 5:42pm
| Project: | Drupal core |
| Version: | 7.x-dev |
| Component: | image system |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (duplicate) |
Issue Summary
This is my first attempt at submitting a patch, so please go easy on me. I have noticed many posts from people using imagecache that would like to avoid having black backgrounds on cropped images. One solution is to allow the scaling function to upscale images, but in my situation, I would just rather have the crop action not perform the crop at all if the image is smaller than the size to be cropped too.
The drawback here is that you can't enforce a Standard size for images across the board - but what can you do when your users upload low resolution images? I say you should just let them be low resolution.
Here is what the patch does, it is pretty simple addition on line 199:
case 'crop':
// Patch to ensure images dont get cropped to a larger size if they are smaller than the crop size, otherwise they will get a black background
if ($action['data']['width'] > $size[0]) {
$action['data']['width'] = $size[0];
$action['data']['xoffset'] = 0;
}
if ($action['data']['height'] > $size[1]) {
$action['data']['height'] = $size[1];
$action['data']['yoffset'] = 0;
}
....| Attachment | Size | Status | Test result | Operations |
|---|---|---|---|---|
| imagecache_5x13_no_crop_background.patch | 907 bytes | Ignored: Check issue status. | None | None |
Comments
#1
Wouldn't it be better to patch image.inc itself instead of imagecache? I'll test this as soon as I get home as I've been experiencing this problem (black background) and found no acceptable solution thus far.
#2
Tested the patch, confirms that it works. However, in times where we absolutely want the image to have the specified height / width, this solution doesn't work - I think it's a minor point.
#3
I agreed so much with the idea of putting a fix into image.inc that I did so.
The following patch adds some extra conditions to both image_gd_crop() and image_crop(). Hopefully, as I have not tested it with Imagick, it should work with both image kits.
This is not necessarily an imagecache patch, but since this issue already exists here, why not add it to the discussion.
Please review and tell me what you all think.
Caviat Emptor: you must provide scale or resize preset to imagecache to see this puppy work. Recall, it is important to have both Width and Height settings set, otherwise Image.inc will through an error.
Happy Drupalling!
#4
I'm moving this to a core issue since the patch is for image.inc.
#5
Coding style needs work. See http://drupal.org/coding-standards
#6
Dang, I couldn't get this patch to work for me -- is it not for Drupal 6?
Anyone have any suggestions on fixes? Imagecache Actions would extremely well for my need, but this black background is making it impossible.
#7
subscribe
#8
Have you guys fixed this yet? I've successfully gotten it to work with this: http://drupal.org/project/imagecache_actions
Just pay careful attention to the versions of imageapi and imagecache_actions that are compatible with each other. The modules seem to be conflicted a bit due to imageapi revisions.
#9
I also would like imagecache not to perform the crop if the width and/or height of the image is smaller than the size to be cropped to. I think it looks much much better to just let users have non standard images sizes than to force an upscale of their image and put big black backgrounds around them. Basically, in my case, I only want the width to be cropped if it goes over 200px and the height to be cropped if it goes over 400px. So can this be done with a custom action? Here is a pic of one of my user's profile image -
http://drupal.org/files/issues/imagecache_problem.jpg
The image is 60x60 and because it is cropped to 200x400 it has the black background. So could a custom action be created that would only crop the image if it's width/height was larger than the specified dimensions?
#10
Figured it out with a custom action. Here's what I did in case someone else has the same problem -
1) First I use a scale action to scale the image to 200px. No upscaling so if the width is less than that the picture will remain unchanged.
2) Then I added a custom action. As the image is never going to be over 200px because of step 1, only the height needs to be cropped.
3) The $width field in imageapi_image_crop should just be set to the value $width which is the width of your existing image, which will either be 200px (max value) or smaller.
if ($height > 400){
// This is to set the y crop offset to center
$yoffset = $height/2 - 400/2;
$image = imageapi_image_crop($image, 0, $yoffset, $width, 400);
}
return $image;
That will let you have smaller images without having black borders around them while still allowing you to crop larger images.
#11
#12
I did it a bit differently. I need standard-sized logos 120x90.
So first I scale 120 width, then scale 90 height.
I used the imagecache_actions module noted and enabled the canvas sub-module.
Then I created a white 120x90 image and saved it under .../sites/default/files/logo_bg.jpg and added a final action to "Underlay" that image.
No PHP required. Bootiful.
#13
I vote for custom background color settings per cropping action, and a general setting, for those cases where the canvas is enlarged. Default setting set to white background. With Drupal's already available color picker. Come on, make it user friendly damnit.
#14
#437702: Separate background color and transparency options, add them to crops that enlarge the canvas had some changes to give the crop a background color.
#15
I found a solution to a similar to dunx but with imagefield crop. We needed to have a client upload logos to their portfolio. The whole process to be "photoshop free" but the problem was some source logos had backgrounds and some didn't. Our page required fixed size logos at the end of the day because we has some jquery fading roll-over animation with the logos.
We needed those logos to:
-Crop with imagefield
-Scale to fit the container (180x140)
-Center Vertically & Horizontally with a White Background
This how I did it.
1. Attached imagefield crop to the content type and the following settings:
- The resolution to crop the image onto: 0
- Enforce box crop box ratio (false)
(So it crops the image as the user needs, but does no scaling/resizing)
2. Created the Imagecache Preset
- Action #1: Scale 180x140 (upscaling allowed) << Note this would work without upscaling enabled but would make very tiny logos.
- Action #2: Define Canvas 180x140 (center, center) #ffffff under image
This uses
http://drupal.org/project/imagecache_actions
http://drupal.org/project/imagefield_crop
Now anyone who can draw a box around their logo can properly crop their logo on the site.
#16
besides coding standards, #3 will need to be rerolled from Drupal root, see instructions at http://drupal.org/patch/create
#17
Awesome - thank you. And thanks to Imagecache Actions! This let me make all thumbnails the same size and avoid the nasty black border.
#18
Hello, working on D6.20 with image 6.x-1.0-alpha6, imagecache 6.x-2.0-beta10
Patch in #3 doesn't work [half it fails] and the patch in #1 also fails.
Is it not going to work with these release versions? The site needs these versions for other reasons...
Seems to me like not having a black backgroun, or this conditional-scale should be a very common feature. Any solutions to get this to work? Or how the patch needs to be changed to run?
Thanks!
#19
BTW here is a good alternative crop which is, as the title suggests, much smarter than the default crop:
http://drupal.org/project/smartcrop
Doesn't add black space around my images anymore, phew!
BTW who would ever want black around an image? doesn't make any sense to me. Not many websites have black backgrounds...?
#20
Thank you, smart crop totally did it for me!
#21
For me it's the same. I get Black background color with smart crop.
I am using it on Views slideshow gallerific + image smart crop.
Don't know what else try to solve it as well as get centered images, they are all at the top left position by default.
#22
#23
Note: issue #204497: Configurable background color when cropping to larger dimensions tries to solve the underlying problem with the background color always being black... The original problem seems to be the same as described here, so maybe this should actually be marked as a duplicate?
#24
I think yes!
Watching there. ;) thanks!