Skip to content

Actions Methods

Andrei Coelho edited this page Sep 5, 2019 · 3 revisions

Actions methods

Create more copies for manipulation
Set path where image will be saved
Resize the image
Crop the image
Flip the image
Use any function on any image

copy()

copy(mixed $value)

Description

This function will generate new copies based on the model image. This function accepts only integers greater than 0 or matrix.

By using integers, you are defining how many copies the function should generate. Like this:

EditImage::from('images/zacarias.jpg')->copy(3)->save()

In this example the script is generating four images. The first was generated with from() and the others with copy()

By using arrays, you are defining how many copies the function should generate and yours alias. Like this:

BuildImage::from('images/zacarias.jpg', 'alias0')->copy(['alias1', 'alias2', 'alias3'])->save()

In the first example, you have alias like this: [0,1,2,3]

In the second example, the script is generating four images too. But in this cases, you have alias on strings. Like this:

['alias0', 'alias1', 'alias2', 'alias3']


path()

path(string $path, mixed $alias = false)

Description

This function sets the path, file name and extension to be saved.

When this function is not used, the file will be saved to the same path as the model image.

If you are using an image from a URL, this function is required.

Example

EditImage::from('images/zacarias.jpg')->path('other_path/zac.png')->save()

In this example the path function will save the image to other_path/, file name will be zac and its extension will be .png


resize()

resize(string $size, mixed $alias = false)

Description

This function resize image using string commands.

The commands is simple:

'WxH' where W is a width value and H is a height value. You can use a integer number in this values or symbols.

Integers

Integers will have the value in pixels. Therefore, if you use '300x200', the value will be width: 300px and height: 200px.

Sybols

You have 2 options for symbols: * or _

The symbol * means that the width or height will be proportional to the value (W or H) and the structure of the image.

The symbol _ means that the height or width will be the 100% value of the image.

Example

This image have a 400x543px.

rabbit_0

Using this code:

EditImage::
    from('rabbit.png') 
    ->resize('200x*')  // string commands 
    ->save();

the image will be 200x271px because this is the proportion of the model image. Look:

rabbit_200


crop()

crop(string $values, mixed $alias = false)

Description

This function crop image using string commands.

The commands is this:

'posx posy WxH' where W is a width value and H is a height value. You can use a integer number in this values or symbols.

(See more about symbols in resize())

The posx posy is the position of the marker that will crop the image.

The posx can be a integer or a string (left or center or right) and posy can be a integer or a string (top or center or bottom)

The following table illustrates all possible combinations using string:

left top center top right top
left center center center right center
left bottom center bottom right bottom

When the integer is used in posx it behaves like a margin-left. Equating the integer is used in posy it behaves like a margin-top. See the image below with the code example:

    ->crop('20 10 230x150')  
     //  posx posy  WxH

crop

You can also combine string with numbers. Example:

    ->crop('left 10 230x150')  
     //  posx posy  WxH

Example

crop_2

This code acts on the image as follows:

EditImage::
    from('images/turtle.jpg', 'cropped')
    ->crop('center top 246x139')       
    ->save();        

crop_3

And result will be:

crop_4


flip()

flip(string $flip, mixed $alias = false)

Description

This function flips the image using string commands.

The commands can be 'horizontal', 'vertical' or 'both'.

Example

EditImage::
    from('images/turtle.jpg')
    ->flip('both')       
    ->save();                 

flip


use()

use(string $method, array $vars)

Description

This function will help make the code more cohesive and work with more than one image at a time.

Example

Imagine you have 5 copies, and they all need to be saved in different directories. Could you do this:

EditImage::
    from('images/turtle.jpg', 'img1')
    ->copy(['img2', 'img3', 'img4', 'img5'])
    ->path('path1/turtle_copy.jpg'. 'img1') // path diferent
    ->path('path2/turtle_copy.jpg', 'img2') // path diferent
    ->path('path3/turtle_copy.jpg', 'img3') // path diferent
    ->path('path4/turtle_copy.jpg', 'img4') // path diferent
    ->path('path5/turtle_copy.jpg', 'img5') // path diferent
    ->flip('horizontal')       
    ->save();

Or this:

EditImage::
    from('images/turtle.jpg', 'img1')
    ->copy(['img2', 'img3', 'img4', 'img5'])
    ->use('path', [
        'img1' => 'path1/turtle_copy.jpg',
        'img2' => 'path2/turtle_copy.jpg',
        'img3' => 'path3/turtle_copy.jpg',
        'img4' => 'path4/turtle_copy.jpg',
        'img5' => 'path5/turtle_copy.jpg'
    ])
    ->flip('horizontal')       
    ->save();

Both options have the same performance.