Download

Download from GitHub.

EasyImage on GitHub


Info

Easy image is an easy to use image manipulation library written entirely in PHP and implemented in a single PHP class. It's intended to be used where ImageMagick is not available, and works on PHP 5.3 and up. It can be used to edit, apply filters, resize, animate, convert and get information about images in several formats.


Input types:

  • PNG file
  • GIF file
  • JPG file
  • PSD file
  • Animated Gif

                                           

as

  • Base 64 string
  • Image URL
  • HTML
  • Local File

                                           

Output types:

  • PNG
  • GIF
  • JPG
  • Animated Gif
  • PDF

                                           

as

Edit & Adjust Image:





Get Image Info:


Utilities & Helpers:


Usage

EasyImage was meant to be easy to use. To that end, most of it's methods are chainable, and the image can be sent to the browser by simply echoing the class. This allows you to generate and alter images in a single statement.


EasyImage::Create()

EasyImage has a universal constructor: EasyImage::Create(). This method is used to create your EasyImage object no matter what the input type.


Create from Local File

EasyImage::Create( $filepath [, $isTemp=false ] )
  • $filepath - string A path to an image on the filesystem
  • $isTemp - boolean (optional) If set to true, will delete the source image when the script completes
$path = realpath(dirname(__FILE__));
echo EasyImage::Create("$path/image.png");

Create from URL

EasyImage::Create( $url )
  • $url - string A URL for an image on the web
echo EasyImage::Create("www.example.com/images/logo.jpg");

Create blank image

EasyImage::Create( $width, $height [, $background=null ] )
  • $width - string Width of the image
  • $height - string Height of the image
  • $background - string|array A hex color or an array of hex colors to form a gradient
echo EasyImage::Create(50, 50);

Create from Base64 String

EasyImage::Create( $b64 )
  • $b64 - string A Base64 encoded image
echo EasyImage::Create("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACg0lEQVR4nO3XTWhUVxQH8F+ipopgCVqTuFFLtQstwS+IChpXrbrr2iJ+rQviwi666aIFQRAFN7pzIyIBUXChKFW0UMGFdqWgLTRG0eBHaltjYro49zmTl3mZNxOhC/3DcGfOvef7vf+5wwe872hpUm8eVqITH+MJBnADL99NaBMxDTtwDaMYq/H5F+fxVVmjZSvQg+NYhmFcwSX045moyCJ8iTXJ7iXsxB9lgynCNyKzYRzBJ3XOL8VpUZFHWDcV59uSoYdNGNqGf/A3VjfjvEdk/hCLmzGAXlG5fnQ1otiKW0l5bZPOM+wRVTzRiNL2pHR4is4zXMUbdJdV+FlkP79KNhv7RW+L0Ia9IuvqN2yTSOhQGefteI2LOfmPKu/7hgLdvVVnvq6St+Ix7uYVWmsY6cZ0XM7Jh9I6hr8KAhgq+P5GVPUzwZxvMb2GkY609ufkB3AfD3CzIIDjGEwBXsjtZfY68HyyAOak9UVOPoqTBY4zjKGvYC9zOq4CtVowmNa5dZw1ioxBn9QLYCCtzZJPERapsOqk+Ej0sFafl2BhHf0VJlZvlqDkX+s5z9Anol2ek98Q43hmgV6XqOCxnDybKd+XDWBjUjibk68U8+EX4wdMC7aK0dtvfAXacE9UtbNsAHAuBbE7J+/FU5V+3q76fQef5s4fTXs/NOIcFohshrE5t9cuaPmMqMYp7BLZVmNfcn5dPFsNY5VgtBF8q/wNqk0l83sq5NYUvhAMOCYewi2YUXB2tpikv6fz14wfaBNQNqN5+ElcSqcJlryKPwXDZXfC9Sqv8cGk86qkj1L4PBn9zcQb8ajo9XcaKHmz/wsILugQ3D4oLqAjU7D3Af8P/gN1UJotAMKqGQAAAABJRU5ErkJggg==");

Create from Text

