- Racó tècnic - http://www.racotecnic.com -

Watermark & Image Component for CakePHP

Note: This Component has been updated and you can find the changes here: /2011/04/clase-php-para-tratar-imagenes-rotar-redimensionar-anadir-marcas-de-agua/ [1]

Time ago I created a CakePHP component for applying watermarks to images. Slowly I extended it and now in addition to applying watermarks is used to rotate and resize images.

You can still improve it a lot, especially in terms of code, but as it works and I have little time, I can barely make improvements.

Tested from:

Well Known Bugs:

Download:

http://github.com/elboletaire/Watimage/archives/master [4]

Usage:

<?php // /app/controllers/foo_controller.php
class FooController extends AppController
{
	var $name = "Foo";
	// Remember to initialize the component
	var $components = array("Watermark");

	public function upload()
	{
		// ... upload stuff
		if ( $is_uploaded )
		{
			$this->Watermark->setImage($image_path);
			$this->Watermark->resize(array('type' => 'resizecrop', 'size' => array(450,450)));
			$this->Watermark->generate($dest_path);
			// ... more stuff ...
		}
	}
}

Original files used for demonstrations:

Resize:

$this->Watermark->setImage($image_path);
$this->Watermark->resize(array('type' => 'resizecrop'), 'size' => array('300', '200'));
$this->Watermark->generate($dest_path);
Resize types:

* Resizing criteria extracted from iamkoa labs image upload component [5]

Rotate:

$this->Watermark->setImage($image_path);
$this->Watermark->rotateImage(array('degrees' => 45));
$this->Watermark->generate($dest_path);

Apply watermark:

$this->Watermark->setImage($image_path);
$this->Watermark->setWatermark(array('file' => $watermark_file, 'position' => 'bottom right', 'size' => '150%'));
$this->Watermark->applyWatermark();
$this->Watermark->generate($dest_path);

All together:

$this->Watermark->setImage($image_path);
$this->Watermark->setWatermark(array('file' => 'watermark.png', 'position' => 'bottom right', 'size' => '150%'));
$this->Watermark->resize(array('type' => 'resizecrop', 'size' => array('300', '200')));
$this->Watermark->applyWatermark();
$this->Watermark->rotateImage(array('degrees' => 45, 'bgcolor' => 0));
$this->Watermark->generate($dest_path);

Changing order:

$this->Watermark->setImage($image_path);
$this->Watermark->setWatermark(array('file' => 'watermark.png', 'position' => 'bottom right', 'size' => '150%'));
$this->Watermark->rotateImage(array('degrees' => 45, 'bgcolor' => 0));
$this->Watermark->resize(array('type' => 'resizecrop', 'size' => array('300', '200')));
$this->Watermark->applyWatermark();
$this->Watermark->generate($dest_path);

Show image instead of saving it:

$this->Watermark->setImage($image_path);
$this->Watermark->generate(); // Without params

With errors:

// every component method return false on error
if ( !$this->Watermark->setImage($image_path) )
{
	// whatever
	print_r($this->Watermark->errors);
}

if ( !$this->Watermark->resize(array('type' => 'resizecrop', 'size' => 250)) )
{
	// ...
}

if ( !$this->Watermark->generate() )
{
	// ...
}