I want to add watermark on image when i upload in CakePHP. I found many components on Google but I don't know how to use them like ImageTool, Qimage etc. I don't know what value should pass to component function to add watermark. Could anybody help me.
Here is the ImageToolComponent::watermark()
function:-
public static function watermark($options = array()) {
$options = array_merge(array(
'afterCallbacks' => null,
'scale' => false,
'strech' => false,
'repeat' => false,
'watermark' => null,
'output' => null,
'input' => null,
'position' => 'center',
'compression' => 9,
'quality' => 100,
'chmod' => null,
'opacity' => 100
), $options);
// if output path (directories) doesn’t exist, try to make whole path
if (!self::createPath($options['output'])) {
return false;
}
$img = self::openImage($options['input']);
unset($options['input']);
if (empty($img)) {
return false;
}
$src_wm = self::openImage($options['watermark']);
unset($options['watermark']);
if (empty($src_wm)) {
return false;
}
// image size
$img_im_w = imagesx($img);
$img_im_h = imagesy($img);
// watermark size
$img_wm_w = imagesx($src_wm);
$img_wm_h = imagesy($src_wm);
if ($options['scale']) {
if ($options['strech']) {
$r = imagecopyresampled($img, $src_wm, 0, 0, 0, 0, $img_im_w, $img_im_h, $img_wm_w, $img_wm_h);
} else {
$x = 0;
$y = 0;
$w = $img_im_w;
$h = $img_im_h;
if (($img_im_w / $img_im_h) > ($img_wm_w / $img_wm_h)) {
$ratio = $img_im_h / $img_wm_h;
$w = $ratio * $img_wm_w;
$x = round(($img_im_w - $w) / 2);
} else {
$ratio = $img_im_w / $img_wm_w;
$h = $ratio * $img_wm_h;
$y = round(($img_im_h - $h) / 2);
}
$r = imagecopyresampled($img, $src_wm, $x, $y, 0, 0, $w, $h, $img_wm_w, $img_wm_h);
}
} else if ($options['repeat']) {
if (is_array($options['position'])) {
$options['position'] = 5;
}
switch ($options['position']) {
case 'top-left':
for ($y = 0; $y < $img_im_h; $y+=$img_wm_h) {
for ($x = 0; $x < $img_im_w; $x+=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
case 'top-right':
for ($y = 0; $y < $img_im_h; $y+=$img_wm_h) {
for ($x = $img_im_w; $x > -$img_wm_w; $x-=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
case 'bottom-right':
for ($y = $img_im_h; $y > -$img_wm_h; $y-=$img_wm_h) {
for ($x = $img_im_w; $x > -$img_wm_w; $x-=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
case 'bottom-left':
for ($y = $img_im_h; $y > -$img_wm_h; $y-=$img_wm_h) {
for ($x = 0; $x < $img_im_w; $x+=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
case 'center':
default:
$pos_x = -(($img_im_w % $img_wm_w) / 2);
$pos_y = -(($img_im_h % $img_wm_h) / 2);
for ($y = $pos_y; $y < $img_im_h; $y+=$img_wm_h) {
for ($x = $pos_x; $x < $img_im_w; $x+=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
}
} else {
// custom location
if (is_array($options['position'])) {
list($pos_x, $pos_y) = $options['position'];
} else {
// predefined location
switch ($options['position']) {
case 'top-left':
$pos_x = 0;
$pos_y = 0;
break;
case 'top-right':
$pos_x = $img_im_w - $img_wm_w;
$pos_y = 0;
break;
case 'bottom-right':
$pos_x = $img_im_w - $img_wm_w;
$pos_y = $img_im_h - $img_wm_h;
break;
case 'bottom-left':
$pos_x = 0;
$pos_y = $img_im_h - $img_wm_h;
break;
case 'center':
default:
$pos_x = round(($img_im_w - $img_wm_w) / 2);
$pos_y = round(($img_im_h - $img_wm_h) / 2);
break;
}
}
$r = self::imagecopymerge_alpha($img, $src_wm, $pos_x, $pos_y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
if (!$r) {
return false;
}
if (!self::afterCallbacks($img, $options['afterCallbacks'])) {
return false;
}
return self::saveImage($img, $options);
}
I don't know what parameter should i pass to watermark function.`