【问题标题】:add watermark image to uploaded image为上传的图片添加水印图片
【发布时间】:2016-02-14 15:18:53
【问题描述】:

我正在使用此代码将 png 图像作为水印添加到上传的图像,但结果不是图像并且不想使用 header() 我希望代码继续执行其他 php 查询而无需导航到另一个页面来显示图像。图片已上传但没有水印,并且 header() 不会发布任何图片,只是一个灰色的小方块

$path = "../large/";
$num = substr(md5(mt_rand(1,9999999999)),0,9);    
$new_name = $path.$num.".jpg";
$image = $num.".jpg";
move_uploaded_file($img_tmpname,$new_name);
$image = imagecreatefromjpeg($new_name);
$logoImage = imagecreatefrompng("images/watermark.png");
imagealphablending($logoImage, true);
$imageWidth=imagesx($image);
$imageHeight=imagesy($image); 
$logoWidth=imagesx($logoImage);
$logoHeight=imagesy($logoImage);

imagecopy(
  // destination
  $image,
  // source
  $logoImage,
  // destination x and y
  $imageWidth-$logoWidth, $imageHeight-$logoHeight,
  // source x and y
  0, 0,
  // width and height of the area of the source to copy
  $logoWidth, $logoHeight);

// Set type of image and send the output
header("Content-type: image/png");
imagePng($image);

// Release memory
imageDestroy($image);
imageDestroy($imageLogo);

【问题讨论】:

  • imagePng 应该是 imagepngimageDestroy 应该是 imagedestroy
  • 路径images/watermark.png~首先该文件是否存在于该位置,其次您是否尝试使用该图像的完整路径?
  • 是的,但我遇到了同样的问题
  • 用这个作为最酷的例子。 stackoverflow.com/questions/8829674/…

标签: php image upload watermark


【解决方案1】:

我使用下面的代码对此进行了测试,结果正常。显然,我使用了与我的测试系统相关的路径,但希望它会有所帮助。

上传和处理上传文件时最重要且经常被遗忘的事情之一是表单的enctype - 所以我以我的测试表单为例。

如果您想保存图像并显示带有水印的图像,请使用 imagepng 函数两次,一次带有文件名,另一次不带。

<form method='post' action='/test/so/wtrmarkimg.php' enctype='multipart/form-data'>
    <h1>Image uploader - Watermark</h1>
    <input type='file' name='image' />
    <input type='submit' value='Submit' />
</form>

<?php

    #$path = "../large/";

    $path='c:/temp/';/* output path for images generated */
    $watermarksrc=realpath( 'c:/wwwroot/images/watermark.png' );    

    if( isset( $_FILES['image'] ) ){

        $img_tmpname=$_FILES['image']['tmp_name'];


        $num = substr( md5( mt_rand( 1,9999999999 ) ),0,9);    
        $new_name = $path.$num.".jpg";
        $image = $num.".jpg";

        if( move_uploaded_file( $img_tmpname, $new_name ) ){

            $image = imagecreatefromjpeg( $new_name );
            $logoImage = imagecreatefrompng( $watermarksrc );
            imagealphablending( $logoImage, true );

            $imageWidth=imagesx($image);
            $imageHeight=imagesy($image); 
            $logoWidth=imagesx($logoImage);
            $logoHeight=imagesy($logoImage);

            imagecopy(
              $image,
              $logoImage,
              $imageWidth-$logoWidth, $imageHeight-$logoHeight,
              0, 0,
              $logoWidth, $logoHeight );

            // Set type of image and send the output
            header("Content-type: image/png");
            imagepng( $image );/*display image with watermark */
            @imagepng( $image, $new_name );/* save image with watermark */

            // Release memory
            imagedestroy( $image );
            imagedestroy( $imageLogo );
        }
    } else {
        echo "ERROR";
        print_r($_FILES);   
    }
?>

【讨论】:

  • 没有错误,也没有发布图片。原图刚刚移动到路径中。
  • 您能展示一下您修改了代码的内容和方式吗?
  • 稍作调整,现在工作正常,非常感谢 $path='../large/';/* 生成图像的输出路径 */ $watermarksrc=realpath( 'images/watermark.png' );
猜你喜欢
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多