/[drupal]/contributions/modules/imagecache_actions/imagecache_coloractions.module
ViewVC logotype

Contents of /contributions/modules/imagecache_actions/imagecache_coloractions.module

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


Revision 1.4 - (show annotations) (download) (as text)
Wed Oct 8 04:24:38 2008 UTC (13 months, 2 weeks ago) by dman
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.3: +40 -33 lines
File MIME type: text/x-php
Incorrectly branched with a local version number. Better to branch D6 then version releases after that. I think
1 <?php
2 // $Id: imagecache_coloractions.module,v 1.3.2.1 2008/09/03 17:55:40 dman Exp $
3
4 /**
5 * @file
6 * Additional actions for imagecache processing.
7 *
8 * Exposes some of the simpler PHP 'imagefilter' actions (colorshift,
9 * brightness, negative)
10 * - A transparency masker for merging with backgrounds.
11 * - A pseudo - file conversion feature.
12 *
13 * Compatible with the 2008 revision (imagecache 2)
14 *
15 * @author dan http://coders.co.nz
16 */
17
18 require_once('utility.inc');
19
20 // During devel, caching is pointless. Flush it
21 //imagecache_action_definitions(TRUE);
22
23 /**
24 * Implementation of hook_imagecache_actions().
25 *
26 * Declare available actions, return help text about this filter.
27 */
28 function imagecache_coloractions_imagecache_actions() {
29 $actions = array(
30 'imagecache_colorshift' => array(
31 'name' => 'Color Shift',
32 'description' => 'Adjust image colors.',
33 ),
34 'imagecache_brightness' => array(
35 'name' => 'Brightness',
36 'description' => 'Adjust image brightness.',
37 ),
38 'imagecache_inverse' => array(
39 'name' => t('Negative Image'),
40 'description' => t('Invert colors and brightness.')
41 ),
42 'imagecache_convert' => array(
43 'name' => t('Change File format'),
44 'description' => t('Choose to save the image as a different filetype.')
45 ),
46 'imagecache_alpha' => array(
47 'name' => t('Alpha Transparency'),
48 'description' => t('Adjust transparency.'),
49 'file' => 'transparency.inc',
50 ),
51 );
52 return $actions;
53 }
54
55 function imagecache_coloractions_theme() {
56 return array(
57 'imagecache_colorshift' => array(
58 'arguments' => array('element' => NULL),
59 ),
60 );
61 }
62
63 /**
64 * Implementation of imagecache_hook_form()
65 *
66 * Settings for colorshift actions.
67 *
68 * @param $action array of settings for this action
69 * @return a form definition
70 */
71 function imagecache_colorshift_form($action) {
72 $form = array('#theme' => 'imagecache_rgb_form');
73 $form['RGB'] = imagecache_rgb_form($action['RGB']);
74 return $form;
75 }
76
77
78 /**
79 * Implementation of theme_hook() for imagecache_ui.module
80 */
81 function theme_imagecache_colorshift($element) {
82 $action = $element['#value'];
83 return theme_canvasactions_rgb($action['RGB']);
84 }
85
86
87 /**
88 * Implementation of hook_image()
89 *
90 * Process the imagecache action on the passed image
91 *
92 * Just converts and passes the vals to the all-purpose 'filter' action
93 */
94 function imagecache_colorshift_image(&$image, $data = array()) {
95 // convert color from hex (as it is stored in the UI)
96 if($data['RGB']['HEX'] && $deduced = hex_to_rgb($data['RGB']['HEX'])) {
97 $data['RGB'] = array_merge($data['RGB'], $deduced);
98 }
99
100 $data['filter'] = 4;
101 $data['filter_arg1'] = $data['RGB']['red'];
102 $data['filter_arg2'] = $data['RGB']['green'];
103 $data['filter_arg3'] = $data['RGB']['blue'];
104 return imagecache_imagefilter($image, $data);
105 }
106
107 /**
108 * Implementation of imagecache_hook_form()
109 *
110 * Settings for colorshift actions.
111 *
112 * @param $action array of settings for this action
113 * @return a form definition
114 */
115 function imagecache_brightness_form($action) {
116 $form = array();
117 $form['filter_arg1'] = array(
118 '#type' => 'textfield',
119 '#title' => t('Brightness'),
120 '#description' => t('-255 - +255'),
121 '#default_value' => $action['filter_arg1'],
122 '#size' => 3
123 );
124 return $form;
125 }
126
127 /**
128 * Implementation of hook_image()
129 *
130 * Process the imagecache action on the passed image
131 */
132 function imagecache_brightness_image(&$image, $data = array()) {
133 $data['filter'] = 2;
134 return imagecache_imagefilter($image, $data);
135 }
136
137 /**
138 * Implementation of theme_hook() for imagecache_ui.module
139 */
140 function theme_imagecache_brightness($element) {
141 return t("Adjust") ." : ". $element['#value']['filter_arg1'];
142 }
143
144
145 /**
146 * Implementation of imagecache_hook_form()
147 *
148 * No settings.
149 *
150 * @param $action array of settings for this action
151 * @return a form definition
152 */
153 function imagecache_inverse_form($action) {
154 $form = array();
155 return $form;
156 }
157
158 /**
159 * Implementation of hook_image()
160 *
161 * Process the imagecache action on the passed image
162 */
163 function imagecache_inverse_image(&$image, $data = array()) {
164 $data['filter'] = 0;
165 return imagecache_imagefilter($image, $data);
166 }
167
168
169 /**
170 * Stub for the image toolkit.
171 *
172 * Used by brightness and colorize
173 *
174 * TODO: other toolkits unimplimented yet.
175 * Just forward the job to gdtoolkit for
176 * now
177 *
178 * @param $image handle on the image definition, including a gd image resource
179 * to act upon
180 * @param $data settings for this process.
181 * @return bool success
182 */
183 function imagecache_imagefilter($image, $data) {
184 if (! imagecache_gd_imagefilter($image, $data['filter'], $data['filter_arg1'], $data['filter_arg2'], $data['filter_arg3'] )) {
185 watchdog('imagecache', 'imagecache_imagefilter failed. image: %image, data: %data.', array('%path' => $image, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
186 return FALSE;
187 }
188 return TRUE;
189 }
190
191 /**
192 * Attempt to run imagefilter, which may or may not be included with your
193 * gdtoolkit. If it isn't, a local script is used to emulate the simpler of its
194 * functions.
195 */
196 function imagecache_gd_imagefilter($image, $filter, $arg1, $arg2, $arg3) {
197 // some distros that allegedly include PHP5 GD2 are faulty.
198 // thankyou http://www.weberdev.com/get_example-4601.html
199 if (!function_exists('imagefilter')) {
200 include_once('imagefilter.inc');
201 }
202 $info = $image->info;
203 if (!$info) {
204 return FALSE;
205 }
206 #dpm("run imagefilter imagefilter($image->res, $filter, $arg1, $arg2, $arg3)");
207
208 imagesavealpha($image->res, TRUE);
209 return imagefilter($image->res, $filter, $arg1, $arg2, $arg3);
210 }
211
212
213 /**
214 * Implementation of imagecache_hook_form()
215 *
216 * @param $action array of settings for this action
217 * @return a form definition
218 */
219 function imagecache_convert_form($action) {
220 $form = array(
221 'help' => array(
222 '#type' => 'markup',
223 '#value' => t("If you've been using transparencies in the process, the result may get saved as a PNG (as the image was treated as a one in in-between processes). If this is not desired (file sizes may get too big) you should use this process to force a flatten action before saving. "),
224 ),
225 'help2' => array(
226 '#type' => 'markup',
227 '#value' => t("For technical reasons, changing the file format within imagecache does <em>not</em> change the filename suffix. A png may be saved as a *.jpg or vice versa. This may confuse some browsers and image software, but most of them have no trouble. "),
228 ),
229 'format' => array(
230 '#title' => t("File format"),
231 '#type' => 'select',
232 '#default_value' => $action['format'],
233 '#options' => imagecache_file_formats(),
234 )
235 );
236 return $form;
237 }
238
239 /**
240 * Implementation of theme_hook() for imagecache_ui.module
241 */
242 function theme_imagecache_convert($element) {
243 $formats = imagecache_file_formats();
244 return t("Convert to") ." : ". $formats[$element['#value']['format']];
245 }
246
247 /**
248 * Implementation of hook_image()
249 *
250 * Process the imagecache action on the passed image
251 */
252 function imagecache_convert_image(&$image, $data = array()) {
253 $formats = imagecache_file_formats();
254 $image->info['mime_type'] = $data['format'];
255 $image->info['extension'] = $formats[$data['format']];
256 return TRUE;
257 }
258
259 function imagecache_file_formats() {
260 return array('image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/png' => 'png');
261 }
262
263
264
265
266
267 /**
268 * Need to flush the cache when this module is enabled
269 */
270 function imagecache_coloractions_install() {
271 imagecache_action_definitions(TRUE);
272 drupal_set_message(t('Additional imagecache actions should now be available in the presets !settings_link', array('!settings_link' => l(t('settings'), 'admin/build/imagecache'))));
273 }

  ViewVC Help
Powered by ViewVC 1.1.2