-
Notifications
You must be signed in to change notification settings - Fork 2
/
README
265 lines (162 loc) · 8.18 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Image manipulation made easy - sfImageTransformPlugin #
## Introduction ##
The aim of sfImageTransformPlugin is take the pain out of image manipulating in PHP/symfony. sfImageTransformPlugin is great for but not limited to common tasks like creating thumbnails, adding text to dynamic images or watermarking.
sfImageTransformPlugin works by applying one or more "transform" to the image. A transform maybe a simple action like resize or thumnnail, mirror or more complex like an overlay (watermarks) and pixelize.
Multiple tranforms can be easily applied by chaining the transform calls as seen below. It is also very easy to extend and create your own transforms, see "Writing your own transforms" for an example.
*Example 1. Simple chaining of transforms*
Load an image, resize it to 100 pixels wide (preserving the aspect ratio) and add some text
$img = new sfImage('image1.jpg', 'image/jpg');
$img->resize(100,null)->text('Writing text on an image is easy!!', 10, 10, 20,'Arial', '#FF0000');
$img->save();
Installation
------------
To install the plugin for a symfony project, the usual process is to use the symfony command line:
symfony plugin:install sfImageTransformPlugin
for symfony 1.0
symfony plugin-install http://plugins.symfony-project.com/sfImageTransformPlugin
Alternatively, if you don't have PEAR installed, you can download the latest package attached to this plugin's wiki page and extract it under your project's plugins/ directory.
Activate the plugin in your ProjectConfiguration.class.php.
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->enablePlugins(..., 'sfImageTransformPlugin', ...);
Clear the cache to enable the autoloading to find the new classes:
php symfony cc
Note: The plugin requires either GD or ImageMagick graphics libraries to be installed on the server.
## Using sfImageTransformPlugin ##
### Geting started ###
*Example 2. Chaining transforms*
The simplest way to use sfImageTransform is to use method chaining. In an action:
$img = new sfImage('image1.jpg', 'image/jpg');
$response = $this->getResponse();
$response->setContentType($img->getMIMEType());
$img->resize(1000,null)->overlay(new sfImage('logo.png'), 'bottom-right')
$response->setContent($img);
return sfView::NONE;
### Thumbnailing ###
sfImageTransformPlugin supports several different types of thumbnailing:
* fit - Creates an image of the specified size and fits the thumbnailed inside. This will produce uniform size thumbnails. (default)
* scale - Scales the image to fit inside the specified dimensions.
* inflate, deflate - simply resizes the image to the specified dimensions, proportions are not preserved.
* left, right, top, bottom, center, bottom-left, bottom-right, top-left, top-right - scales the image to specified size and then crops
*Example 3. Thumbnailing*
Create a 150x150 pixel thumbnail
$img = new sfImage('image1.jpg', 'image/jpg');
$img->thumbnail(150,150);
$img->setQuality(50);
$img->saveAs('image1_150x150.gif');
*Example 4. Watermarking*
Overlaying another image such as a logo or watermark is easy.
Create a 150x150 pixel thumbnail
$img = new sfImage('image1.jpg'); // using MIME detection
$img->overlay(new sfImage('logo.png'), 'top-left'); // or you can use coords array($x,$y)
$img->saveAs('image1_watermarked.jpg');
*Example 5. Standalone, useful for bulk use on many images*
$img = new sfImage('image1.jpg', 'image/jpg', 'ImageMagick');
$scale = new sfImageScaleImageMagick(0.5);
$img = $scale->execute($img);
$img->saveAs('image2.gif');
Note: If you install one of the supported MIME detection libraries you do not have to pass in the image's MIME type. See the section "Enabling MIME detection" for details.
## Included tranforms ##
sfImageTransform comes bundled with many transforms, which are listed below. The plugin has full phpdoc style API documentation, including all the available transforms. This can be generated using [phpDocumentor](http://www.phpdoc.org/)
Once phpDocumentor is installed you can generate the docs from inside the plugin's directory as follows,
phpdoc -o HTML:frames:DOM/phphtmllib -d . -t docs
Generic
* border
* callback
* resize
* thumbnail
GD
* alphaMask, arc
* brightness
* colorize, contrast, crop
* edgeDetect, ellipse, emboss
* fill, flip
* gamma, gaussianBlur, greyscale
* line
* mirror
* negate, noise
* opacity, overlay
* pixelBlur, pixelize
* rectangle, rotate, roundedCorners
* scale, scatter, selectiveBlur, sketchy, smooth
* text, transparency
ImageMagick
* brightness
* colorize, crop
* fill, flip
* greyscale
* line
* mirror
* opacity, overlay
* prettyThumbnail
* rectangle, rotate
* scale
* text, trim
## Writing your own transforms ##
sfImageTransformPlugin is designed to be easily extended. To make a new transform you simple create a class that extends the abstract class (sfImageTransformAbstract) and implements the transform method.
Transforms are written specifically for the image library you want to use or generically if they don't use image library specific calls (see the thumbnail transform).
The naming convention for a transform is important. For generic transforms the class should be named sfImage#transform name#Generic and for graphic library specific transforms, sfImage#transform name##image library#.class.php and class names should be sfImage#transform name##image library#
*Example 6. Creating a new GD transform, "Example"*
sfImageExampleGD.class.php
class sfImageExampleGD extends sfImageTransformAbstract
{
// Parameters can be passed in the standard way
public funnction __construct($arg1, $arg2)
{
...
}
public function execute(sfImage $image)
{
// Get the actual image resource
$resource = $image->getAdapter()->getHolder();
// Manipulate image using the GD functions
...
// To set a new resource for the image object
$image->getAdapter()->setHolder($dest_resource);
}
}
Note: Don't forget to symfony cc so the new class is found!
action.class.php
$img = new sfImage('image1.jpg', 'image/jpg');
$img->resize(1000,null)->example($arg1, $arg2);
$response = $this->getResponse();
// Output the right content type
$response->setContentType($img->getMIMEType());
$response->setContent($img);
return sfView::NONE;
## Configuration ##
### Overriding default settings ###
You can override the default settings used by redefining the plugin settings in your project
app.yml
all:
sfImageTransformPlugin:
default_adapter: GD # GD or ImageMagick
default_image:
mime_type: image/png
filename: Untitled.png
width: 100
height: 100
color: '#FFFFFF'
font_dir: /usr/share/fonts/truetype/msttcorefonts
mime_type:
auto_detect: false
library: gd_mime_type # gd_mime_type (GD), Fileinfo (PECL), MIME_Type (PEAR)
### Enabling MIME detection ###
sfImageTransformPlugin currently supports three MIME type detection methods, GD's getimagesize, PECL's Fileinfo and PEAR's MIME_Type. These can be installed via PECL and PEAR respectively.
To enable support in the plugin set auto_detect to true and select the library you have installed.
app.yml
sfImageTransformPlugin:
mime_type:
auto_detect: true
library: gd_mime_type # Fileinfo (PECL), MIME_Type (PEAR), gd_mime_type (GD)
With MIME detection enabled there is no need to specific the MIME types in the method calls.
*Example. Add a watermark to the uploaded image "file"*
$filename = $this->getRequest()->getFileName('file');
$img = new sfImage($filename);
$response = $this->getResponse();
$response->setContentType($img->getMIMEType());
$img->resize(150,null)->overlay(new sfImage('watermark.png'));
$img->saveAs(sfConfig::get('sf_upload_dir'). DIRECTORY_SEPARATOR . 'watermarked' . DIRECTORY_SEPARATOR . $filename);
return sfView::NONE;