summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-29 23:25:28 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-29 23:25:28 +0000
commit4ab53d145bec440529a6304f470eb952a40eec6b (patch)
tree77c534b43cff4398c065f5eb2bdf140c3520b5db
parent4439ca94a9850261d7f27d06c8327eafe0ff1ea3 (diff)
Create Image::composite() and implement it in GD, ImageMagick and GraphicsMagick drivers.
-rw-r--r--kohana/libraries/Image.php29
-rw-r--r--kohana/libraries/drivers/Image.php7
-rw-r--r--kohana/libraries/drivers/Image/GD.php22
-rw-r--r--kohana/libraries/drivers/Image/GraphicsMagick.php10
-rw-r--r--kohana/libraries/drivers/Image/ImageMagick.php10
5 files changed, 78 insertions, 0 deletions
diff --git a/kohana/libraries/Image.php b/kohana/libraries/Image.php
index bd723a27..6fcce090 100644
--- a/kohana/libraries/Image.php
+++ b/kohana/libraries/Image.php
@@ -255,6 +255,35 @@ class Image_Core {
}
/**
+ * Overlay a second image on top of this one.
+ *
+ * @throws Kohana_Exception
+ * @param string $overlay_file path to an image file
+ * @param integer $x x offset for the overlay
+ * @param integer $y y offset for the overlay
+ * @param integer $transparency transparency percent
+ */
+ public function composite($overlay_file, $x, $y, $transparency)
+ {
+ $image_info = getimagesize($overlay_file);
+
+ // Check to make sure the image type is allowed
+ if ( ! isset(Image::$allowed_types[$image_info[2]]))
+ throw new Kohana_Exception('image.type_not_allowed', $overlay_file);
+
+ $this->actions['composite'] = array
+ (
+ 'overlay_file' => $overlay_file,
+ 'mime' => $image_info['mime'],
+ 'x' => $x,
+ 'y' => $y,
+ 'transparency' => $transparency
+ );
+
+ return $this;
+ }
+
+ /**
* Flip an image horizontally or vertically.
*
* @throws Kohana_Exception
diff --git a/kohana/libraries/drivers/Image.php b/kohana/libraries/drivers/Image.php
index 0f900c74..33f0f940 100644
--- a/kohana/libraries/drivers/Image.php
+++ b/kohana/libraries/drivers/Image.php
@@ -146,4 +146,11 @@ abstract class Image_Driver {
*/
abstract public function sharpen($amount);
+ /**
+ * Overlay a second image. Valid properties are: overlay_file, mime, x, y and transparency.
+ *
+ * @return boolean
+ */
+ abstract public function composite($properties);
+
} // End Image Driver \ No newline at end of file
diff --git a/kohana/libraries/drivers/Image/GD.php b/kohana/libraries/drivers/Image/GD.php
index c3789b6d..24657c56 100644
--- a/kohana/libraries/drivers/Image/GD.php
+++ b/kohana/libraries/drivers/Image/GD.php
@@ -331,6 +331,28 @@ class Image_GD_Driver extends Image_Driver {
return imageconvolution($this->tmp_image, $matrix, $amount - 8, 0);
}
+ public function composite($properties)
+ {
+ switch($properties['mime'])
+ {
+ case "image/jpeg":
+ $overlay_img = imagecreatefromjpeg($properties['overlay_file']);
+ break;
+
+ case "image/gif":
+ $overlay_img = imagecreatefromgif($properties['overlay_file']);
+ break;
+
+ case "image/png":
+ $overlay_img = imagecreatefrompng($properties['overlay_file']);
+ break;
+ }
+
+ imagecopymerge($this->tmp_image, $overlay_img, $properties['x'], $properties['y'], 0, 0, imagesx($overlay_img), imagesy($overlay_img), $properties['transparency']);
+ imagedestroy($overlay_img);
+ return TRUE;
+ }
+
protected function properties()
{
return array(imagesx($this->tmp_image), imagesy($this->tmp_image));
diff --git a/kohana/libraries/drivers/Image/GraphicsMagick.php b/kohana/libraries/drivers/Image/GraphicsMagick.php
index 8840eb80..a8bc4d9b 100644
--- a/kohana/libraries/drivers/Image/GraphicsMagick.php
+++ b/kohana/libraries/drivers/Image/GraphicsMagick.php
@@ -203,6 +203,16 @@ class Image_GraphicsMagick_Driver extends Image_Driver {
return TRUE;
}
+ public function composite($properties)
+ {
+ if ($error = exec(escapeshellcmd($this->dir.'gm'.$this->ext.' composite').' -geometry ' . escapeshellarg('+'.$properties['x'].'+'.$properties['y']).' -dissolve '.escapeshellarg($properties['transparency']).' '.escapeshellarg($properties['overlay_file']).' '.$this->cmd_image.' '.$this->cmd_image))
+ {
+ $this->errors[] = $error;
+ return FALSE;
+ }
+ return TRUE;
+ }
+
protected function properties()
{
return array_slice(getimagesize($this->tmp_image), 0, 2, FALSE);
diff --git a/kohana/libraries/drivers/Image/ImageMagick.php b/kohana/libraries/drivers/Image/ImageMagick.php
index 3397e1d0..497dd8c4 100644
--- a/kohana/libraries/drivers/Image/ImageMagick.php
+++ b/kohana/libraries/drivers/Image/ImageMagick.php
@@ -204,6 +204,16 @@ class Image_ImageMagick_Driver extends Image_Driver {
return TRUE;
}
+ public function composite($properties)
+ {
+ if ($error = exec(escapeshellcmd($this->dir.'composite'.$this->ext).' -geometry ' . escapeshellarg('+'.$properties['x'].'+'.$properties['y']).' -dissolve '.escapeshellarg($properties['transparency']).' '.escapeshellarg($properties['overlay_file']).' '.$this->cmd_image.' '.$this->cmd_image))
+ {
+ $this->errors[] = $error;
+ return FALSE;
+ }
+ return TRUE;
+ }
+
protected function properties()
{
return array_slice(getimagesize($this->tmp_image), 0, 2, FALSE);