blob: ca464df9cbb1f967dad68793b1cdcb257b5dff0f [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
32object you will use to call all functions is: $this->image_lib
33
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
43 $config['image_library'] = 'gd2'; $config['source_image'] = '/path/to/image/mypic.jpg'; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $config['width'] = 75; $config['height'] = 50; $this->load->library('image_lib', $config); $this->image_lib->resize();
44
45The above code tells the image_resize function to look for an image
46called *mypic.jpg* located in the source_image folder, then create a
47thumbnail that is 75 X 50 pixels using the GD2 image_library. Since the
48maintain_ratio option is enabled, the thumb will be as close to the
49target width and height as possible while preserving the original aspect
50ratio. The thumbnail will be called *mypic_thumb.jpg*
51
52.. note:: In order for the image class to be allowed to do any
53 processing, the folder containing the image files must have write
54 permissions.
55
56.. note:: Image processing can require a considerable amount of server
57 memory for some operations. If you are experiencing out of memory errors
58 while processing images you may need to limit their maximum size, and/or
59 adjust PHP memory limits.
60
61Processing Functions
62====================
63
64There are four available processing functions:
65
66- $this->image_lib->resize()
67- $this->image_lib->crop()
68- $this->image_lib->rotate()
69- $this->image_lib->watermark()
70- $this->image_lib->clear()
71
72These functions return boolean TRUE upon success and FALSE for failure.
73If they fail you can retrieve the error message using this function::
74
75 echo $this->image_lib->display_errors();
76
77A good practice is use the processing function conditionally, showing an
78error upon failure, like this::
79
80 if ( ! $this->image_lib->resize()) {     echo $this->image_lib->display_errors(); }
81
82Note: You can optionally specify the HTML formatting to be applied to
83the errors, by submitting the opening/closing tags in the function, like
84this::
85
86 $this->image_lib->display_errors('<p>', '</p>');
87
88Preferences
89===========
90
91The preferences described below allow you to tailor the image processing
92to suit your needs.
93
94Note that not all preferences are available for every function. For
95example, the x/y axis preferences are only available for image cropping.
96Likewise, the width and height preferences have no effect on cropping.
97The "availability" column indicates which functions support a given
98preference.
99
100Availability Legend:
101
102- R - Image Resizing
103- C - Image Cropping
104- X - Image Rotation
105- W - Image Watermarking
106
107Preference
108Default Value
109Options
110Description
111Availability
112**image_library**
113GD2
114GD, GD2, ImageMagick, NetPBM
115Sets the image library to be used.
116R, C, X, W
117**library_path**
118None
119None
120Sets the server path to your ImageMagick or NetPBM library. If you use
121either of those libraries you must supply the path.
122R, C, X
123**source_image**
124None
125None
126Sets the source image name/path. The path must be a relative or absolute
127server path, not a URL.
128R, C, S, W
129**dynamic_output**
130FALSE
131TRUE/FALSE (boolean)
132Determines whether the new image file should be written to disk or
133generated dynamically. Note: If you choose the dynamic setting, only one
134image can be shown at a time, and it can't be positioned on the page. It
135simply outputs the raw image dynamically to your browser, along with
136image headers.
137R, C, X, W
138**quality**
13990%
1401 - 100%
141Sets the quality of the image. The higher the quality the larger the
142file size.
143R, C, X, W
144**new_image**
145None
146None
147Sets the destination image name/path. You'll use this preference when
148creating an image copy. The path must be a relative or absolute server
149path, not a URL.
150R, C, X, W
151**width**
152None
153None
154Sets the width you would like the image set to.
155R, C
156**height**
157None
158None
159Sets the height you would like the image set to.
160R, C
161**create_thumb**
162FALSE
163TRUE/FALSE (boolean)
164Tells the image processing function to create a thumb.
165R
166**thumb_marker**
167_thumb
168None
169Specifies the thumbnail indicator. It will be inserted just before the
170file extension, so mypic.jpg would become mypic_thumb.jpg
171R
172**maintain_ratio**
173TRUE
174TRUE/FALSE (boolean)
175Specifies whether to maintain the original aspect ratio when resizing or
176use hard values.
177R, C
178**master_dim**
179auto
180auto, width, height
181Specifies what to use as the master axis when resizing or creating
182thumbs. For example, let's say you want to resize an image to 100 X 75
183pixels. If the source image size does not allow perfect resizing to
184those dimensions, this setting determines which axis should be used as
185the hard value. "auto" sets the axis automatically based on whether the
186image is taller then wider, or vice versa.
187R
188**rotation_angle**
189None
19090, 180, 270, vrt, hor
191Specifies the angle of rotation when rotating images. Note that PHP
192rotates counter-clockwise, so a 90 degree rotation to the right must be
193specified as 270.
194X
195**x_axis**
196None
197None
198Sets the X coordinate in pixels for image cropping. For example, a
199setting of 30 will crop an image 30 pixels from the left.
200C
201**y_axis**
202None
203None
204Sets the Y coordinate in pixels for image cropping. For example, a
205setting of 30 will crop an image 30 pixels from the top.
206C
207Setting preferences in a config file
208====================================
209
210If you prefer not to set preferences using the above method, you can
211instead put them into a config file. Simply create a new file called
212image_lib.php, add the $config array in that file. Then save the file
213in: config/image_lib.php and it will be used automatically. You will
214NOT need to use the $this->image_lib->initialize function if you save
215your preferences in a config file.
216
217$this->image_lib->resize()
218===========================
219
220The image resizing function lets you resize the original image, create a
221copy (with or without resizing), or create a thumbnail image.
222
223For practical purposes there is no difference between creating a copy
224and creating a thumbnail except a thumb will have the thumbnail marker
225as part of the name (ie, mypic_thumb.jpg).
226
227All preferences listed in the table above are available for this
228function except these three: rotation_angle, x_axis, and y_axis.
229
230Creating a Thumbnail
231--------------------
232
233The resizing function will create a thumbnail file (and preserve the
234original) if you set this preference to TRUE::
235
236 $config['create_thumb'] = TRUE;
237
238This single preference determines whether a thumbnail is created or not.
239
240Creating a Copy
241---------------
242
243The resizing function will create a copy of the image file (and preserve
244the original) if you set a path and/or a new filename using this
245preference::
246
247 $config['new_image'] = '/path/to/new_image.jpg';
248
249Notes regarding this preference:
250
251- If only the new image name is specified it will be placed in the same
252 folder as the original
253- If only the path is specified, the new image will be placed in the
254 destination with the same name as the original.
255- If both the path and image name are specified it will placed in its
256 own destination and given the new name.
257
258Resizing the Original Image
259---------------------------
260
261If neither of the two preferences listed above (create_thumb, and
262new_image) are used, the resizing function will instead target the
263original image for processing.
264
265$this->image_lib->crop()
266=========================
267
268The cropping function works nearly identically to the resizing function
269except it requires that you set preferences for the X and Y axis (in
270pixels) specifying where to crop, like this::
271
272 $config['x_axis'] = '100'; $config['y_axis'] = '40';
273
274All preferences listed in the table above are available for this
275function except these: rotation_angle, width, height, create_thumb,
276new_image.
277
278Here's an example showing how you might crop an image::
279
280 $config['image_library'] = 'imagemagick'; $config['library_path'] = '/usr/X11R6/bin/'; $config['source_image'] = '/path/to/image/mypic.jpg'; $config['x_axis'] = '100'; $config['y_axis'] = '60'; $this->image_lib->initialize($config); if ( ! $this->image_lib->crop()) {     echo $this->image_lib->display_errors(); }
281
282Note: Without a visual interface it is difficult to crop images, so this
283function is not very useful unless you intend to build such an
284interface. That's exactly what we did using for the photo gallery module
285in ExpressionEngine, the CMS we develop. We added a JavaScript UI that
286lets the cropping area be selected.
287
288$this->image_lib->rotate()
289===========================
290
291The image rotation function requires that the angle of rotation be set
292via its preference::
293
294 $config['rotation_angle'] = '90';
295
296There are 5 rotation options:
297
298#. 90 - rotates counter-clockwise by 90 degrees.
299#. 180 - rotates counter-clockwise by 180 degrees.
300#. 270 - rotates counter-clockwise by 270 degrees.
301#. hor - flips the image horizontally.
302#. vrt - flips the image vertically.
303
304Here's an example showing how you might rotate an image::
305
306 $config['image_library'] = 'netpbm'; $config['library_path'] = '/usr/bin/'; $config['source_image'] = '/path/to/image/mypic.jpg'; $config['rotation_angle'] = 'hor'; $this->image_lib->initialize($config); if ( ! $this->image_lib->rotate()) {     echo $this->image_lib->display_errors(); }
307
308$this->image_lib->clear()
309==========================
310
311The clear function resets all of the values used when processing an
312image. You will want to call this if you are processing images in a
313loop.
314
315::
316
317 $this->image_lib->clear();
318
319
320******************
321Image Watermarking
322******************
323
324The Watermarking feature requires the GD/GD2 library.
325
326Two Types of Watermarking
327=========================
328
329There are two types of watermarking that you can use:
330
331- **Text**: The watermark message will be generating using text, either
332 with a True Type font that you specify, or using the native text
333 output that the GD library supports. If you use the True Type version
334 your GD installation must be compiled with True Type support (most
335 are, but not all).
336- **Overlay**: The watermark message will be generated by overlaying an
337 image (usually a transparent PNG or GIF) containing your watermark
338 over the source image.
339
340Watermarking an Image
341=====================
342
343Just as with the other functions (resizing, cropping, and rotating) the
344general process for watermarking involves setting the preferences
345corresponding to the action you intend to perform, then calling the
346watermark function. Here is an example::
347
348 $config['source_image'] = '/path/to/image/mypic.jpg'; $config['wm_text'] = 'Copyright 2006 - John Doe'; $config['wm_type'] = 'text'; $config['wm_font_path'] = './system/fonts/texb.ttf'; $config['wm_font_size'] = '16'; $config['wm_font_color'] = 'ffffff'; $config['wm_vrt_alignment'] = 'bottom'; $config['wm_hor_alignment'] = 'center'; $config['wm_padding'] = '20'; $this->image_lib->initialize($config); $this->image_lib->watermark();
349
350The above example will use a 16 pixel True Type font to create the text
351"Copyright 2006 - John Doe". The watermark will be positioned at the
352bottom/center of the image, 20 pixels from the bottom of the image.
353
354.. note:: In order for the image class to be allowed to do any
355 processing, the image file must have "write" file permissions. For
356 example, 777.
357
358Watermarking Preferences
359========================
360
361This table shown the preferences that are available for both types of
362watermarking (text or overlay)
363
364Preference
365Default Value
366Options
367Description
368**wm_type**
369text
370text, overlay
371Sets the type of watermarking that should be used.
372**source_image**
373None
374None
375Sets the source image name/path. The path must be a relative or absolute
376server path, not a URL.
377**dynamic_output**
378FALSE
379TRUE/FALSE (boolean)
380Determines whether the new image file should be written to disk or
381generated dynamically. Note: If you choose the dynamic setting, only one
382image can be shown at a time, and it can't be positioned on the page. It
383simply outputs the raw image dynamically to your browser, along with
384image headers.
385**quality**
38690%
3871 - 100%
388Sets the quality of the image. The higher the quality the larger the
389file size.
390**padding**
391None
392A number
393The amount of padding, set in pixels, that will be applied to the
394watermark to set it away from the edge of your images.
395**wm_vrt_alignment**
396bottom
397top, middle, bottom
398Sets the vertical alignment for the watermark image.
399**wm_hor_alignment**
400center
401left, center, right
402Sets the horizontal alignment for the watermark image.
403**wm_hor_offset**
404None
405None
406You may specify a horizontal offset (in pixels) to apply to the
407watermark position. The offset normally moves the watermark to the
408right, except if you have your alignment set to "right" then your offset
409value will move the watermark toward the left of the image.
410**wm_vrt_offset**
411None
412None
413You may specify a vertical offset (in pixels) to apply to the watermark
414position. The offset normally moves the watermark down, except if you
415have your alignment set to "bottom" then your offset value will move the
416watermark toward the top of the image.
417Text Preferences
418----------------
419
420This table shown the preferences that are available for the text type of
421watermarking.
422
423Preference
424Default Value
425Options
426Description
427**wm_text**
428None
429None
430The text you would like shown as the watermark. Typically this will be a
431copyright notice.
432**wm_font_path**
433None
434None
435The server path to the True Type Font you would like to use. If you do
436not use this option, the native GD font will be used.
437**wm_font_size**
43816
439None
440The size of the text. Note: If you are not using the True Type option
441above, the number is set using a range of 1 - 5. Otherwise, you can use
442any valid pixel size for the font you're using.
443**wm_font_color**
444ffffff
445None
446The font color, specified in hex. Note, you must use the full 6
447character hex value (ie, 993300), rather than the three character
448abbreviated version (ie fff).
449**wm_shadow_color**
450None
451None
452The color of the drop shadow, specified in hex. If you leave this blank
453a drop shadow will not be used. Note, you must use the full 6 character
454hex value (ie, 993300), rather than the three character abbreviated
455version (ie fff).
456**wm_shadow_distance**
4573
458None
459The distance (in pixels) from the font that the drop shadow should
460appear.
461Overlay Preferences
462-------------------
463
464This table shown the preferences that are available for the overlay type
465of watermarking.
466
467Preference
468Default Value
469Options
470Description
471**wm_overlay_path**
472None
473None
474The server path to the image you wish to use as your watermark. Required
475only if you are using the overlay method.
476**wm_opacity**
47750
4781 - 100
479Image opacity. You may specify the opacity (i.e. transparency) of your
480watermark image. This allows the watermark to be faint and not
481completely obscure the details from the original image behind it. A 50%
482opacity is typical.
483**wm_x_transp**
4844
485A number
486If your watermark image is a PNG or GIF image, you may specify a color
487on the image to be "transparent". This setting (along with the next)
488will allow you to specify that color. This works by specifying the "X"
489and "Y" coordinate pixel (measured from the upper left) within the image
490that corresponds to a pixel representative of the color you want to be
491transparent.
492**wm_y_transp**
4934
494A number
495Along with the previous setting, this allows you to specify the
496coordinate to a pixel representative of the color you want to be
497transparent.