【问题标题】:Add watermark using Jquery File Upload使用 Jquery File Upload 添加水印
【发布时间】:2014-12-11 03:32:03
【问题描述】:

我正在使用 jquery 文件上传,但我似乎无法在图片上传后找到添加水印的方法。

http://blueimp.github.io/jQuery-File-Upload/

有人有什么想法吗?

【问题讨论】:

  • 您需要在服务器上编写一些代码来为上传的图像添加您选择的水印。由于您使用的是 PHP,我建议您看一下 ImageMagick,因为它应该有您正在寻找的东西,尽管任何图像处理库都应该能够处理这个简单的任务。
  • 添加(永久)水印不属于jQuery。您必须使用服务器端语言。例如 PHP,您将在此处找到一个运行示例:sitepoint.com/watermark-images-php 如果您仍想使用 CSS/HTML/Javascript 创建水印,您可以使用一组带有 css 和 javascript 样式的 html 层。但我想这不是理想的解决方案。
  • 我明白这一点。我只是不确定在哪里将它添加到现有代码中

标签: php jquery jquery-ui jquery-file-upload


【解决方案1】:

我知道这是一个老问题,但我必须找到解决方案,这是我的示例。所以你需要修改UploadHandler.php,我在function gd_create_scaled_image

中做了

添加:

$stamp = imagecreatefrompng('../../img/stamp.png'); //path to your watermark file
$im = $src_func($file_path);
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

和:

$success = imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp)) && $write_func($im, $file_path, $image_quality)&& imagecopyresampled($new_img, $src_img, $dst_x, $dst_y, 0, 0, $new_width, $new_height, $img_width, $img_height) && $write_func($new_img, $new_file_path, $image_quality);
$this->gd_set_image_object($file_path, $new_img);
return $success;

整个函数如下所示:

protected function gd_create_scaled_image($file_name, $version, $options) {
    if (!function_exists('imagecreatetruecolor')) {
        error_log('Function not found: imagecreatetruecolor');
        return false;
    }
    list($file_path, $new_file_path) =
        $this->get_scaled_image_file_paths($file_name, $version);
    $type = strtolower(substr(strrchr($file_name, '.'), 1));
    switch ($type) {
        case 'jpg':
        case 'jpeg':
            $src_func = 'imagecreatefromjpeg';
            $write_func = 'imagejpeg';
            $image_quality = isset($options['jpeg_quality']) ?
                $options['jpeg_quality'] : 75;
            break;
        case 'gif':
            $src_func = 'imagecreatefromgif';
            $write_func = 'imagegif';
            $image_quality = null;
            break;
        case 'png':
            $src_func = 'imagecreatefrompng';
            $write_func = 'imagepng';
            $image_quality = isset($options['png_quality']) ?
                $options['png_quality'] : 9;
            break;
        default:
            return false;
    }

    //watermark
    $stamp = imagecreatefrompng('../../img/stamp.png'); //path to your watermark file
    $im = $src_func($file_path);
    $marge_right = 10;
    $marge_bottom = 10;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);

    $src_img = $this->gd_get_image_object(
        $file_path,
        $src_func,
        !empty($options['no_cache'])
    );
    $image_oriented = false;
    if (!empty($options['auto_orient']) && $this->gd_orient_image(
            $file_path,
            $src_img
        )) {
        $image_oriented = true;
        $src_img = $this->gd_get_image_object(
            $file_path,
            $src_func
        );
    }
    $max_width = $img_width = imagesx($src_img);
    $max_height = $img_height = imagesy($src_img);
    if (!empty($options['max_width'])) {
        $max_width = $options['max_width'];
    }
    if (!empty($options['max_height'])) {
        $max_height = $options['max_height'];
    }
    $scale = min(
        $max_width / $img_width,
        $max_height / $img_height
    );
    if ($scale >= 1) {
        if ($image_oriented) {
            return $write_func($src_img, $new_file_path, $image_quality);
        }
        if ($file_path !== $new_file_path) {
            return copy($file_path, $new_file_path);
        }
        return true;
    }
    if (empty($options['crop'])) {
        $new_width = $img_width * $scale;
        $new_height = $img_height * $scale;
        $dst_x = 0;
        $dst_y = 0;
        $new_img = imagecreatetruecolor($new_width, $new_height);
    } else {
        if (($img_width / $img_height) >= ($max_width / $max_height)) {
            $new_width = $img_width / ($img_height / $max_height);
            $new_height = $max_height;
        } else {
            $new_width = $max_width;
            $new_height = $img_height / ($img_width / $max_width);
        }
        $dst_x = 0 - ($new_width - $max_width) / 2;
        $dst_y = 0 - ($new_height - $max_height) / 2;
        $new_img = imagecreatetruecolor($max_width, $max_height);
    }
    // Handle transparency in GIF and PNG images:
    switch ($type) {
        case 'gif':
        case 'png':
            imagecolortransparent($new_img, imagecolorallocate($new_img, 0, 0, 0));
        case 'png':
            imagealphablending($new_img, false);
            imagesavealpha($new_img, true);
            break;
    }
    //watermark
    $success = imagecopy(
        $im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp)
    ) && $write_func($im, $file_path, $image_quality)&& imagecopyresampled(
        $new_img, $src_img, $dst_x, $dst_y, 0, 0, $new_width, $new_height, $img_width, $img_height
    ) && $write_func($new_img, $new_file_path, $image_quality);
    $this->gd_set_image_object($file_path, $new_img);
    return $success;
}

我希望这对某人有所帮助。

编辑:

您还必须通过更改同一文件来强制使用 GD 库:

'image_library' => 0

【讨论】:

    【解决方案2】:

    我发现这个很棒的教程可以使用 ajax 在上传的图片上添加水印,请点击此链接Ajax file upload and watermark

    您可以使用UploadHandler.php 文件中UploadHandler 类中的上述链接中的函数代码创建一个单独的函数,并根据需要使用它。您可以在文件上传后立即调用此函数。

    【讨论】:

      【解决方案3】:

      您需要在server/php/UploadHandler.php 文件中添加一些执行此操作的代码

      https://github.com/blueimp/jQuery-File-Upload/blob/master/server/php/UploadHandler.php

      【讨论】:

        猜你喜欢
        • 2019-01-06
        • 2014-07-14
        • 1970-01-01
        • 2013-11-12
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 2014-08-26
        • 2018-06-01
        相关资源
        最近更新 更多