Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Jquery文件上传添加水印_Php_Jquery_Jquery Ui_Jquery File Upload - Fatal编程技术网

Php 使用Jquery文件上传添加水印

Php 使用Jquery文件上传添加水印,php,jquery,jquery-ui,jquery-file-upload,Php,Jquery,Jquery Ui,Jquery File Upload,我正在使用jquery文件上传,但我似乎无法找到在上传图像后添加水印的方法 有人有什么想法吗?您需要在服务器/php/UploadHandler.php文件中添加一些这样做的代码 我发现了这篇使用ajax在上传的图像上添加水印的精彩教程,请点击此链接 您可以在UploadHandler.php文件中的UploadHandler类中使用上述链接中的函数代码创建一个单独的函数,并根据需要使用它。您可以在上传文件后立即调用此函数。我知道这是一个老问题,但我必须找到解决方案,下面是我的示例。所以您需要

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


有人有什么想法吗?

您需要在
服务器/php/UploadHandler.php
文件中添加一些这样做的代码


我发现了这篇使用ajax在上传的图像上添加水印的精彩教程,请点击此链接


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

我知道这是一个老问题,但我必须找到解决方案,下面是我的示例。所以您需要修改UploadHandler.php,我是在函数gd\u create\u scaled\u image中修改的

加:

以及:

整个函数如下所示:

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

您需要在服务器上编写一些代码,以便在上载的图像上添加您选择的水印。由于您使用的是PHP,我建议您看看ImageMagick,因为它应该具备您所需要的功能,尽管任何图像处理库都应该能够处理这个简单的任务。添加(永久)水印不属于jQuery。您必须使用服务器端语言。例如PHP,您可以在这里找到一个运行示例:sitepoint.com/watermark-images-PHP如果您仍然希望使用CSS/HTML/Javascript创建水印,则可以使用一组使用CSS和Javascript样式的HTML层。但我想这不是我想要的解决办法,我理解。我只是不确定在现有代码中添加到哪里
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;
}
'image_library' => 0