EasyImage::Create( $text, $font_size, $font_color, $font_file, [, $background=null [, $wrap_width=false [, $alpha=EasyImage::TEXT_ALIGN_LEFT [, $padding=5 [, $align=0 ] ] ] ] ] )
  • $text - string A string of text to be drawn as an image
  • $font_size - int The size of the font
  • $font_color - mixed An image or color to be overlayed over the font
  • $font_file - string Path to a trueType font file
  • $background - mixed (optional) An image or hex color to be used as the background
  • $wrap_width - int (optional) How wide to allow text to get before wrapping
  • $alpha - int (optional) Transparency level (0-127)
  • $padding - int (optional) padding around the text
  • $align - int (optional) Text alignment. May be any of the following
    • EasyImage::TEXT_ALIGN_LEFT - Left align text (default)
    • EasyImage::TEXT_ALIGN_RIGHT - Right align text
    • EasyImage::TEXT_ALIGN_CENTER - Center align text
    • EasyImage::TEXT_ALIGN_JUSTIFY - Justify align text
echo EasyImage::Create(
    "Hello, World",
    13,
    "#FF0000",
    "fancyFont.ttf"
);

Create from HTML

EasyImage::Create( $HTML, $fonts )
  • $HTML - string HTML string
  • $fonts - array An array of fonts where the keys are font title and the values are the path to the file
$fonts = array("courier"=>"courier.ttf", "impact"=>"impact.ttf");
$HTML = "<span style='font-size:15;'>Hello, </span><span style='font-face:impact'>world</span>";
echo EasyImage::Create($HTLM, fonts);

Create Animated Gif

EasyImage::Create( $sources [, $delayTimes=5 [, $loops=0 [, $disposal=2 [, $transRed=0 [, $transGreen=0 [, $transBlue=0 ] ] ] ] ] ] )
  • $sources - array An array of images to use as frames. Can be image paths, URLs, base64 strings, EasyImage objects or any combination
  • $delayTimes - array|int (optional) Either an array of delay times for each frame, or a single delay time that will be applied to all frames
  • $loops - int|bool (optional) The number of times to loop the image; 0=infinite; false=no loop
  • $disposal - int (optional) Gif disposal method
  • $transRed - int (optional) Transparent red color
  • $transGreen - int (optional) Transparent green color
  • $transBlue - int (optional) Transparent blue color
$sources = array("frame1.gif", "frame2.gif", "frame3.gif"); 
echo EasyImage::Create($sources);

Editing

All the editing methods are chainable for convenience.


Border Radius

borderRadius( [ $radius=10 [, $colour="#FFFFFF" ] ] )
  • $radius - int (optional) Radius of the corners
  • $colour - string (optional) A hex color for the corners
echo EasyImage::Create($file)->borderRadius();

Add Watermark

addWatermark( $text, $font_size, $color, $font_file [, $horizontal_pos=EasyImage::RIGHT [, $vertical_pos=EasyImage::BOTTOM [, $opacity=65 [, $padding=3 ] ] ] ] )
  • $text - string Text for the watermark
  • $font_size - int Size of font for the watermark
  • $color - string Hex color for the image
  • $font_file - string Path to trueType font file
  • $horizontal_pos - string (optional) Horizontal position of the watermark
  • $vertical_pos - string (optional) Vertical position of the watermark
  • $opacity - int (optional) Opacity of the watermark
  • $padding - int (optional) Padding around the watermark
echo EasyImage::Create($file)->
    addWatermark(
        "uploaded to mysite.com",
        18,
        "#000",
        "courier.ttf"
    );

Change Opacity

makeOpaque( $alpha )
  • $alpha - int Alpha level, a number 0 through 127
echo EasyImage::Create($file)->makeOpaque(56);

Add Overlay

addOverlay( $image, $dst_x, $dst_y [, $src_w=null [, $src_h=null ] ] )
  • $image - mixed An image URL, filepath, or EasyImage object
  • $dst_x - int Destination x ordinate
  • $dst_y - int Destination y ordinate
  • $src_w - int (optional) Source width
  • $src_h - int (optional) Source height
echo EasyImage::Create($file)->addOverlay($overlay, 0, 0);

Convert to Black and White

blackAndWhite( )
echo EasyImage::Create($file)->blackAndWhite();

