Mecha CMS

Add extra functionality to the core engine.

Image

Author: Taufik Nurrohman · 161 Views

Tweet

Image manipulation using GD library.

Download Latest Version

Original 

The original image:

1

Resize 

->resize($max_width = 100, $max_height = null, $proportional = true, $crop = false);

Proportional 

Image::take('photo.jpg')->resize(200, 200)->saveAs('photo-2.jpg');

Result:

2

Stretch 

Image::take('photo.jpg')->resize(200, 200, false)->saveAs('photo-2.jpg');

Result:

3

Crop 

->crop($x = 72, $y = null, $width = null, $height = null);

Crop and Resize 

Simultaneously crop and resize image to obtain the ideal cropping result:

Image::take('photo.jpg')->crop(72, 72)->saveAs('photo-2.jpg');

Result:

4

Crop 

Cropping without resizing image. You need to provide X and Y coordinates before width and height:

/**
 * x: 130
 * y: 50
 * width: 100
 * height: 100
 */

Image::take('photo.jpg')->crop(130, 50, 100, 100)->saveAs('photo-2.jpg');

Result:

5

Color 

Brightness 

->brightness($level = 1);
Image::take('photo.jpg')->brightness(50)->saveAs('photo-2.jpg');

Result:

6

Contrast 

->contrast($level = 1);
Image::take('photo.jpg')->contrast(40)->saveAs('photo-2.jpg');

Result:

7

Colorize 

->colorize($r, $g, $b, $a);
->colorize([$r, $g, $b, $a]);
->colorize($rgba);
->colorize($hex, $a);

Manipulate the color hue of image using RGBA or HEX color code:

Image::take('photo.jpg')->colorize(255, 10, 20, .4)->saveAs('photo-2.jpg');
Image::take('photo.jpg')->colorize([255, 10, 20, .4])->saveAs('photo-2.jpg');
Image::take('photo.jpg')->colorize('rgba(255, 10, 20, .4)')->saveAs('photo-2.jpg');
Image::take('photo.jpg')->colorize('#FF0A14', .4)->saveAs('photo-2.jpg');

Result:

8

Grayscale 

->grayscale();
Image::take('photo.jpg')->grayscale()->saveAs('photo-2.jpg');

Result:

9

Negate 

->negate();
Image::take('photo.jpg')->negate()->saveAs('photo-2.jpg');

Result:

10

Emboss 

->emboss($level = 1);
Image::take('photo.jpg')->emboss(1)->saveAs('photo-2.jpg');

Result:

11

Effect 

Blur 

->blur($level = 1);
Image::take('photo.jpg')->blur(4)->saveAs('photo-2.jpg');

Result:

12

Sharpen 

->sharpen($level = 1);
Image::take('photo.jpg')->sharpen(1)->saveAs('photo-2.jpg');

Result:

13

Pixelate 

->pixelate($level = 1, $advance = false);
Image::take('photo.jpg')->pixelate(2)->saveAs('photo-2.jpg');

Result:

14

Image::take('photo.jpg')->pixelate(2, true)->saveAs('photo-2.jpg');

Result:

15

Flip and Rotate 

Flip 

->flip($direction = 'horizontal');
Image::take('photo.jpg')->flip('horizontal')->saveAs('photo-2.jpg');
Image::take('photo.jpg')->flip('vertical')->saveAs('photo-2.jpg');
Image::take('photo.jpg')->flip('both')->saveAs('photo-2.jpg');

Result for horizontal:

16

Rotate 

->rotate($angle = 0, $bg = false);
->rotate($angle = 0, [$r, $g, $b, $a]);
->rotate($angle = 0, $rgba);
->rotate($angle = 0, $hex, $a);
Image::take('photo.jpg')->rotate(90)->saveAs('photo-2.jpg');

Result:

17

Fill image holes with a solid background color (or transparent and semi-transparent background color for non JPG/JPEG images):

Image::take('photo.png')->rotate(45, [53, 92, 152, 1])->saveAs('photo-2.png');
Image::take('photo.png')->rotate(45, 'rgba(53, 92, 152, .5)')->saveAs('photo-2.png');
Image::take('photo.png')->rotate(45, '#355C98', .5)->saveAs('photo-2.png');

Merge 

->merge($gap = 0, $direction = 'vertical', $bg = false);
->merge($gap = 0, $direction = 'vertical', [$r, $g, $b, $a]);
->merge($gap = 0, $direction = 'vertical', $rgba);
->merge($gap = 0, $direction = 'vertical', $hex, $a);

Combine images into an image:

Image::take([
    'icon-1.png',
    'icon-2.png',
    'icon-3.png',
    'icon-4.png',
    'icon-5.png'
])->merge()->saveAs('sprites.png');

Result:

18

Add distance between images:

Image::take([
    'icon-1.png',
    'icon-2.png',
    'icon-3.png',
    'icon-4.png',
    'icon-5.png'
])->merge(10)->saveAs('sprites.png');

Result:

19

Horizontal image sprites:

Image::take([
    'icon-1.png',
    'icon-2.png',
    'icon-3.png',
    'icon-4.png',
    'icon-5.png'
])->merge(10, 'horizontal')->saveAs('sprites.png');

Result:

20

Fill the gap between images with background color:

Image::take([ … ])->merge(10, 'vertical', [53, 92, 152, 1])->saveAs('sprites.png');
Image::take([ … ])->merge(10, 'vertical', 'rgba(53, 92, 152, .5)')->saveAs('sprites.png');
Image::take([ … ])->merge(10, 'vertical', '#355C98', .5)->saveAs('sprites.png');

File 

Inspect 

Image::inspect($file, $key = null, $fail = false);

Get image info from a file.

$test = Image::inspect('photo.jpg');

Result:

$test
 ├── … ← from `File::inspect()`
 ├── width: 320
 ├── height: 240
 ├── bit: 8
 └── mime: image/jpeg

Optionally to output only single part of the generated data:

echo Image::inspect('photo.jpg', 'mime', 'default value');
echo Image::inspect('photo.jpg')['mime'];

Save 

->saveAs($name = 'image-%{id}%.png');

Change image format. Currently supports for GIF, JPG, JPEG and PNG image extension:

Image::take('photo.png')->saveAs('photo.jpg');

Save image to another place:

Image::take('photo.png')->saveTo('path/to/photo-2.jpg');
Image::take('photo.png')->saveTo('path/to/directory');

Draw 

->draw($save = false);

Output the manipulated image into browser.

What I mean as “output” here is not the same as outputting an image with the HTML::img() method. Here, images will be outputted as the image itself and not as the part of the HTML markup. For example, if you have a PHP file named as image.php with contents like this:

<?php Image::take('photo.jpg')->resize(200, 200)->draw(); ?>

Then you will be able to use that PHP file in the HTML image like this:

<img alt="test" src="image.php">

The code below can give you more ideas:

$w = Request::get('width');
$h = Request::get('height');
Image::take('photo.jpg')->resize($w, $h)->draw();
<img alt="test" src="image.php?width=200&amp;height=200">

You could also output and save the generated image at the same time by defining an image path to it. Useful for generating image cache:

// Draw and save image output to `path/to/directory` as `saved-image.png`
Image::take('path/to/image.png')->resize(100, 100)->draw('path/to/directory/saved-image.png');

0 Comments

No comments yet.