/[drupal]/contributions/modules/imagecache_actions/transparency.inc
ViewVC logotype

Contents of /contributions/modules/imagecache_actions/transparency.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Tue Aug 19 14:33:56 2008 UTC (15 months, 1 week ago) by dman
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, DRUPAL-6--1-1, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.2: +176 -176 lines
File MIME type: text/x-php
synched up new tag branch
1 <?php
2 // $ID: $
3 /**
4 * Helper functions for the alpha action for imagecache
5 *
6 */
7
8
9 /**
10 * Implementation of imagecache_hook_form()
11 *
12 * Settings for alpha actions.
13 *
14 * @param $action array of settings for this action
15 * @return a form definition
16 */
17 function imagecache_alpha_form($action) {
18 $form = array();
19 $form['description'] = array('#value' => t(
20 "<p>Alpha toning is an advanced method of greyscaling or colorizing.
21 It works using transparency, not colour matching.
22 The results of this filter are excellent for using as watermarks,
23 and for 'sepia' type imprints on coloured or textured backgrounds.
24 It converts dark areas of the image to opaque, light to transparent.</p>
25 <p>Note that if you are working with JPEGs, this alpha effect will not last into the final image
26 <em>unless</em> you either <strong>flatten</strong> this image against a background color
27 or image in a later process or <strong>convert</strong> it to a PNG before saving
28 using available imagecache actions.</p>"
29 ));
30 $form['color'] = array(
31 '#type' => 'textfield',
32 '#title' => t('Fill Color'),
33 '#default_value' => $action['color'] ? $action['color'] : "#000000",
34 '#size' => 10,
35 '#description' => t("Although this image will end up as an alpha transparency mask, it still has to have some colour to be visible. Black is safe. Dark Sepia #704214 is good too. If it's not set, the colors will be left as they were, but will unsaturate unevenly.")
36 );
37 $form['flatten'] = array(
38 '#type' => 'checkbox',
39 '#title' => t('Flatten Transparency'),
40 '#default_value' => $action['flatten'],
41 '#return_value' => TRUE,
42 '#description' => t("The opposite of adding alpha transparency, 'flatten' will place the given colour solidly behind the image. Use this if you can't trust IE, or you really do want the image filled in with a solid colour.")
43 );
44 return $form;
45 }
46
47
48 /**
49 * Implementation of theme_hook() for imagecache_ui.module
50 */
51 function theme_imagecache_alpha($element) {
52 return ($element['#value']['flatten'] ? t("Flatten") : t("Transparent"))
53 ." : ". $element['#value']['color'];
54 }
55
56
57 /**
58 * Given an image, manipulate the transparancy behaviour.
59 *
60 * implementation of hook_image()
61 *
62 * Either convert light parts of an image to see-through, or place a solid
63 * colour behind areas that would otherwise be see-though
64 *
65 * An imagecache_action_hook() . Handle a pipelined image transformation.
66 *
67 * To save a partially transparent image, the image resource must be switched to PNG.
68 * REMEMBER TO SWITCH IT BACK if needed
69 *
70 * @param $image handle on the image definition, including a gd image resource
71 * to act upon
72 * @param $data settings for this process.
73 * @return bool success
74 */
75 function imagecache_alpha_image(&$image, $data = array()) {
76 if (! $data['flatten']) {
77 // given an image, convert dark areas to opaque,
78 // light to transparent,
79 return png_color2alpha($image, $data['color']);
80 }
81 else {
82 // Do the opposite, flatten the transparency ONTO the given colour
83 $info = $image->info;
84
85 if (!$info) {
86 watchdog("imagecache", "Problem converting image to fill behind. Source image returned no info");
87 #dsm($source);
88 return; // error
89 }
90
91 $base_image = imagecreatetruecolor($info['width'], $info['height']) ;
92 imagesavealpha($base_image, TRUE);
93 imagealphablending($base_image, FALSE);
94
95 // Start with a solid colour
96 $background_rgb = hex_to_rgb($data['color']) ;
97
98 // Setting the background colour here solid is what flattens the image
99 $background_color = @imagecolorallocatealpha($base_image, $background_rgb['red'], $background_rgb['green'], $background_rgb['blue'], 0) ;
100
101 // But what I really want to do is set it
102 // coloured rgb AND 100% transparent, in the hope that
103 // a failure to render transparency would instead render
104 // THAT colour.
105 $background_color = @imagecolorallocatealpha($base_image, $background_rgb['red'], $background_rgb['green'], $background_rgb['blue'], 0) ;
106 // But that still doesn't work.
107 // Yet somehow I've seen transparent images that fallback to white, not silver.
108
109 imagefill( $base_image, 0, 0, $background_color );
110
111 // And set the overlay behaviour back again
112 imagealphablending($base_image, TRUE);
113
114 // Place the current image over it
115 $foreground = $image->res;
116 $success = imagecopy($base_image, $image->res, 0, 0, 0, 0, $info['width'], $info['height']);
117
118 # imagesavealpha($image, FALSE);
119
120 $image->res = $base_image;
121 return TRUE;
122 }
123 }
124
125 /**
126 * This achives a tonal effect by converting the images combined tone and
127 * existing transparency into one shade value. This is then used as the ALPHA
128 * transparency for that pixel, while the whole thing is coloured the same
129 * shade. Images 'greytoned' in this manner should sit smoothly on any
130 * background.
131 *
132 * With no color set, use the existing hue.
133 *
134 * To save a partially transparent image, the image resource must be switched to PNG.
135 * ... or maybe not. Just flatten it yourself, or swithch the format yourself.
136 * This hack would produce side effects otherwise.
137 */
138 function png_color2alpha(&$image, $color) {
139 #$image->info['extension'] = 'png';
140 #$image->info['mime_type'] = 'image/png';
141 $info = $image->info;
142 if (!$info) { return FALSE; }
143
144 $im1 = $image->res;
145
146 imagesavealpha( $im1, TRUE);
147 imagealphablending($im1, FALSE);
148
149 if ($color) $background = hex_to_rgb($color);
150 $width = imagesx($im1);
151 $height = imagesy($im1);
152 for ($i = 0; $i < $height; $i++) { //this loop traverses each row in the image
153 for ($j = 0; $j < $width; $j++) { //this loop traverses each pixel of each row
154
155 // Get the color & alpha info of the current pixel
156 $retrieved_color = imagecolorat($im1, $j, $i); // an index
157 $rgba_array = imagecolorsforindex($im1, $retrieved_color);
158
159 // Calculate the total shade value of this pixel
160 $lightness = ( $rgba_array['red'] + $rgba_array['green'] + $rgba_array['blue'] ) /3;
161 // Need to flip the numbers around before doing maths.
162 #$opacity = 1-($rgba_array['alpha']/127);
163 #$darkness = 1-($lightness/256); // 0 is white, 1 is black
164 #$visibility = $darkness * $opacity;
165 #$alpha = (1-$visibility) * 127;
166 # =>
167 $alpha = (1- ((1-($lightness/256)) * (1-($rgba_array['alpha']/127)))) * 127;
168 if (!$color) $background=$rgba_array;
169 //paint the pixel
170 $color_to_paint = imagecolorallocatealpha($image->res, $background['red'], $background['green'], $background['blue'], $alpha);
171 imagesetpixel($image->res, $j, $i, $color_to_paint);
172 }
173 }
174 return TRUE;
175 }
176

  ViewVC Help
Powered by ViewVC 1.1.2