Skip to content

Commit

Permalink
Added initialize method por CakePHP
Browse files Browse the repository at this point in the history
Added some @return comments
  • Loading branch information
elboletaire committed May 11, 2011
1 parent 35f8203 commit bda0368
Showing 1 changed file with 47 additions and 17 deletions.
64 changes: 47 additions & 17 deletions watermark.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// error_reporting(E_ALL);
// ini_set('display_errors', '1');
// ini_set('display_errors', 1);
/**
*
* @author Òscar Casajuana Alonso <[email protected]>
Expand All @@ -24,7 +24,6 @@
*
*/


// Uncomment next comment to use as CakePHP Component
class Watermark//Component extends Object
{
Expand Down Expand Up @@ -68,7 +67,8 @@ class Watermark//Component extends Object
private $image;
private $watermark = false;
private $output;
private $quality = 80;
private $quality = 80; // image export quality. You can set it with $watermark->setQuality(50);
// or with $watermark->setImage(array('quality' => 50, 'file' => 'file.jpg')
private $file;
private $extension;

Expand All @@ -81,9 +81,22 @@ public function __construct($file = null, $watermark = null)
$this->setWatermark($watermark);
}


/**
* Set image options
* @param array $options [optional]
* Contructor function for CakePHP
* @param Object &$controller pointer to calling controller
*/
public function initialize(&$controller, $options = array()) {
if ( !empty($options) )
{
if ( !empty($options['quality']) ) $this->setQuality($options['quality']);
}
}

/**
* Set image options
* @param array $options [optional]
* @return true on success; otherwise will return false
*/
public function setImage($file)
{
Expand Down Expand Up @@ -131,8 +144,8 @@ public function setQuality($quality)
* or you can set an array of options like:
* $options = array(
* 'file' => 'watermark.png',
* 'position' => 'bottom center',
* 'margin' => array('20', '10')
* 'position' => 'bottom center', // 'bottom center' by default
* 'margin' => array('20', '10') // 0 by default
* );
* @return true on success; false on failure
*/
Expand Down Expand Up @@ -206,7 +219,11 @@ public function setWatermark($options = array())

/**
* Resizes the image
* @param array $options = array('type' => 'resizemin|resizecrop|resize|crop', 'size' => array('x' => 2000, 'y' => 500))
* @param array $options = array(
'type' => 'resizemin|resizecrop|resize|crop',
'size' => array('x' => 2000, 'y' => 500)
)
You can also set the size without specifying x and y: array(2000, 500). Or directly 'size' => 2000 (takes 2000x2000)
* @return bool true on success; otherwise false
*/
public function resize($options = array())
Expand Down Expand Up @@ -257,7 +274,7 @@ public function resize($options = array())
// Resize image!
switch ( $this->resize['type'] )
{
/**
/*
* 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)
*/
Expand Down Expand Up @@ -296,7 +313,7 @@ public function resize($options = array())
$this->current_size['image']['width'] = $new_x;
$this->current_size['image']['height'] = $new_y;
break;
/**
/*
* 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)
Expand Down Expand Up @@ -332,7 +349,7 @@ public function resize($options = array())
$this->current_size['image']['width'] = $new_x;
$this->current_size['image']['height'] = $new_y;
break;
/**
/*
* resize to max, then crop to center
*/
case 'resizecrop':
Expand Down Expand Up @@ -364,8 +381,7 @@ public function resize($options = array())
$this->current_size['image']['width'] = $this->resize['size']['x'];
$this->current_size['image']['height'] = $this->resize['size']['y'];
break;

/**
/*
* a straight centered crop
*/
case 'crop':
Expand Down Expand Up @@ -405,7 +421,8 @@ public function resize($options = array())

/**
* Rotates an image
* @param mixed $options. You can specify directly the degrees or you can pass an array with degrees and bgcolor
* @param mixed $options = array('bgcolor' => 230, 'degrees' => -90); or $options = -90; // takes bgcolor = -1 by default
* @return true on success; false on failure
*/
public function rotateImage($options = array())
{
Expand Down Expand Up @@ -448,13 +465,17 @@ public function rotateImage($options = array())
}

/**
* Shortcut for rotateImage
* rotateImage alias
*/
public function rotate($options = array())
{
return $this->rotateImage($options);
}

/**
* Applies a watermark to the image. Needs to be initialized with $this->setWatermark()
* @return true on success, otherwise false
*/
public function applyWatermark()
{
if ( !empty($this->errors) ) return false;
Expand Down Expand Up @@ -501,6 +522,7 @@ public function applyWatermark()
/**
* Flips an image.
* @param string $type [optional] type of flip: horizontal / vertical / both
* @return true on success. Otherwise false
*/
public function flip($type = 'horizontal')
{
Expand Down Expand Up @@ -542,6 +564,7 @@ public function flip($type = 'horizontal')
* Generates the image file.
* @param string $path [optional] if not specified image will be printed on screen
* @param string $output [optional] mime type for output image (image/png, image/gif, image/jpeg)
* @return true on success. Otherwise false
*/
public function generate($path = null, $output = null)
{
Expand Down Expand Up @@ -615,6 +638,7 @@ public function generate($path = null, $output = null)

/**
* Creates an image from string
* @return true on success. Otherwise throws an Exception
*/
private function createImage($file)
{
Expand All @@ -631,6 +655,7 @@ private function createImage($file)

/**
* Applies some values to image for handling transparency
* @throw Exception on error
*/
private function handleTransparentImage()
{
Expand All @@ -652,6 +677,10 @@ private function handleTransparentImage()
}
}

/**
* Gets the file extension
* @return string with extension. Throws an Exception on error
*/
private function getFileExtension( &$file )
{
$f = pathinfo($file);
Expand All @@ -663,7 +692,7 @@ private function getFileExtension( &$file )

/**
* Obtains image sizes
* @return
* @return void
*/
private function getSizes()
{
Expand Down Expand Up @@ -743,7 +772,7 @@ private function getSizes()

/**
* Calculates the position using the 'position' and 'margin' vars
* @return
* @return void
*/
private function getWatermarkPosition()
{
Expand Down Expand Up @@ -792,6 +821,7 @@ private function getWatermarkPosition()

/**
* Reseizes a png image preserving transparency
* @return image resource
*/
private function resize_png_image($src_image, $width, $height)
{
Expand Down

0 comments on commit bda0368

Please sign in to comment.