Ves al contingut
Racó Tècnic
Torna enrere

Watermark & Image Component for CakePHP

Edita la pàgina

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:

Well Known Bugs:

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:

* 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()) {
  // ...
}

Comentaris arxivats (6)

Els comentaris estan tancats. Aquests comentaris provenen de l'antic lloc de WordPress i només es poden llegir.

  1. nannz

    Thanks a lot.

  2. Pyo

    Is it working with CakePhp 2.x ?

  3. Pyo

    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. Booletaire

    @nannz you're welcome :D

    @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. Pyo

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

  6. Booletaire

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

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


Edita la pàgina
Comparteix aquest article:

Article anterior
Web site inspiration - Inspiración para creación de paginas web
Article següent
Gigya: Todas las redes sociales optimizadas para el desarrollo