CodeDmx has a class to manipulate images in PHP as simple as possible.
With this class, we can do:
$this->image('/path/to/the/image.jpg');
$this->imageclass->best_firt(300, 250)->save('/path/to/new/image.jpg');
In the first line we try to load the image.
In the second line we shrink it to fit within a 300x250 box, and save it to another path.
You can load an image when you need to use it:
$this->image('/path/to/image.jpg');
// The you can use its methods with $this->imageclass();
// Or you can create an empty image 300x250 with white background
$this->image(null, 200, 100, '#fff');
It's necessary to save the image after you manipulate it.
// To save your changes and replace the original file
$this->imageclass->save();
// Or you can specify a new filename
$this->imageclass->save('new-image.jpg');
// Also, you can specify quality as a second parameter in percents within range 0 - 100
$this->imageclass->save('new-image.jpg', 90);
When you try to save it, you can determined by the file extension.
$this->image('/path/to/image.jpg');
$this->imageclass->save('/path/to/image.gif');
This class supports method chaining, so you can make multiple changes and save the resulting image with just one line of code:
$this->image('/path/to/image.jpg');
$this->imageclass->flip('x')->rotate(90)->best_fit(300, 250)->desaturate()->invert()->save('/path/to/new-image.jpg');
// You can chain all of the methods below as well methods above.
// First of all, we call the method to get the class
$this->image('/path/to/image.jpg');
// Flip the image horizontally (use 'y' to flip vertically)
$this->imageclass->flip('x');
// Rotate the image 90 degrees clockwise
$this->imageclass->rotate(90);
// Adjust the orientation if needed
$this->imageclass->auto_orient();
// Resize the image to 300x250
$this->imageclass->resize(300, 250);
// Trim the image and resize to exactly 100x75
$this->imageclass->thumbnail(100, 75);
// Trim the image and resite to exactly 100x75, keeping the top of the image
$this->imageclass->thumbnail(100, 75, 'top');
// Shrink the image to the specified width while maintaining proportion (width)
$this->imageclass->fit_to_width(300);
// Shrink the image to the specified height while maintaining proportion (height)
$this->imageclass->fit_to_height(250);
// Shrink the image proportionally to fit inside to 500x500 boz
$this->imageclass->best_fit(500, 500);
// Crop a portion of the image from x1, y1 to x2, y2
$this->imageclass->crop(100, 100, 400, 400);
// Fill image with white color
$this->imageclass->fill('#fff');
// Desaturate (grayscale)
$this->imageclass->desaturate();
// Invert
$this->imageclass->invert();
// Adjust Brightness (-255 to 255)
$this->imageclass->brightness(100);
// Adjust Contrast (-100 to 100)
$this->imageclass->contrast(50);
// Colorize red at 50% opacity
$this->imageclass->colorize('#FF0000', .5);
// Edges filter
$this->imageclass->edges();
// Emboss filter
$this->imageclass->emboss();
// Mean removal filter
$this->imageclass->mean_remove();
// Selective blur (one pass)
$this->imageclass->blur();
// Gaussian blur (two passes)
$this->imageclass->blur('guassian', 2);
// Sketch filter
$this->imageclass->sketch();
// Smooth filter (-10 to 10)
$this->imageclass->smooth(5);
// Pixelate using 8px blocks
$this->imageclass->pixelate(8);
// Sepia effect (simulated)
$this->imageclass->sepia();
// Change opacity
$this->imageclass->opacity(.5);
// Overlay watermark.png at 50% opacity at the bottom-right of the image with 10 pixel horizontal and vertical margin
$this->imageclass->overlay('watermark.png', 'bottom right', .5, -10, -10);
// Add 32-point white text top-centered (plus 20px) on the image
$this->imageclass->text('Test text', 'font.ttf', 32, '#fff', 'top', 0, 20);
// Add multiple colored text
$this->imageclass->text('Test text', 'font.ttf', 32, ['#fff', '#000'], 'top', 0, 20);
Valid positions are
The following methods are not chainable, 'case they return information about the image you're working with or output the image directly to the browser:
// First of all, we call the method to get the class
$this->image('/path/to/image.jpg');
/*
Returns: array(
width => 300,
height => 250,
orientation => ['portrait', 'landscape', 'square'],
exif => array(...),
mime => ['image/jpeg', 'image/gif', 'image/png'],
format => ['jpeg', 'gif', 'png']
)*/
$info = $this->imageclass->get_original_info();
// Get the current width
$width = $this->imageclass->get_width();
// Get the current height
$height = $this->imageclass->get_height();
// Get the current orientation
$orientation = $this->imageclass->get_orientation();
// Flip the image and output it directly to the browser (without saving to file)
$this->imageclass->flip('x')->output();