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