Inici > Programació, Web > Watermark & Image Component for CakePHP

Watermark & Image Component for CakePHP

dimarts, 6 d'abril de 2010 Imprimir

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/

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:

  • Linux php 5.2.10
  • Linux php 5.2.13
  • Windows php 5.3.1
  • Windows php 5.3.2

Well Known Bugs:

  • Uses the method mime_content_type that as they say in php.net: This function has been deprecated as the PECL extension Fileinfo provides the same functionality (and more) in a much cleaner way. [ SOLVED ]
  • By rotating a transparent PNG image at an angle that is not a multiple of 90º the extra generated background is not transparent.

Download:

http://github.com/elboletaire/Watimage/archives/master

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:
  • resize: Maintains the aspect ratio of the image and makes sure that it fits within the max width and max height (thus some side will be smaller).
  • resizemin: Maintains aspect ratio but resizes the image so that once one side meets its max width or max height condition, it stays at that size (thus one side will be larger).
  • resizecrop: Resize to max, then crop to center.
  • crop: A straight centered crop.

* Resizing criteria extracted from iamkoa labs image upload component

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() )
{
	// ...
}
Categories: Programació, Web Etiquetes:, , , ,
  1. nannz
    dilluns, 21 de maig de 2012 a les 11:48 | #1

    Thanks a lot.

  2. dijous, 14 de juny de 2012 a les 00:43 | #2

    Is it working with CakePhp 2.x ?

  3. dijous, 14 de juny de 2012 a les 01:07 | #3

    Two small modifications to do to use this component with CakePhp 2.x :

    1. After “class Watimage”, add “Component extends Component” (instead of ‘extends Object’)

    2. Remove the four lines in the construct method.

  4. dimarts, 19 de juny de 2012 a les 19:20 | #4

    @nannz you’re welcome 😀

    @Pyo thanks for your comment. I’ll try to create a tag for the 2.X version ASAP.

    Also, if I get the chance, I will update this post too.

  5. dimarts, 19 de juny de 2012 a les 20:44 | #5

    Keep us in touch by posting a new comment if you do it :-)

  6. dimecres, 5 de setembre de 2012 a les 18:12 | #6

    https://github.com/elboletaire/Watimage/tree/2.X

    Pardon the delay, too much work lately .. btw, thanks for your help Pyo :)

Comentaris tancats.