Crop an Image

crop( $new_width, $new_height [, $x=0 [, $y=0] ] )
  • $new_width - int New image width
  • $new_height - int New image height
  • $x - int (optional) Starting x position
  • $y - int (optional) Starting y position
echo EasyImage::Create($file)->crop(32, 32);

Auto-Crop an Image

autoCrop( [ $threshold=50 [, $padding=3 ] ] )
  • $threshold - int (optional) Color sensitivity
  • $padding - int (optional) Padding of pixels around image
echo EasyImage::Create($file)->autoCrop();

Transparent to Color

transparentToColor( $hexColor )
  • $hexColor - string Hex color to remove
echo EasyImage::Create($file)->transparentToColor($hexColor);

Remove a Color

removeColor( $hexColor )
  • $hexColor - string Hex color to remove
echo EasyImage::Create($file)->removeColor($hexColor);

Replace a Color

replaceColor( $oldColor, $newColor [, $threshold=50 ] )
  • $oldColor - string Color to remove
  • $newColor - string Color to replace with
  • $threshold - int (optional) Color sensitivity
echo EasyImage::Create($file)->replaceColor("#ff0000", "#00ffff");

Scale an Image

scale( $width [, $height=null [, $cover=EasyImage::FILL ] ] )
  • $width - int Maximum new width of image
  • $height - int (optional) Maximum new height of image
  • $cover - string (optional) Scale mode
echo EasyImage::Create($file)->scale(150);

Resize an Image

resize( $width, $height )
  • $width - int New width of image
  • $height - int New height of image
echo EasyImage::Create($file)->resize(150, 200);

Rotate an Image

rotate( $degrees )
  • $degrees - int Degrees to rotate image
echo EasyImage::Create($file)->rotate(180);

Tile an Image

tile( $width, $height )
  • $width - int Width to tile image to
  • $height - int Height to tile image to
echo EasyImage::Create($file)->rotate(500, 300);

Reverse Colors

reverseColors( )
echo EasyImage::Create($file)->reverseColors();

Tile an Image

greyScale( )
echo EasyImage::Create($file)->greyScale();

Adjust Brightness

adjustBrightness( $newBrightness )
  • $newBrightness - int a number between 255 and -255
echo EasyImage::Create($file)->adjustBrightness(150);

Adjust Contrast

adjustContrast( $newContrast )
  • $newContrast - int New contrast level
echo EasyImage::Create($file)->adjustContrast(150);

Edge Detect

edgeDetect( )
echo EasyImage::Create($file)->edgeDetect();

Emboss

emboss( )
echo EasyImage::Create($file)->emboss();

Gaussian Blur

gaussianBlur( )
echo EasyImage::Create($file)->gaussianBlur();

Selective Blur

selectiveBlur( )
echo EasyImage::Create($file)->selectiveBlur();

Sketch Filter

sketch( )
echo EasyImage::Create($file)->sketch();

Add Vignette

vignette( )
echo EasyImage::Create($file)->vignette();

Create Alpha Map

alphaMap( )
echo EasyImage::Create($file)->alphaMap();

Remove Transparency

removeTransparency( [ $newHexColor="#FFFFFF" ] )
  • $newHexColor - string (optional) Hex color to replace transparent pixels with
echo EasyImage::Create($file)->removeTransparency();

Add Perspective

perspective( [ $gradient=0.85 [, $angle=EasyImage::TOP ] ] )
  • $gradient - float (optional) Gradient of perspective
  • $angle - string (optional) Angle of perspective
echo EasyImage::Create($file)->perspective();

Pixelate

pixelate( $blocksize [, $advanced=true ] )
  • $blocksize - int New pixel size
  • $advanced - boolean (optional) Use advanced method?
echo EasyImage::Create($file)->pixelate(10);

Adjust Smoothness

adjustSmoothness( $level )
  • $level - int New smoothness level
echo EasyImage::Create($file)->adjustSmoothness(10);

Color Mask

colorMask( $MaskHexColor )
  • $MaskHexColor - string A hex color
echo EasyImage::Create($file)->colorMask("#FF000");

Colorize

