Editing images

The Artaengine PhotoEdit class can be used for few basic image editing.

Loading

To work on an image it has be loaded by the constructor or method load.

$photo = new PhotoEdit('example.jpg');
$photo = new PhotoEdit();
$photo->load('example.jpg');

The following file formats can be loaded: jpeg, png, gif, bmp, wbmp, gd, gd2, xbm and xpm.

Checking

Method isPhoto checks if a file is a valid image or not. If filename is not given as an argument then check will be done on the loaded image.

$photo = new PhotoEdit('example.jpg');

if ($photo->isPhoto()) {
    echo 'example.jpg is a photo';
}
$photo = new PhotoEdit();

if ($photo->isPhoto('example2.jpg')) {
    echo 'example2.jpg is a photo';
}

Method checkType checks if the loaded file format is within a set or not:

$photo = new PhotoEdit('example.jpg');

if ($photo->checkType('jpg')) {
    echo 'example.jpg is a jpeg<br/>';
}

if ($photo->checkType('png', 'jpg')) {
    echo 'example.jpg is a jpeg or png<br/>';
}

if ($photo->checkType(array('png', 'jpg'))) {
    echo 'example.jpg is a jpeg or png<br/>';
}

if ($photo->checkType()) {
    echo 'example.jpg is a jpeg, png, gif, bmp, wbmp, gd, gd2, xbm or xpm<br/>';
}

Getting file type

Method getType returns file type which will be one of: bmp, jpg, png, gif, cod, ief, jfif, tiff, ras, ico, cmx, pnm, pbm, pgm, ppm, rgb, xbm, xpm, xwd, jps, fh or False.

$photo = new PhotoEdit('example.jpg');
$fileType = $photo->getType();
$photo = new PhotoEdit();
$fileType = $photo->getType('example.jpg');

Method getMimeType returns file mime type which will be one of: image/jpeg, image/png, image/gif, image/bmp, image/cis-cod, image/ief, image/pipeg, image/tiff, image/x-cmu-raster, image/x-cmx, image/x-icon, image/x-portable-anymap, image/x-portable-bitmap, image/x-portable-graymap, image/x-portable-pixmap, image/x-rgb, image/x-xbitmap, image/x-xpixmap, image/x-xwindowdump, image/x-jps, image/x-freehand or False.

$photo = new PhotoEdit('example.jpg');
$fileType = $photo->getMimeType();
$photo = new PhotoEdit();
$fileMimeType = $photo->getMimeType('example.png');

Getting photo size

To get photo width/height:

$photo = new PhotoEdit('example.jpg');

$examplepngWidth   = $photo->getWidth();
$examplepngHeight  = $photo->getHeight();

$example2pngWidth  = $photo->getWidth('example2.png');
$example2pngHeight = $photo->getHeight('example2.png');

Converting

Method convert is used to convert the loaded file format to another type.

In below example loaded photo is converted in memory but not saved to a file. More changes can be done on the loaded file and finally saved to a file by method save.

$photo = new PhotoEdit('example.jpg');
/* convert loaded photo to png in memory */
$photo->convert('png');

In below example loaded photo is converted and saved to a new file.

$photo = new PhotoEdit('example.jpg');
/* convert example.jpg to png and save as newfile.png */
$photo->convert('png', 'newfile.png');

When saving a png or jpeg photos to a file quality can be mentioned.

For a jpeg file quality can be (0 to 9) default is 9.

For a png file quality can be (0 to 100) default is 100.

$photo = new PhotoEdit('example.jpg');
/* convert example.jpg to png and save as newfile.png */
$photo->convert('png', 'newfile.png', 5);
$photo = new PhotoEdit('example.png');
/* convert example.png to png and save as newfile.jpg */
$photo->convert('png', 'newfile.jpg', 50);

Resizing

Method resize is used to resize the loaded file.

In below example loaded photo is resize in memory but not saved to a file. More changes can be done on the loaded file and finally saved to a file by method save.

$photo = new PhotoEdit('example.jpg');
/* resize width to 100px height to 200px */
$photo->resize(100, 200);
$photo = new PhotoEdit('example.jpg');
/* resize width to 100px and calculate height from original ratio */
$photo->resize(100);
$photo = new PhotoEdit('example.jpg');
/* resize height to 200px and calculate width from original ratio */
$photo->resize(Null, 200);

In below example loaded photo is resized and saved to a new file.

$photo = new PhotoEdit('example.jpg');
$photo->resize(100, Null, 'newfile.jpg');

When saving a png or jpeg photos to a file quality can be mentioned.

For a jpeg file quality can be (0 to 9) default is 9.

For a png file quality can be (0 to 100) default is 100.

$photo = new PhotoEdit('example.jpg');
$photo->resize(100, Null, 'newfile.jpg', 80);

To resize a photo only if the photo will be smaller after being resized use resizeLarger. Usage of resizeLarger is the same as resize.

$photo = new PhotoEdit('example.jpg');
/* will resize only if example.jpg width is more than 100px */
$photo->resizeLarger(100, Null);

