seikan / Image

A very simple PHP image library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Image

This is a very simple PHP image editing library. It required GD library to work. Examples in this documentation will use the following photo I took in Japan.

example

Usage

Getting Started

$image = new Image( );

// Include core Image library
require_once 'class.Image.php';

// Initialize Image object
$cart = new Image();

Open Image

Opens an image for editing.

bool $image->open( string $imagePath );

$image->open('example.jpg');

Save Image

Saved image to a location when finish editing. Default output format is JPG with 75% compression level.

bool $image->save( string $output[, string $imageType = IMAGETYPE_JPEG][, int $compression = 75] );

// Save image as output.png
$image->save('output.png', 'png');

Show Image

Displays image directly to browser without saving it to a local directory. Image will show as JPG by default.

$image->show([string $imageType = IMAGETYPE_JPEG][, bool $showHeader = true]);

$image->show();

Get Image Type

Gets the type of image currently open.

int $image->getImageType( );

// Get image type
$type = $image->getImageType();

if (IMAGETYPE_JPEG == $type) {
	echo 'This is a JPG image.';
} else if (IMAGETYPE_GIF == $type) {
	echo 'This is a GIF image.';
} else if (IMAGETYPE_PNG == $type) {
	echo 'This is a PNG image.';
}

Get Image Width

Gets image width.

int $image->getWidth( );

$width = $image->getWidth();

Get Image Height

Gets image height.

int $image->getHeight( );

$width = $image->getHeight();

Get HEX Code For a Pixel

Gets HEX color code for a pixel in the image.

string $image->getColorFromPixel( int $x, int $y );

echo $image->getColorFromPixel(10, 10);

Result:

505050

Convert HEX to RGB

Converts a HEX color code into RGB array.

array $image->hex2rgb( string $hex );

$color = $image->hex2rgb('505050');

print_r($color);

Result:

Array ( [r] => 80 [g] => 80 [b] => 80 ) 

Convert RGB to HEX

Converts RGB color into HEX code.

string $image->rgb2hex(int $r, int $g, int $b);

echo $image->rgb2hex(80, 80, 80);

Result:

505050

Scale

Scales image to specified percent.

$image->scale( int $percent );

// Make the image 50% smaller
$image->scale(50);

Result:

example

Resize to Width

Resizes to a widthand keep the image dimension ratio.

resizeToWidth(int $width);

$image->resizeToWidth(200);

Result:

example

Resize to Height

Resizes to a height and keep the image dimension ratio.

resizeToHeight(int $height);

$image->resizeToHeight(200);

Result:

example

Resize to Width and Height

Resizes image into specified width and height.

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

$image->resize(200, 150);

Result:

example

Crop

Crops image to a specified width and height.

$image->crop(int $width, int $height[, string $position = 'center']);

Available Position:

topLeft topCenter topRight

middleLeft center middleRight

bottomLeft bottomCenter bottomRight

int x, int y

$image->crop(300, 300, 'topLeft');

Result:

example

Add Text

Adds text to the image. Default text position is bottom right.

$image->addText(string $text, string $font, int $font_size, string $font_color[, string $position = "bottomRight"][, int $margin = 10]);

Available Position:

topLeft topCenter topRight

middleLeft center middleRight

bottomLeft bottomCenter bottomRight

int x, int y

$image->addText('example.com', 'arial.ttf', 12, '#FFFFFF');

Result:

example

Watermark

Adds watermark to the image. Default watermark will be added at bottom right.

$image->addWatermark(string $watermarkImage[, string $position = "bottomRight"][, int $margin = 10]);

Available Position:

topLeft topCenter topRight

middleLeft center middleRight

bottomLeft bottomCenter bottomRight

int x, int y

$image->addWatermark('watermark.png');

Result:

example

About

A very simple PHP image library.

License:MIT License


Languages

Language:PHP 100.0%