colorize( $hexColor [, $alpha=0 ] )
  • $hexColor - int Color to tint the image
  • $alpha - int (optional) Alpha level to use
echo EasyImage::Create($file)->colorize("#FF0000");

Color Fill

colorFill( $startx, $starty, $hexColor )
  • $startx - int Starting x ordinate
  • $starty - int Starting y ordinate
  • $hexColor - string Hex color string to remove
echo EasyImage::Create($file)->colorFill(0, 0, "#FF0000");

Merge Images

mergeImages( $src [, $dst_x=0 [, $dst_y=0 [, $src_x=0 [, $src_y=0 [, $src_w=null [, $src_h=null [, $pct=50 ] ] ] ] ] ] ] )
  • $src - mixed An Image file, base64 image string, or EasyImage object
  • $dst_x - int (optional) Destination x position
  • $dst_y - int (optional) Destination y position
  • $src_x - int (optional) Source x position
  • $src_y - int (optional) Source y position
  • $src_w - int (optional) Source width; will use full width if null
  • $src_h - int (optional) Source height; will use full height if null
  • $pct - int (optional) Percent of image to show thru
echo EasyImage::Create($file)->mergeImages("myImage.psd");

Flip Image

flip( [ $mode=EasyImage::HORIZ ] )
  • $mode - string (optional) Image flip method
echo EasyImage::Create($file)->flip();

Convert Image Type

convertTo( $type )
  • $type - string New image mime type
echo EasyImage::Create($file)->convertTo(EasyImage::PDF);

Concatenate Images

concat( $image [, $inline=EasyImage::HORIZ ] )
  • $image - mixed New image file, url, base64 string, or EasyImage object
  • $inline - string Concat position
echo EasyImage::Create($file)->concat($newImage);

Getters

The class exposes several methods that allow you to get information about the image.


Get Colors in the Image

getColors( [ $hex=false ] )
  • $hex - bool If true, will return hex colors instead of GRB arrays

Returns: An array containing all unique colors in the image. If the EasyImage object is an animated GIF, will return an array of arrays for each layer.

$colors = EasyImage::Create($file)->getColors();

Get GD Image Resource

getImageResource( )

Returns: The GD image resource for the current EasyImage object. If it's an animated GIF, it will return an array containing the GD resource for each layer of the GIF.

$im = EasyImage::Create($file)->getImageResource();

Get a color that doesn't exist in the image

getUniqueColor( [ $omitColors=array() ] )
  • $omitColors - array (optional) array of rgb colors to omit

Returns: A color that does not exist in the current image, or false if it's an animated GIF.

$color = EasyImage::Create($file)->getUniqueColor();

Get Offsets after being auto-cropped

getOffsets( )

Returns: If the current image was autoCrop()'ed, it will return an array containing the offsets of the crop. If it's an animated GIF it will return an array for each layer/frame.

$offsets = EasyImage::Create($file)->getOffsets();

Get Original Filepath

getFilepath( )

Returns: Returns the filepath for the original image used to create the current EasyImage object. This may or may not be a temporary file. If it's an animated GIF, will return an array with the filepath foe each layer.

$path = EasyImage::Create($file)->getFilepath();

Get Image Width

getWidth( )

Returns: The width of the current image.

$width = EasyImage::Create($file)->getWidth();

Get Image Height

getHeight( )

Returns: The height of the current image.

$height = EasyImage::Create($file)->getHeight();

Get Image Mime Type

getMimeType( )

Returns: The mime type of the current image.

$type = EasyImage::Create($file)->getMimeType();

Get Orientation

getOrientation( )

Returns: Either the string "portrait" or "landscape".

$orientation = EasyImage::Create($file)->getOrientation();

Get Pixel Color Index

getPixelColorIndex( $x, $y )
  • $x - int X position of pixel
  • $y - int y position of pixel

Returns: The color index of a color at the given coordinates, or an array containing the color at that index of each frame of the GIF.

$color = EasyImage::Create($file)->getPixelColorIndex();

Get Pixel Color FROM Index

getColorsFromIndex( $index )
  • $index - int Color index of the image

Returns: The pixel color for a given color index.

$color = EasyImage::Create($file)->getColorsFromIndex($index);

