Skip to content

Commit af1937c

Browse files
author
jools
committed
Merge commit 'b7b41bbdaf7fc66edebb4b06fcee2a9354d88675' into HEAD
2 parents c2aaed5 + 3251185 commit af1937c

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
### Construction
10+
11+
When creating a new `JImage` object, the constructor will check that the `gd` extension is loaded, and throw a `RuntimeException` if it is not.
12+
13+
The constructor takes a single optional `$source` parameter. This argument can be one of two things:
14+
15+
- A variable containing an existing, valid image resource created using a `imagecreate*` method.
16+
- A string containing a valid, absolute path to an image
17+
18+
If you choose the first option, the class sets the protected property `$handle` to the provided image resource.
19+
20+
If you choose the second option, the class will call the `loadFile` method, passing along the `$source` parameter.
21+
22+
```php
23+
// Creating a new JImage object, passing it an existing handle.
24+
$resource = imagecreate(100, 100);
25+
$image = new JImage($resource);
26+
27+
// Creating a new JImage object, passing it an image path
28+
$image = new JImage(JPATH_SITE . '/media/com_foo/images/uploads/bar.png');
29+
30+
// Creating a new JImage object then manually calling `loadFile`
31+
$image = new JImage;
32+
$image->loadFile(JPATH_SITE . '/media/com_foo/images/uploads/bar.png');
33+
```
34+
35+
### Usage
36+
37+
Keep in mind that most public methods return a `JImage` instance with a valid image handle for easy method chaining. The examples for each method will break each method call out to be able to comment on what the code is doing, but production code can be chained like so (if you prefer):
38+
39+
```php
40+
$image = new JImage();
41+
$image->loadFile(JPATH_SITE . '/path/to/image.png')->crop(600, 250)->toFile(JPATH_SITE . '/tmp/image.png');
42+
```
43+
44+
Since Platform version 12.3, there is a new `destroy()` method that get's called in appropriate places throughout the class which runs the `imagedestroy` function to free memory associated with an image handle. This method is called before each time an image handle is replaced (when `$createNew` is set to false) as well as in the class `__descruct` method as a final cleanup.
45+
46+
47+
#### The `resize` method
48+
__Accepted Parameters__
49+
* `$width`: The width of the resized image in pixels or a percentage.
50+
* `$height`: The height of the resized image in pixels or a percentage.
51+
* `$createNew`: If true the current image will be cloned, resized and returned; else the current image will be resized and returned.
52+
* `$scaleMethod`: Which method to use for scaling
53+
54+
Example: Using `JImage::resize()` to generate a resized image.
55+
56+
```php
57+
// Create our image object
58+
$image = new JImage(JPATH_SITE . '/media/com_foo/images/uploads/bar.png');
59+
60+
// Resize the image using the SCALE_INSIDE method
61+
$image->resize(300, 150, true, JImage::SCALE_INSIDE);
62+
63+
// Write it to disk
64+
$image->toFile(JPATH_SITE . '/tmp/bar_resized.png');
65+
66+
```
67+
68+
69+
#### The `crop` method
70+
__Accepted Parameters__
71+
* `$width`: The width of the image section to crop in pixels or a percentage.
72+
* `$height`: The height of the image section to crop in pixels or a percentage.
73+
* `$left`: The number of pixels from the left to start cropping.
74+
* `$top`: The number of pixels from the top to start cropping.
75+
* `$createNew`: If true the current image will be cloned, cropped and returned; else the current image will be cropped and returned.
76+
77+
Example: Using `JImage::crop()` to generate a cropped image.
78+
79+
```php
80+
// Create our image object
81+
$image = new JImage(JPATH_SITE . '/media/com_foo/images/uploads/bar.png');
82+
83+
// Crop the image to 150px square, starting 10 pixels from the left, and 20 pixels from the top
84+
$image->crop(150, null, 10, 20);
85+
86+
// Write it to disk
87+
$image->toFile(JPATH_SITE . '/tmp/bar_cropped.png');
88+
```
89+
90+
91+
#### The `createThumbs` method
92+
__Accepted Parameters__
93+
* `$thumbsizes`: String or array of strings. Example: $thumbSizes = array('150x75','250x150');
94+
* `$creationMethod`: See __Resize Methods__ below.
95+
* `$thumbsFolder`: Destination for thumbnails. Passing null generates a thumbs folder in the loaded image's containing folder.
96+
97+
Example: Using `JImage::createThumbs()` to generate thumbnails of an image.
98+
99+
```php
100+
// Set the desired sizes for our thumbnails.
101+
$sizes = array('300x300', '64x64', '250x125');
102+
103+
// Create our object
104+
$image = new JImage(JPATH_SITE . '/media/com_foo/images/uploads/uploadedImage.jpg');
105+
106+
// Create the thumbnails
107+
$image->createThumbs($sizes, JImage::SCALE_INSIDE);
108+
```
109+
110+
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. (See Resize Methods below)
111+
112+
113+
#### Resize Methods
114+
115+
The `resize`, `createThumbs` and `generateThumbs` methods take an optional parameter that defines what method to use when scaling an image.
116+
This parameter can be one of the following:
117+
118+
* `JImage::SCALE_FILL` - Gives you a thumbnail of the exact size, stretched or squished to fit the parameters.
119+
* `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.
120+
* `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.
121+
* `JImage::CROP` - Gives you a thumbnail of the exact size, cropped from the center of the full sized image.

manual/en-US/menu.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [JGithub](chapters/packages/github.md)
77
- [JGoogle](chapters/packages/google.md)
88
- [JHttp](chapters/packages/http.md)
9+
- [JImage](chapters/packages/image.md)
910
- [JInput](chapters/packages/input.md)
1011
- [JKeychain](chapters/packages/keychain.md)
1112
- [JLog](chapters/packages/log.md)

0 commit comments

Comments
 (0)