Note: This Component has been recently 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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:
1 2 3 | $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:
1 2 3 | $this->Watermark->setImage($image_path); $this->Watermark->rotateImage(array('degrees' => 45)); $this->Watermark->generate($dest_path); |
Apply watermark:
1 2 3 4 | $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:
1 2 3 4 5 6 | $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:
1 2 3 4 5 6 | $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:
1 2 | $this->Watermark->setImage($image_path); $this->Watermark->generate(); // Without params |
With errors:
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 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()) { // ... } |