To resize and crop a photo (resize to a new width/height which do not respect the original ratio but not to stretch the new image by cropping and compromising image edges) use resizeCrop. Usage of resizeCrop is the same as resize.

$photo = new PhotoEdit('example.jpg');
/* will resize only example.jpg to a square,
   is original image is not a square the image will not be stretched
   but cropped */
$photo->resizeCrop(100, 100, Null);

Cropping

To crop an image from a point and image width and height:

$photo = new PhotoEdit('example.jpg');
/* crop from point(x=50, y=50) 200px right and 100px down */
$photo->crop(40, 50, 200, 100);
$photo = new PhotoEdit('example.jpg');
/* crop from point(x=50, y=50) 200px right and 100px down */
$photo->crop(40, 50, 200, 100, 'newfile.jpg');
$photo = new PhotoEdit('example.jpg');
/* crop from point(x=50, y=50) 200px right and 100px down */
$photo->crop(40, 50, 200, 100, 'newfile.jpg', 80);

To crop an image from one point to another point:

$photo = new PhotoEdit('example.jpg');
/* crop from point(50, 50) to point(200, 100) */
$photo->cropX(40, 50, 200, 100);
$photo = new PhotoEdit('example.jpg');
/* crop from point(50, 50) to point(200, 100) */
$photo->cropX(40, 50, 200, 100, 'newfile.jpg');
$photo = new PhotoEdit('example.jpg');
/* crop from point(50, 50) to point(200, 100) */
$photo->cropX(40, 50, 200, 100, 'newfile.jpg', 80);

Watermarking

A photo can be watermarked by another photo or by a text.

Method watermark watermarks the loaded photo with another photo. The result can be in memory or saved to a file:

$photo = new PhotoEdit('example.jpg');
/* watermarks example.jpg with stamp.png at (0, 0) */
$photo->watermark('stamp.png');
$photo = new PhotoEdit('example.jpg');
/* watermarks example.jpg with stamp.png at (10, 15) */
$photo->watermark('stamp.png', 10, 15);
$photo = new PhotoEdit('example.jpg');
/* watermarks example.jpg with stamp.png at (10, 15) and saves the watermarked
   photo to newfile.png */
$photo->watermark('stamp.png', 10, 15, 'newfile.png', 80);

Method watermarkText watermarks the loaded photo with text. The result can be in memory or saved to a file:

$photo = new PhotoEdit('example.jpg');
/* watermarks example.jpg with a text at (0, 0) */
$photo->watermarkText('Copyright (c) by me', 10, 15);
$photo = new PhotoEdit('example.jpg');
/* watermarks example.jpg with a text at (10, 10) */
$photo->watermarkText('Copyright (c) by me', 10, 15);
$photo = new PhotoEdit('example.jpg');
/* watermarks example.jpg with a
   text(font=arial font-size:10px)
   at (10, 10) */
$photo->watermarkText('Copyright (c) by me', 10, 15, 'arial.ttf');
$photo = new PhotoEdit('example.jpg');
/* watermarks example.jpg with a
   text(font=arial font-size:20px)
   at (10, 10) */
$photo->watermarkText('Copyright (c) by me', 10, 15, 'arial.ttf', 20);
$photo = new PhotoEdit('example.jpg');
/* watermarks example.jpg with a
   text(font=arial font-size:20px)
   at (10, 10)
   and saves the watermarked photo to newfile.png */
$photo->watermarkText(
    'Copyright (c) by me',
    10, 15,
    'arial.ttf', 20,
    'newfile.png', 80
);

Saving

When a photo is loaded and one or more edits are done on it in the memory then the result can be saved to a photo file by the save method.

In this example after editing, the photo is saved on the loaded photo:

$photo = new PhotoEdit();
$photo
      ->load('example.jpg')
      ->resize(100, 200)
      ->watermark('stamp.png', 10, 10)
      ->save();

In this example after editing, the photo is saved into a new file:

$photo = new PhotoEdit();
$photo
      ->load('example.jpg')
      ->resize(100, 200)
      ->watermark('stamp.png', 10, 10)
      ->convert('png')
      ->save('newphoto.png', 80);

Error stack

After one or more action(s) have been done on a photo property errors can be checked to see if there was errors:

$photo = new PhotoEdit();
$photo
      ->load('example.jpg')
      ->resize(100, 200)
      ->watermark('stamp.png', 10, 10)
      ->convert('png')
      ->save('newphoto.png', 80);

/* check for errors: */
foreach ($upload->errors as $error) {
    switch ($error) {
        case Upload::ERR_LOAD:      echo 'Not loaded</br>';     break;
        case Upload::ERR_SAVE:      echo 'Not saved</br>';      break;
        case Upload::ERR_RESIZE:    echo 'Not resized</br>';    break;
        case Upload::ERR_CROP:      echo 'Not cropped</br>';    break;
        case Upload::ERR_WATERMARK: echo 'Not watermarked</br>';break;
    }
}

Example

Editing images
TOP