blob: cde4e44dce48d45a2a2e75e36d4fc8fb45a290e9 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001########################
2Image Manipulation Class
3########################
4
5CodeIgniter's Image Manipulation class lets you perform the following
6actions:
7
8- Image Resizing
9- Thumbnail Creation
10- Image Cropping
11- Image Rotating
12- Image Watermarking
13
14All three major image libraries are supported: GD/GD2, NetPBM, and
15ImageMagick
16
17.. note:: Watermarking is only available using the GD/GD2 library. In
18 addition, even though other libraries are supported, GD is required in
19 order for the script to calculate the image properties. The image
20 processing, however, will be performed with the library you specify.
21
22**********************
23Initializing the Class
24**********************
25
26Like most other classes in CodeIgniter, the image class is initialized
27in your controller using the $this->load->library function::
28
29 $this->load->library('image_lib');
30
31Once the library is loaded it will be ready for use. The image library
Andrey Andreev3a18df12014-01-06 12:59:11 +020032object you will use to call all functions is: ``$this->image_lib``
Derek Jones8ede1a22011-10-05 13:34:52 -050033
34Processing an Image
35===================
36
37Regardless of the type of processing you would like to perform
38(resizing, cropping, rotation, or watermarking), the general process is
39identical. You will set some preferences corresponding to the action you
40intend to perform, then call one of four available processing functions.
41For example, to create an image thumbnail you'll do this::
42
Derek Jones156c4022011-10-05 15:58:56 -050043 $config['image_library'] = 'gd2';
44 $config['source_image'] = '/path/to/image/mypic.jpg';
45 $config['create_thumb'] = TRUE;
46 $config['maintain_ratio'] = TRUE;
47 $config['width'] = 75;
48 $config['height'] = 50;
49
50 $this->load->library('image_lib', $config);
51
52 $this->image_lib->resize();
Derek Jones8ede1a22011-10-05 13:34:52 -050053
54The above code tells the image_resize function to look for an image
55called *mypic.jpg* located in the source_image folder, then create a
56thumbnail that is 75 X 50 pixels using the GD2 image_library. Since the
57maintain_ratio option is enabled, the thumb will be as close to the
58target width and height as possible while preserving the original aspect
59ratio. The thumbnail will be called *mypic_thumb.jpg*
60
61.. note:: In order for the image class to be allowed to do any
62 processing, the folder containing the image files must have write
63 permissions.
64
65.. note:: Image processing can require a considerable amount of server
66 memory for some operations. If you are experiencing out of memory errors
67 while processing images you may need to limit their maximum size, and/or
68 adjust PHP memory limits.
69
Andrey Andreev3a18df12014-01-06 12:59:11 +020070Processing Methods
71==================
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Andrey Andreev3a18df12014-01-06 12:59:11 +020073There are four available processing methods:
Derek Jones8ede1a22011-10-05 13:34:52 -050074
75- $this->image_lib->resize()
76- $this->image_lib->crop()
77- $this->image_lib->rotate()
78- $this->image_lib->watermark()
Derek Jones8ede1a22011-10-05 13:34:52 -050079
Andrey Andreev3a18df12014-01-06 12:59:11 +020080These methods return boolean TRUE upon success and FALSE for failure.
Derek Jones8ede1a22011-10-05 13:34:52 -050081If they fail you can retrieve the error message using this function::
82
83 echo $this->image_lib->display_errors();
84
85A good practice is use the processing function conditionally, showing an
86error upon failure, like this::
87
Derek Jones156c4022011-10-05 15:58:56 -050088 if ( ! $this->image_lib->resize())
89 {
Andrey Andreev3a18df12014-01-06 12:59:11 +020090 echo $this->image_lib->display_errors();
Derek Jones156c4022011-10-05 15:58:56 -050091 }
Derek Jones8ede1a22011-10-05 13:34:52 -050092
Andrey Andreev9438e262012-10-05 13:16:27 +030093.. note:: You can optionally specify the HTML formatting to be applied to
94 the errors, by submitting the opening/closing tags in the function,
95 like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050096
97 $this->image_lib->display_errors('<p>', '</p>');
98
Andrey Andreev3a18df12014-01-06 12:59:11 +020099.. _processing-preferences:
100
Derek Jones8ede1a22011-10-05 13:34:52 -0500101Preferences
102===========
103
104The preferences described below allow you to tailor the image processing
105to suit your needs.
106
107Note that not all preferences are available for every function. For
108example, the x/y axis preferences are only available for image cropping.
109Likewise, the width and height preferences have no effect on cropping.
110The "availability" column indicates which functions support a given
111preference.
112
113Availability Legend:
114
115- R - Image Resizing
116- C - Image Cropping
117- X - Image Rotation
118- W - Image Watermarking
119
Joseph Wensley80a11812011-10-06 00:22:49 -0400120======================= ======================= =============================== =========================================================================== =============
121Preference Default Value Options Description Availability
122======================= ======================= =============================== =========================================================================== =============
123**image_library** GD2 GD, GD2, ImageMagick, NetPBM Sets the image library to be used. R, C, X, W
124**library_path** None None Sets the server path to your ImageMagick or NetPBM library. If you use R, C, X
125 either of those libraries you must supply the path. R, C, S, W
126**source_image** None None Sets the source image name/path. The path must be a relative or absolute
127 server path, not a URL.
128**dynamic_output** FALSE TRUE/FALSE (boolean) Determines whether the new image file should be written to disk or R, C, X, W
129 generated dynamically. Note: If you choose the dynamic setting, only one
130 image can be shown at a time, and it can't be positioned on the page. It
131 simply outputs the raw image dynamically to your browser, along with
132 image headers.
133**quality** 90% 1 - 100% Sets the quality of the image. The higher the quality the larger the R, C, X, W
134 file size.
135**new_image** None None Sets the destination image name/path. You'll use this preference when R, C, X, W
136 creating an image copy. The path must be a relative or absolute server
137 path, not a URL.
138**width** None None Sets the width you would like the image set to. R, C
139**height** None None Sets the height you would like the image set to. R, C
140**create_thumb** FALSE TRUE/FALSE (boolean) Tells the image processing function to create a thumb. R
141**thumb_marker** _thumb None Specifies the thumbnail indicator. It will be inserted just before the R
142 file extension, so mypic.jpg would become mypic_thumb.jpg
143**maintain_ratio** TRUE TRUE/FALSE (boolean) Specifies whether to maintain the original aspect ratio when resizing or R, C
144 use hard values.
145**master_dim** auto auto, width, height Specifies what to use as the master axis when resizing or creating R
146 thumbs. For example, let's say you want to resize an image to 100 X 75
147 pixels. If the source image size does not allow perfect resizing to
148 those dimensions, this setting determines which axis should be used as
149 the hard value. "auto" sets the axis automatically based on whether the
Andrey Andreevba231aa2014-01-20 16:43:41 +0200150 image is taller than wider, or vice versa.
Joseph Wensley80a11812011-10-06 00:22:49 -0400151**rotation_angle** None 90, 180, 270, vrt, hor Specifies the angle of rotation when rotating images. Note that PHP X
152 rotates counter-clockwise, so a 90 degree rotation to the right must be
153 specified as 270.
154**x_axis** None None Sets the X coordinate in pixels for image cropping. For example, a C
155 setting of 30 will crop an image 30 pixels from the left.
156**y_axis** None None Sets the Y coordinate in pixels for image cropping. For example, a C
157 setting of 30 will crop an image 30 pixels from the top.
158======================= ======================= =============================== =========================================================================== =============
159
Derek Jones8ede1a22011-10-05 13:34:52 -0500160Setting preferences in a config file
161====================================
162
163If you prefer not to set preferences using the above method, you can
164instead put them into a config file. Simply create a new file called
165image_lib.php, add the $config array in that file. Then save the file
Andrey Andreev3a18df12014-01-06 12:59:11 +0200166in *config/image_lib.php* and it will be used automatically. You will
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600167NOT need to use the ``$this->image_lib->initialize()`` method if you save
Derek Jones8ede1a22011-10-05 13:34:52 -0500168your preferences in a config file.
169
Derek Jones8ede1a22011-10-05 13:34:52 -0500170******************
171Image Watermarking
172******************
173
174The Watermarking feature requires the GD/GD2 library.
175
176Two Types of Watermarking
177=========================
178
179There are two types of watermarking that you can use:
180
181- **Text**: The watermark message will be generating using text, either
182 with a True Type font that you specify, or using the native text
183 output that the GD library supports. If you use the True Type version
184 your GD installation must be compiled with True Type support (most
185 are, but not all).
186- **Overlay**: The watermark message will be generated by overlaying an
187 image (usually a transparent PNG or GIF) containing your watermark
188 over the source image.
189
Andrey Andreev3a18df12014-01-06 12:59:11 +0200190.. _watermarking:
191
Derek Jones8ede1a22011-10-05 13:34:52 -0500192Watermarking an Image
193=====================
194
Andrey Andreev3a18df12014-01-06 12:59:11 +0200195Just as with the other methods (resizing, cropping, and rotating) the
Derek Jones8ede1a22011-10-05 13:34:52 -0500196general process for watermarking involves setting the preferences
197corresponding to the action you intend to perform, then calling the
198watermark function. Here is an example::
199
Derek Jones156c4022011-10-05 15:58:56 -0500200 $config['source_image'] = '/path/to/image/mypic.jpg';
201 $config['wm_text'] = 'Copyright 2006 - John Doe';
202 $config['wm_type'] = 'text';
203 $config['wm_font_path'] = './system/fonts/texb.ttf';
204 $config['wm_font_size'] = '16';
205 $config['wm_font_color'] = 'ffffff';
206 $config['wm_vrt_alignment'] = 'bottom';
207 $config['wm_hor_alignment'] = 'center';
208 $config['wm_padding'] = '20';
209
210 $this->image_lib->initialize($config);
211
212 $this->image_lib->watermark();
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
214The above example will use a 16 pixel True Type font to create the text
215"Copyright 2006 - John Doe". The watermark will be positioned at the
216bottom/center of the image, 20 pixels from the bottom of the image.
217
218.. note:: In order for the image class to be allowed to do any
Andrey Andreev9438e262012-10-05 13:16:27 +0300219 processing, the image file must have "write" file permissions
220 For example, 777.
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
222Watermarking Preferences
223========================
224
225This table shown the preferences that are available for both types of
226watermarking (text or overlay)
227
Joseph Wensley80a11812011-10-06 00:22:49 -0400228======================= =================== ======================= ==========================================================================
229Preference Default Value Options Description
230======================= =================== ======================= ==========================================================================
231**wm_type** text text, overlay Sets the type of watermarking that should be used.
232**source_image** None None Sets the source image name/path. The path must be a relative or absolute
233 server path, not a URL.
234**dynamic_output** FALSE TRUE/FALSE (boolean) Determines whether the new image file should be written to disk or
235 generated dynamically. Note: If you choose the dynamic setting, only one
236 image can be shown at a time, and it can't be positioned on the page. It
237 simply outputs the raw image dynamically to your browser, along with
238 image headers.
239**quality** 90% 1 - 100% Sets the quality of the image. The higher the quality the larger the
240 file size.
David Dotsond0c09b92011-11-21 09:56:39 -0600241**wm_padding** None A number The amount of padding, set in pixels, that will be applied to the
Joseph Wensley80a11812011-10-06 00:22:49 -0400242 watermark to set it away from the edge of your images.
243**wm_vrt_alignment** bottom top, middle, bottom Sets the vertical alignment for the watermark image.
244**wm_hor_alignment** center left, center, right Sets the horizontal alignment for the watermark image.
245**wm_hor_offset** None None You may specify a horizontal offset (in pixels) to apply to the
246 watermark position. The offset normally moves the watermark to the
247 right, except if you have your alignment set to "right" then your offset
248 value will move the watermark toward the left of the image.
249**wm_vrt_offset** None None You may specify a vertical offset (in pixels) to apply to the watermark
250 position. The offset normally moves the watermark down, except if you
251 have your alignment set to "bottom" then your offset value will move the
252 watermark toward the top of the image.
253======================= =================== ======================= ==========================================================================
254
Derek Jones8ede1a22011-10-05 13:34:52 -0500255Text Preferences
256----------------
257
258This table shown the preferences that are available for the text type of
259watermarking.
260
Joseph Wensley80a11812011-10-06 00:22:49 -0400261======================= =================== =================== ==========================================================================
262Preference Default Value Options Description
263======================= =================== =================== ==========================================================================
264**wm_text** None None The text you would like shown as the watermark. Typically this will be a
265 copyright notice.
266**wm_font_path** None None The server path to the True Type Font you would like to use. If you do
267 not use this option, the native GD font will be used.
268**wm_font_size** 16 None The size of the text. Note: If you are not using the True Type option
269 above, the number is set using a range of 1 - 5. Otherwise, you can use
270 any valid pixel size for the font you're using.
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200271**wm_font_color** ffffff None The font color, specified in hex. Both the full 6-length (ie, 993300) and
272 the short three character abbreviated version (ie, fff) are supported.
Joseph Wensley80a11812011-10-06 00:22:49 -0400273**wm_shadow_color** None None The color of the drop shadow, specified in hex. If you leave this blank
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200274 a drop shadow will not be used. Both the full 6-length (ie, 993300) and
275 the short three character abbreviated version (ie, fff) are supported.
Joseph Wensley80a11812011-10-06 00:22:49 -0400276**wm_shadow_distance** 3 None The distance (in pixels) from the font that the drop shadow should
277 appear.
278======================= =================== =================== ==========================================================================
279
Derek Jones8ede1a22011-10-05 13:34:52 -0500280Overlay Preferences
281-------------------
282
283This table shown the preferences that are available for the overlay type
284of watermarking.
285
Joseph Wensley80a11812011-10-06 00:22:49 -0400286======================= =================== =================== ==========================================================================
287Preference Default Value Options Description
288======================= =================== =================== ==========================================================================
289**wm_overlay_path** None None The server path to the image you wish to use as your watermark. Required
290 only if you are using the overlay method.
291**wm_opacity** 50 1 - 100 Image opacity. You may specify the opacity (i.e. transparency) of your
292 watermark image. This allows the watermark to be faint and not
293 completely obscure the details from the original image behind it. A 50%
294 opacity is typical.
295**wm_x_transp** 4 A number If your watermark image is a PNG or GIF image, you may specify a color
296 on the image to be "transparent". This setting (along with the next)
297 will allow you to specify that color. This works by specifying the "X"
298 and "Y" coordinate pixel (measured from the upper left) within the image
299 that corresponds to a pixel representative of the color you want to be
300 transparent.
301**wm_y_transp** 4 A number Along with the previous setting, this allows you to specify the
302 coordinate to a pixel representative of the color you want to be
303 transparent.
304======================= =================== =================== ==========================================================================
Andrey Andreev3a18df12014-01-06 12:59:11 +0200305
306***************
307Class Reference
308***************
309
310.. class:: CI_Image_lib
311
312 .. method:: initialize([$props = array()])
313
314 :param array $props: Image processing preferences
315 :returns: void
316
317 Initializes the class for processing an image.
318
319 .. method:: resize()
320
321 :returns: bool
322
323 The image resizing method lets you resize the original image, create a
324 copy (with or without resizing), or create a thumbnail image.
325
326 For practical purposes there is no difference between creating a copy
327 and creating a thumbnail except a thumb will have the thumbnail marker
328 as part of the name (i.e. mypic_thumb.jpg).
329
330 All preferences listed in the :ref:`processing-preferences` table are available for this
331 method except these three: *rotation_angle*, *x_axis* and *y_axis*.
332
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600333 **Creating a Thumbnail**
Andrey Andreev3a18df12014-01-06 12:59:11 +0200334
335 The resizing method will create a thumbnail file (and preserve the
336 original) if you set this preference to TRUE::
337
338 $config['create_thumb'] = TRUE;
339
340 This single preference determines whether a thumbnail is created or not.
341
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600342 **Creating a Copy**
Andrey Andreev3a18df12014-01-06 12:59:11 +0200343
344 The resizing method will create a copy of the image file (and preserve
345 the original) if you set a path and/or a new filename using this
346 preference::
347
348 $config['new_image'] = '/path/to/new_image.jpg';
349
350 Notes regarding this preference:
351
352 - If only the new image name is specified it will be placed in the same
353 folder as the original
354 - If only the path is specified, the new image will be placed in the
355 destination with the same name as the original.
356 - If both the path and image name are specified it will placed in its
357 own destination and given the new name.
358
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600359 **Resizing the Original Image**
Andrey Andreev3a18df12014-01-06 12:59:11 +0200360
361 If neither of the two preferences listed above (create_thumb, and
362 new_image) are used, the resizing method will instead target the
363 original image for processing.
364
365 .. method:: crop()
366
367 :returns: bool
368
369 The cropping method works nearly identically to the resizing function
370 except it requires that you set preferences for the X and Y axis (in
371 pixels) specifying where to crop, like this::
372
373 $config['x_axis'] = '100';
374 $config['y_axis'] = '40';
375
376 All preferences listed in the :ref:`processing-preferences` table are available for this
377 method except these: *rotation_angle*, *create_thumb* and *new_image*.
378
379 Here's an example showing how you might crop an image::
380
381 $config['image_library'] = 'imagemagick';
382 $config['library_path'] = '/usr/X11R6/bin/';
383 $config['source_image'] = '/path/to/image/mypic.jpg';
384 $config['x_axis'] = '100';
385 $config['y_axis'] = '60';
386
387 $this->image_lib->initialize($config);
388
389 if ( ! $this->image_lib->crop())
390 {
391 echo $this->image_lib->display_errors();
392 }
393
394 .. note:: Without a visual interface it is difficult to crop images, so this
395 method is not very useful unless you intend to build such an
396 interface. That's exactly what we did using for the photo gallery module
397 in ExpressionEngine, the CMS we develop. We added a JavaScript UI that
398 lets the cropping area be selected.
399
400 .. method:: rotate()
401
402 :returns: bool
403
404 The image rotation method requires that the angle of rotation be set
405 via its preference::
406
407 $config['rotation_angle'] = '90';
408
409 There are 5 rotation options:
410
411 #. 90 - rotates counter-clockwise by 90 degrees.
412 #. 180 - rotates counter-clockwise by 180 degrees.
413 #. 270 - rotates counter-clockwise by 270 degrees.
414 #. hor - flips the image horizontally.
415 #. vrt - flips the image vertically.
416
417 Here's an example showing how you might rotate an image::
418
419 $config['image_library'] = 'netpbm';
420 $config['library_path'] = '/usr/bin/';
421 $config['source_image'] = '/path/to/image/mypic.jpg';
422 $config['rotation_angle'] = 'hor';
423
424 $this->image_lib->initialize($config);
425
426 if ( ! $this->image_lib->rotate())
427 {
428 echo $this->image_lib->display_errors();
429 }
430
431 .. method:: watermark()
432
433 :returns: bool
434
435 Creates a watermark over an image, please refer to the :ref:`watermarking`
436 section for more info.
437
438 .. method:: clear()
439
440 :returns: void
441
442 The clear method resets all of the values used when processing an
443 image. You will want to call this if you are processing images in a
444 loop.
445
446 ::
447
448 $this->image_lib->clear();
449
450 .. method:: display_errors([$open = '<p>[, $close = '</p>']])
451
452 :param string $open: Error message opening tag
453 :param string $close: Error message closing tag
454 :returns: string
455
456 Returns all detected errors formatted as a string.
457 ::
458
459 echo $this->image_lib->diplay_errors();