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