Graphics Draw

PHP is not limited to creating just HTML output. It can also be used to create and manipulate image files in a variety of different image formats, including GIF, PNG, JPEG, WBMP, and XPM. Even more conveniently, PHP can output image streams directly to a browser. You will need to compile PHP with the GD library of image functions for this to work. GD and PHP may also require other libraries, depending on which image formats you want to work with.

You can use the image functions in PHP to get the size of JPEG, GIF, PNG, SWF, TIFF and JPEG2000 images.

$image = new \Samy\Image\Gd();

General Operations

create

Create empty image.

$image = $image->create($width, $height);

isLoadable

Check is file loadable.

$loadable = $image->isLoadable($filename);

loadFile

Load file image.

$image = $image->loadFile($filename);

save

Save image.

$image = $image->save($image_type, $filename);
image_typeValue
IMAGETYPE_BMP6
IMAGETYPE_JPEG2
IMAGETYPE_PNG3
IMAGETYPE_WBMP15
IMAGETYPE_WEBP18
IMAGETYPE_XBM16

describe

Describe image.

$image = $image->describe($image_type, &$image_describe);
image_typeValue
IMAGETYPE_BMP6
IMAGETYPE_JPEG2
IMAGETYPE_PNG3
IMAGETYPE_WBMP15
IMAGETYPE_WEBP18
IMAGETYPE_XBM16
image_describeTypeDescription
mimetypestringimage content-type
datastringblob image file
sizeintegerfile size
md5stringmd5 hash
sha1stringsha1 hash

getImage

Retrieve resource image.

$resource = $image->getImage();

hasImage

Check if instance has an image.

$has_image = $image->hasImage();

getType

Retrieve image type.

$image_type = $image->getType();

getWidth

Retrieve image width.

$image_width = $image->getWidth();

getHeight

Retrieve image height.

$image_height = $image->getHeight();

getBlendMode

Retrieve image blend mode.

$blend_mode = $image->getBlendMode();

withBlendMode

Return an instance with provided image blend mode.

$image = $image->withBlendMode($blend_mode);

Pixel Operations

getArgb

Retrieve argb color of a pixel.

$argb = $image->getArgb($x, $y);

withArgb

Return an instance with provided argb color of a pixel.

$image = $image->withArgb($x, $y, $value);

getAlpha

Retrieve alpha color of a pixel.

$alpha = $image->getAlpha($x, $y);

withAlpha

Return an instance with provided alpha color of a pixel.

$image = $image->withAlpha($x, $y, $value);

getRed

Retrieve red color of a pixel.

$red = $image->getRed($x, $y);

withRed

Return an instance with provided red color of a pixel.

$image = $image->withRed($x, $y, $value);

getGreen

Retrieve green color of a pixel.

$green = $image->getGreen($x, $y);

withGreen

Return an instance with provided green color of a pixel.

$image = $image->withGreen($x, $y, $value);

getBlue

Retrieve blue color of a pixel.

$blue = $image->getBlue($x, $y);

withBlue

Return an instance with provided blue color of a pixel.

$image = $image->withBlue($x, $y, $value);

Draw Operations

drawRectangle

Draw a rectangle.

$image = $image->drawRectangle($x1, $y1, $x2, $y2, $argb);

drawFilledRectangle

Draw a filled rectangle.

$image = $image->drawFilledRectangle($x1, $y1, $x2, $y2, $argb);

drawEllipse

Draw an ellipse.

$image = $image->drawEllipse($center_x, $center_y, $width, $height, $argb);

drawFilledEllipse

Draw a filled ellipse.

$image = $image->drawFilledEllipse($center_x, $center_y, $width, $height, $argb);

drawArc

Draw an arc.

$image = $image->drawArc($center_x, $center_y, $width, $height, $start_angle, $end_angle, $argb);

drawFilledArc

Draw a filled arc.

$image = $image->drawFilledArc($center_x, $center_y, $width, $height, $start_angle, $end_angle, $argb, $arc_style);
arc_styleValue
IMG_ARC_PIE0
IMG_ARC_CHORD1
IMG_ARC_NOFILL2
IMG_ARC_EDGED4

drawPolygon

Draw a polygon.

$polygon_points = array(
    array($x1, $y1),
    array($x2, $y2),
    array($x3, $y3),
    ...
);

$image = $image->drawPolygon($polygon_points, $argb);

drawFilledPolygon

Draw a filled polygon.

$polygon_points = array(
    array($x1, $y1),
    array($x2, $y2),
    array($x3, $y3),
    ...
);

$image = $image->drawFilledPolygon($polygon_points, $argb);

TrueType Fonts Operations

ttfBox

Give the bounding box of a text using TrueType fonts.

$ttf_box = $image->ttfBox($font, $text, $size, $angle);
ttf_boxTypeDescription
x1integerlower left corner, X position
y1integerlower left corner, Y position
x2integerupper right corner, X position
y2integerupper right corner, Y position

ttfWrite

Write text to the image using TrueType fonts.

$image = $image->ttfWrite($x, $y, $font, $text, $size, $angle, $argb);

Filter Operations

reverseColor

Reverses all colors of the image.

$image = $image->reverseColor();

grayscale

Converts the image into grayscale.

$image = $image->grayscale();

edgeDetect

Uses edge detection to highlight the edges in the image.

$image = $image->edgeDetect();

embosses

Embosses the image.

$image = $image->embosses();

blurGaussian

Blurs image using Gaussian method.

$image = $image->blurGaussian();

blurSelective

Blurs image using Selective method.

$image = $image->blurSelective();

sketchy

Uses mean removal to achieve a "sketchy" effect.

$image = $image->sketchy();

brightness

Changes the brightness of the image.

$image = $image->brightness($level);

contrast

Changes the contrast of the image.

$image = $image->contrast($level);

smooth

Makes the image smoother.

$image = $image->smooth($level);

pixelate

Applies pixelation effect to the image.

$image = $image->pixelate($block_size, $advanced_effect);

scatter

Applies scatter effect to the image.

$image = $image->scatter($substraction, $addition, $indexed_color);

colorize

Converts the image into specify color.

$image = $image->colorize($red, $green, $blue, $alpha);

Transform Operations

resize

Resize image.

$image = $image->resize($width, $height);

crop

Crop image.

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

copy

Copy image.

$image = $image->copy($source_image, $source_x, $source_y, $source_width, $source_height, $destination_x, $destination_y, $destination_width, $destination_height);

rotate

Rotate image with a given angle.

$image = $image->rotate($angle);

flipHorizontal

Flip image horizontally.

$image = $image->flipHorizontal();

flipVertical

Flip image vertically.

$image = $image->flipVertical();

flipBoth

Flip image horizontally and vertically.

$image = $image->flipBoth();

roundCorner

Rounded corner.

$image = $image->roundCorner($radius, $corner_mode = GdCorner::ALL);
corner_modeValueDescription
GdCorner::NW GdCorner::NORTHWEST1top left side
GdCorner::NE GdCorner::NORTHEAST2top right side
GdCorner::N GdCorner::NORTH3top side
GdCorner::SE GdCorner::SOUTHEAST4bottom right side
GdCorner::E GdCorner::EAST6right side
GdCorner::SW GdCorner::SOUTHWEST8bottom left side
GdCorner::W GdCorner::WEST9left side
GdCorner::S GdCorner::SOUTH12bottom side
GdCorner::ALL15all side

thumbnail

Generate thumbnail.

$image = $image->flipBoth($width, $height);