PluginProbe
Sharing Image / 2.0.13
Sharing Image v2.0.13
3.10 trunk 2.0 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0 3.1 3.2 3.3 All 29 releases
sharing-image / vendor / antonlukin / poster-editor / README.md

README.md in Sharing Image 2.0.13, at vendor/antonlukin/poster-editor/README.md

360 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 # Poster Editor
2
3 This class is an alternative to the package [](https://github.com/Intervention/imageIntervention Image](https://github.com/Intervention/image](https://github.com/Intervention/image) for more flexible work with text on the image.
4 Use it if you need to **fit text** in a given area, automatically **calculate font size** and change **line height**.
5 The text drawing method also knows how to return the actual size of the inscriptions, which will allow, for example, to place blocks under each other.
6
7 In addition to flexible work with text, the class provides an Image API similar to the Intervention package.
8 Including smart poster resizing, filters, drawing shapes, and overlaying other images.
9
10 If you are not using composer in your work, you may also be interested in the simplicity of the class and the absence of any dependencies.
11 Note that this class supports PHP-GD driver only.
12 You can also easily inherit your class — all methods of the parent can be overridden.
13
14 ## Installation
15 The best way to install Poster Editor is quickly and easily with [](http://getcomposer.org/Composer](http://getcomposer.org/](http://getcomposer.org/).
16 However, you can require the class directly without using a loader - this will not affect performance in any way.
17
18 `php composer.phar require antonlukin/poster-editor`
19
20 ## Usage
21 In case of an error, the class methods return an exception.
22 Therefore, it is best to call them inside a block `try..catch`.
23
24 #### Example
25 ```php
26 // Using composer
27 require_once __DIR__ . '/vendor/autoload.php';
28
29 // Or directly
30 // require_once __DIR__ . '/PosterEditor.php';
31
32 try {
33 $image = new PosterEditor\PosterEditor();
34 $image->make('images/bridge.jpg')->fit(600, 600);
35 $image->show();
36
37 } catch(Exception $e) {
38 echo $e->getMessage();
39 }
40 ```
41
42 ## Availible methods
43 ### get
44 ```
45 $image->get()
46 ```
47 Get image resource to use raw gd commands.
48
49 ### set
50 ```
51 $image->set(instance $resourse)
52 ```
53 Set image resource after using raw gd commands.
54
55 ### make
56 ```
57 $image->make(mixed $data)
58 ```
59 Make new image instance from file or binary data.
60
61 ### insert
62 ```
63 $image->insert(mixed $data, array $options = array())
64 ```
65 Paste a given image source over the current image with an optional position.
66 First param can be binary data or path to file or another class instance.
67 Options is an array of x/y relative offset coords from top left corner. By default image will be centered.
68
69 ### canvas
70 ```
71 $image->canvas(int $width, int $height, array $options = array())
72 ```
73 Intialise the canvas by width and height. Background color options is in array of color/opacity settings.
74 By default black canvas without opacity.
75
76 ### show
77 ```
78 $image->show(string $format = null, int $quality = 90)
79 ```
80 Sends HTTP response with current image in given format and quality.
81 Format is a file image extension. By default the response data will be encoded in the type of the current image. If no image type is defined yet, method will return jpeg encoded data.
82 Quality is normalized for all file types to a range from 0 (poor quality, small file) to 100 (best quality, big file).
83 The default value is 90. PNG format is losless and the quality affects only image size and compression speed.
84
85 ### save
86 ```
87 $image->save(string $path, int $quality = 90, string $format = null)
88 ```
89 Save the current state of the image object in filesystem. Define optionally a certain path where the image should be saved. The image type will be defined by file extension. If there is no extension available, the response data will be encoded in the type of the current image. If no image type is defined yet, method will return jpeg encoded data. Optionally you can override this with the format parameter.
90 Quality is normalized for all file types to a range from 0 (poor quality, small file) to 100 (best quality, big file).
91 The default value is 90. PNG format is losless and the quality affects only image size and compression speed.
92
93 ### destroy
94 ```
95 $image->destroy()
96 ```
97 Frees memory associated with the current image instance before the PHP script ends.
98 Normally resources are destroyed automatically after the script is finished.
99
100 ### width
101 ```
102 $image->width()
103 ```
104 Returns the height in pixels of the current image.
105
106 ### height
107 ```
108 $image->height()
109 ```
110 Returns the height in pixels of the current image.
111
112 ### resize
113 ```
114 $image->resize(int $width, int $height)
115 ```
116 Resizes current image based on given width and height.
117 Does not pay attention to the aspect ratio. In order to resize proportionally, use `upsize` and `downsize` methods.
118
119 ### upsize
120 ```
121 $image->upsize(int $width = null, int $height = null)
122 ```
123 Upsize image on the largest side. Calculated by ratio for nulled width or height.
124
125 ### downsize
126 ```
127 $image->downsize(int $width = null, int $height = null)
128 ```
129 Downsize image on the largest side. Calculated by ratio for nulled width or height.
130
131 ### crop
132 ```
133 $image->crop(int $width, int $height, array $options = array())
134 ```
135 Cut out a rectangular part of the current image with given width and height.
136 Define optional x,y coordinates to move the top-left corner of the cutout to a certain position.
137
138 ### fit
139 ```
140 $image->fit(int $width, int $height, string $position = 'center')
141 ```
142 Combine cropping and resizing to format image in a smart way.
143 The method will find the best fitting aspect ratio on the current image automatically,
144 cut it out and resize it to the given dimension.
145 Possible positions `top-left`, `top`, `top-right`, `bottom-left`, `bottom`, `bottom-right`, `right`, `left` and `center` by default.
146
147 ### line
148 ```
149 $image->line(int $x1, int $y1, int $x2, int $y2, array $options = array())
150 ```
151 Draw a line from x,y point 1 to x,y point 2 on current image. Use options param to set color, opacity and width values.
152
153 ### rectangle
154 ```
155 $image->rectangle(int $x, int $y, int $width, int $height, array $options = array())
156 ```
157 Draw a colored rectangle on current image. Use options param to set color, opacity, thickness and outline values.
158
159 ### ellipse
160 ```
161 $image->ellipse(int $x, int $y, int $width, int $height, array $options = array())
162 ```
163 Draw an ellipse. Use options param to set color, opacity and outline values.
164
165 ### brightness
166 ```
167 $image->brightness(int $level = 0)
168 ```
169 Change the brightness of the current image by the given level.
170 Use values between -100 for min brightness 0 for no change and +100 for max.
171
172 ### contrast
173 ```
174 $image->contrast(int $level = 0)
175 ```
176 Change the contrast of the current image by the given level.
177 Use values between -100 for min contrast 0 for no change and +100 for max.
178
179 ### grayscale
180 ```
181 $image->grayscale()
182 ```
183 Turn an image into a grayscale version.
184
185 ### blur
186 ```
187 $image->blur()
188 ```
189 Apply a blur image effect.
190
191 ### invert
192 ```
193 $image->invert()
194 ```
195 Invert colors of an image.
196
197 ### blackout
198 ```
199 $image->blackout()
200 ```
201 Draw black opactity rectangle on image.
202
203 ### rotate
204 ```
205 $image->rotate(int $angle, array $options = array())
206 ```
207 Rotate image. Set an angle in degrees and optional color of the uncovered zone after the rotation.
208
209 ### text
210 ```
211 $image->text(string $text, array $options = array(), array &$boundary = array())
212 ```
213 Draw text on image. Look for all possible options in the examples below.
214
215 ## Examples
216
217 ### Append downsized image to blackout background
218 ```php
219 $image = new PosterEditor\PosterEditor();
220 $image->make('images/bridge.jpg')->fit(1200, 630, 'bottom')->blackout(50);
221
222 $logo = new PosterEditor\PosterEditor();
223 $logo->make('images/logo.png')->downsize(150, null);
224
225 $image->insert($logo, array('x' => 50, 'y' => 50))->show();
226 ```
227 #### Result
228 <img src="example/samples/append.jpg" alt="append image" width="400" />
229
230 ### Draw and center multiline text
231 ```php
232 $image = new PosterEditor\PosterEditor();
233 $image->make('images/bridge.jpg')->fit(1200, 630);
234 $image->grayscale()->brightness(-40);
235
236 $image->text(
237 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat Lorem ipsum dolor sit amet', // phpcs:ignore
238 array(
239 'x' => 100,
240 'y' => 100,
241 'width' => 1000, // Use image width if null or unset
242 'height' => 400, // Use image height if null or unset
243 'horizontal' => 'center', // Can be left/right/center
244 'vertical' => 'center', // Can be top/center/bottom
245 'fontpath' => 'fonts/opensans.ttf', // Can only be .ttf
246 'fontsize' => 24,
247 'lineheight' => 1.75,
248 'color' => '#ffffff',
249 'opacity' => 1,
250 'debug' => true,
251 )
252 );
253
254 $image->show('jpg', 70);
255 ```
256 #### Result
257 <img src="example/samples/center.jpg" alt="multiline centered text" width="400" />
258
259 ### Use boundaries to draw one title right after another
260 ```php
261 $image = new PosterEditor\PosterEditor();
262 $image->make('images/bridge.jpg')->crop(
263 900, 600,
264 array(
265 'x' => '0',
266 'y' => '100'
267 )
268 );
269
270 $image->grayscale()->brightness(-40);
271
272 $image->text(
273 'Large title with unknown height. Can be multi-line',
274 array(
275 'x' => 50,
276 'y' => 100,
277 'width' => 800,
278 'fontpath' => '/fonts/merriweather.ttf',
279 'fontsize' => 48,
280 'lineheight' => 1.5,
281 'color' => '#9999ff',
282 ),
283 $boundary
284 );
285
286 $image->text(
287 'This text appears right after title using smart boundaries',
288 array(
289 'x' => 50,
290 'y' => 100 + $boundary['height'],
291 'width' => 800,
292 'fontpath' => '/fonts/opensans.ttf',
293 'fontsize' => 20,
294 'lineheight' => 1.5,
295 'color' => '#ff9999',
296 ),
297 $boundary
298 );
299
300 $image->show();
301 ```
302 #### Result
303 <img src="example/samples/boundary.jpg" alt="boundary" width="400" />
304
305 ### Draw shapes
306 ```php
307 $image = new PosterEditor\PosterEditor();
308 $image->make('images/bridge.jpg')->fit(1000, 630, 'bottom');
309 $image->contrast(5)->brightness(-30)->blur();
310
311 $image->rectangle(
312 20, 20, 960, 590,
313 array(
314 'color' => '#ffffff',
315 'outline' => true,
316 'width' => 4,
317 )
318 );
319
320 $image->ellipse(
321 200, 200, 200, 200,
322 array(
323 'color' => '#00ff00',
324 'opacity' => 50,
325 )
326 );
327
328 $image->ellipse(
329 800, 200, 200, 200,
330 array(
331 'color' => '#ff0000',
332 'opacity' => 50,
333 )
334 );
335
336 $image->rectangle(
337 480, 280, 80, 140,
338 array(
339 'color' => '#0000ff',
340 )
341 );
342
343 $image->line(
344 200, 500, 800, 500,
345 array(
346 'color' => array(255, 255, 0),
347 'opacity' => 10,
348 'width' => 4,
349 )
350 );
351
352 $image->show('png');
353 ```
354 #### Result
355 <img src="example/samples/shapes.png" alt="shapes result" width="400" />
356
357
358 ## Contribution
359 All project code is stored on Github. The best way to help the project is to report a bug or add some new functionality.
360 You can also open here an [](https://github.com/antonlukin/poster-editor/issuesissue](https://github.com/antonlukin/poster-editor/issues](https://github.com/antonlukin/poster-editor/issues) or send a [](https://github.com/antonlukin/poster-editor/pullspull reguest](https://github.com/antonlukin/poster-editor/pulls](https://github.com/antonlukin/poster-editor/pulls).