Get Pixel RGBA Colors

getPixelRGBA( $x, $y )
  • $x - int The x position of the pixel
  • $y - int The y position of the pixel

Returns: The GRBA color values of a given pixel.

$color = EasyImage::Create($file)->getPixelRGBA(0, 0);

Get Pixel HEX Color

getPixelHexColor( $x, $y )
  • $x - int The x position of the pixel
  • $y - int The y position of the pixel

Returns: The HEX color values of a given pixel.

$color = EasyImage::Create($file)->getPixelHexColor(0, 0);

Get Layers of an animated GIF

getGifSources( )

Returns: An array of EasyImage objects for each layer of an animated GIF.

$Layers = EasyImage::Create($AnimatedGIF)->getGifSources();

Get a Base 64 encoded string of the image

getBase64( [ $output_mimetype=null ] )
  • $output_mimetype - int (optional) Type of image to encode to; Defaults to the inputted image's mimetype

Returns: The base 64 data URL of the image.

$color = EasyImage::Create($file)->getBase64();

Get Raw Image Data

getString( [ $output_mimetype=null ] )
  • $output_mimetype - int (optional) Type of image to encode to; Defaults to the inputted image's mimetype
$color = EasyImage::Create($file)->getString();

Returns: The image as a string.


Get a Copy of the Object

getCopy( )
$color = EasyImage::Create($file)->getCopy();

Returns: A copy of the current EasyImage object.


Utilities

EasyImage provides some helper methods and utilities.


Compare Two Colors

compareColors( $color1, $color2 )
  • $color1 - mixed A hex color or RGB color array
  • $color2 - mixed A hex color or RGB color array

Returns: true if the two colors are the same, false else.

$sameColor = EasyImage::compareColors("#FFF", array(255, 255, 255));

Compare Two Colors' Luma

compareLuma( $color1, $color2 )
  • $color1 - mixed An RGB color array
  • $color2 - mixed An RGB color array

Returns: 1 if the first color has a greater luma value than the second, else -1.

$brighterColor = EasyImage::compareLuma("#FFF", "#FF0);

Sort Colors by Luma

colorSortLuma( $colors )
  • $colors - array An array of RGB color arrays

Returns: An array of colors, sorted by luma value.

$colors = EasyImage::colorSortLuma($colors);

Check if an image is an animated gif

isAnimatedGif( $file )
  • $file - string A file path to an image

Returns: True if the image is an animated GIF, else false.

echo EasyImage::isAnimatedGif($file) ? 
"Hoorah, animation!" :
"Boo, no animation." ;

Sort Colors by Dominance

colorSort( $colors )
  • $colors - array An array of RGB color arrays

Returns: An array of colors, sorted by color dominance value.

$colors = EasyImage::colorSort($colors);

Get Color Distance

getColorDistance( $color1, $color2 )
  • $color1 - array An array of RGB color arrays
  • $color2 - array An array of RGB color arrays

Returns: Get the distance between any two given colors.

$distance = EasyImage::getColorDistance($color1, $color2);

Convert RGB to HEX Color

RGBToHex( $color )
  • $color - array An array of RGB colors

Returns: The hex color string for the given RGB color array.

$distance = EasyImage::RGBToHex($color);

Convert HEX to RGB Colors

hexToRGB( $color )
  • $color - string A HEX color string

Returns: The RGB color array for the given hex color string.

$distance = EasyImage::hexToRGB($color);

Get Colors in Gradient

gradientColors( $color1, $color2 [, $ColorSteps=10 ] )
  • $color1 - array Array of hex colors in gradient
  • $ColorSteps - int (optional) How many colors to return

Returns: An array of colors in the gradient.

$colors = EasyImage::gradientColors($color1, $color2);

Output

EasyImage provides several built-in methods for outputting the image.


Output to Browser

This is the most common type of output, and the easiest to use. To send the Image to the browser, simply echo or print it.

echo EasyImage::Create($file);

Save Image to the Server

save( $filename )
  • $filename - string filename to save to
EasyImage::Create($image)->save("myImage.png");

Force Download

download( $filename )
  • $filename - string filename to save as
EasyImage::Create($image)->download("yourImage.png");