使用Jquery File Upload添加水印

时间:2014-10-15 22:11:53

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

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

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

有人有什么想法吗?

3 个答案:

答案 0 :(得分:1)

您需要在server/php/UploadHandler.php文件

中添加一些代码

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

答案 1 :(得分: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 :(得分:0)

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

您可以使用UPDATE machine_log as next inner join machine_log as prev on next.machine_id=prev.machine_id SET prev.date_end = next.date_start WHERE next.record_id != prev.record_id UploadHandlerUploadHandler.php中的上述链接中的功能代码创建单独的函数,并根据需要使用它。您可以在文件上传后立即调用此函数。