|
| 1 | +## The Image Package |
| 2 | + |
| 3 | +This package comprises of 2 main classes, `JImage` and `JImageFilter` which has 8 filter sub-classes that it can use to apply a desired filter to your image. `JImage` depends on the `GD` php extension to be loaded on your server. More information on `GD` can be found at: http://php.net/manual/en/book.image.php |
| 4 | + |
| 5 | +Manipulating images in raw PHP using the `GD` image* functions requires a lot of boilerplate code. The intent of this package is to handle those requirements and make it simple for developers to accomplish those tasks through easy to use (and remember) methods. |
| 6 | + |
| 7 | +All classes in this package are supported by the auto-loader so can be invoked at any time. |
| 8 | + |
| 9 | +### JImage |
| 10 | + |
| 11 | +#### Construction |
| 12 | + |
| 13 | +When creating a new `JImage` object, the constructor will check that the `gd` extension is loaded, and throw a `RuntimeException` if it is not. |
| 14 | + |
| 15 | +The constructor takes a single optional `$source` parameter. This argument can be one of two things: |
| 16 | + |
| 17 | +- A variable containing an existing, valid image resource created using a `imagecreate*` method. |
| 18 | +- A string containing a valid, absolute path to an image |
| 19 | + |
| 20 | +If you choose the first option, the class sets the protected property `$handle` to the provided image resource. |
| 21 | + |
| 22 | +If you choose the second option, the class will call the `loadFile` method, passing along the `$source` parameter. |
| 23 | + |
| 24 | +```php |
| 25 | +// Creating a new JImage object, passing it an existing handle. |
| 26 | +$resource = imagecreate(100, 100); |
| 27 | +$image = new JImage($resource); |
| 28 | + |
| 29 | +// Creating a new JImage object, passing it an image path |
| 30 | +$image = new JImage(JPATH_SITE . '/media/com_foo/images/uploads/bar.png'); |
| 31 | + |
| 32 | +// Creating a new JImage object then manually calling `loadFile` |
| 33 | +$image = new JImage; |
| 34 | +$image->loadFile(JPATH_SITE . '/media/com_foo/images/uploads/bar.png'); |
| 35 | +``` |
| 36 | + |
| 37 | +#### Usage |
| 38 | + |
| 39 | +##### The `createThumbs` method |
| 40 | +A common usage of the `JImage` class would be to resize uploaded images to thumbnails. Here is some example code for that. |
| 41 | + |
| 42 | +```php |
| 43 | +// Set the desired sizes for our thumbnails. |
| 44 | +$sizes = array('300x300', '64x64', '250x125'); |
| 45 | + |
| 46 | +// Create our object |
| 47 | +$image = new JImage(JPATH_SITE . '/media/com_foo/images/uploads/uploadedImage.jpg'); |
| 48 | + |
| 49 | +// Create the thumbnails |
| 50 | +$image->createThumbs($sizes); |
| 51 | +``` |
| 52 | + |
| 53 | +In this example, we use the `createThumbs` method of `JImage`. This method takes 2 parameters. The first parameter can be a string containing a single size in `WIDTHxHEIGHT` format, or it can be an array of sizes in the format (as shown in the example). The second parameter specifizes the resize method, and is one of the following: |
| 54 | + |
| 55 | +* `JImage::SCALE_FILL` - Gives you a thumbnail of the exact size, stretched or squished to fit the parameters. |
| 56 | +* `JImage::SCALE_INSIDE` - Fits your thumbnail within your given parameters. It will not be any taller or wider than the size passed, whichever is larger. |
| 57 | +* `JImage::SCALE_OUTSIDE` - Fits your thumbnail to the given parameters. It will be as tall or as wide as the size passed, whichever is smaller. |
| 58 | +* `JImage::CROP` - Gives you a thumbnail of the exact size, cropped from the center of the full sized image. |
0 commit comments