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.
$image = new Image( );
// Include core Image library
require_once 'class.Image.php';
// Initialize Image object
$cart = new Image();
Opens an image for editing.
bool $image->open( string $imagePath );
$image->open('example.jpg');
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');
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();
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.';
}
Gets image width.
int $image->getWidth( );
$width = $image->getWidth();
Gets image height.
int $image->getHeight( );
$width = $image->getHeight();
Gets HEX color code for a pixel in the image.
string $image->getColorFromPixel( int $x, int $y );
echo $image->getColorFromPixel(10, 10);
Result:
505050
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 )
Converts RGB color into HEX code.
string $image->rgb2hex(int $r, int $g, int $b);
echo $image->rgb2hex(80, 80, 80);
Result:
505050
Scales image to specified percent.
$image->scale( int $percent );
// Make the image 50% smaller
$image->scale(50);
Result:
Resizes to a widthand keep the image dimension ratio.
resizeToWidth(int $width);
$image->resizeToWidth(200);
Result:
Resizes to a height and keep the image dimension ratio.
resizeToHeight(int $height);
$image->resizeToHeight(200);
Result:
Resizes image into specified width and height.
$image->resize( int $width, int $height );
$image->resize(200, 150);
Result:
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:
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:
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: