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