如何在CakePHP中为图像添加水印

时间:2015-06-22 17:17:10

标签: php image-processing watermark

我想在CakePHP上传时在图像上添加水印。我在Google上发现了许多组件,但我不知道如何使用它们,如ImageTool,Qimage等。我不知道应该传递给组件函数添加水印的值。可以有人帮助我。

以下是ImageToolComponent::watermark()功能: -

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);
}

我不知道应该将哪个参数传递给水印功能。

0 个答案:

没有答案