php添加水印-标头问题

时间:2018-08-14 13:29:58

标签: php html watermark

我阅读了许多类似的问题和答案,包括关于Stackoverflow的问题,但是我找不到当前问题的解决方案。

我正在研究一个PHP脚本,该脚本在运行时添加了水印。我从php.net(http://us3.php.net/manual/en/function.imagecopymerge.php)获得了初始脚本。

脚本作为独立脚本使用时,就像一个超级按钮。仅显示图像时(浏览器仅显示图像)。但是,当我从具有嵌入式php脚本的html调用php函数时,它不显示图像,但返回:“警告:无法修改标题信息-已由(输出...行61)发送的标题” 61是行号其中:标头(“内容类型:images / png”)。

有什么方法可以将它与嵌入HTML的php中的水印函数调用一起使用?

谢谢你, 克里斯。

正在工作,但仅显示图片

<?php

/******************************************************************/

function watermark($sourcefile, $watermarkfile) {

    #
    # $sourcefile = Filename of the picture to be watermarked.
    # $watermarkfile = Filename of the 24-bit PNG watermark file.
    #
    //Get the resource ids of the pictures
    $watermarkfile_id = imagecreatefrompng($watermarkfile);

    imageAlphaBlending($watermarkfile_id, false);
    imageSaveAlpha($watermarkfile_id, true);

    $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));

    switch($fileType) {
        case('gif'):
            $sourcefile_id = imagecreatefromgif($sourcefile);
            break;

        case('png'):
            $sourcefile_id = imagecreatefrompng($sourcefile);
            break;

        default:
            $sourcefile_id = imagecreatefromjpeg($sourcefile);
    }

    //Get the sizes of both pix   
    $sourcefile_width=imageSX($sourcefile_id);
    $sourcefile_height=imageSY($sourcefile_id);
    $watermarkfile_width=imageSX($watermarkfile_id);
    $watermarkfile_height=imageSY($watermarkfile_id);

    $dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 );
    $dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 );

    // if a gif, we have to upsample it to a truecolor image
    if($fileType == 'gif') {
        // create an empty truecolor container
        $tempimage = imagecreatetruecolor($sourcefile_width, $sourcefile_height);

        // copy the 8-bit gif into the truecolor image
        imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0, $sourcefile_width, $sourcefile_height);

        // copy the source_id int
        $sourcefile_id = $tempimage;
    }

    imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0, $watermarkfile_width, $watermarkfile_height);

    //Create a jpeg out of the modified picture
    switch($fileType) {

        // remember we don't need gif any more, so we use only png or jpeg.
        // See the upsaple code immediately above to see how we handle gifs
        case('png'):
            header('Content-type: image/png');
            imagepng ($sourcefile_id);
            break;

        default:
            header('Content-type: image/jpg');
            imagejpeg ($sourcefile_id);
    }           

    imagedestroy($sourcefile_id);
    imagedestroy($watermarkfile_id);

} 

    $watermarkfile_nathalie="./images/cartes/watermark.png";
    $image_to_watermark="./images/cartes/YV1_1ereDeCouverture2.png";
    $image_watermarked=watermark($image_to_watermark, $watermarkfile_nathalie);


?>

与html集成时不起作用

<?php

/******************************************************************/

function watermark($sourcefile, $watermarkfile) {

    #
    # $sourcefile = Filename of the picture to be watermarked.
    # $watermarkfile = Filename of the 24-bit PNG watermark file.
    #
    //Get the resource ids of the pictures
    $watermarkfile_id = imagecreatefrompng($watermarkfile);

    imageAlphaBlending($watermarkfile_id, false);
    imageSaveAlpha($watermarkfile_id, true);

    $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));

    switch($fileType) {
        case('gif'):
            $sourcefile_id = imagecreatefromgif($sourcefile);
            break;

        case('png'):
            $sourcefile_id = imagecreatefrompng($sourcefile);
            break;

        default:
            $sourcefile_id = imagecreatefromjpeg($sourcefile);
    }

    //Get the sizes of both pix   
    $sourcefile_width=imageSX($sourcefile_id);
    $sourcefile_height=imageSY($sourcefile_id);
    $watermarkfile_width=imageSX($watermarkfile_id);
    $watermarkfile_height=imageSY($watermarkfile_id);

    $dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 );
    $dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 );

    // if a gif, we have to upsample it to a truecolor image
    if($fileType == 'gif') {
        // create an empty truecolor container
        $tempimage = imagecreatetruecolor($sourcefile_width, $sourcefile_height);

        // copy the 8-bit gif into the truecolor image
        imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0, $sourcefile_width, $sourcefile_height);

        // copy the source_id int
        $sourcefile_id = $tempimage;
    }

    imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0, $watermarkfile_width, $watermarkfile_height);

    //Create a jpeg out of the modified picture
    switch($fileType) {

        // remember we don't need gif any more, so we use only png or jpeg.
        // See the upsaple code immediately above to see how we handle gifs
        case('png'):
            header('Content-type: image/png');
            imagepng ($sourcefile_id);
            break;

        default:
            header('Content-type: image/jpg');
            imagejpeg ($sourcefile_id);
    }           

    imagedestroy($sourcefile_id);
    imagedestroy($watermarkfile_id);

} 



?>
<html>
<head>
</head>
<body>
<p>
Hello world
<br />
<?php
    $watermarkfile_nathalie="./images/cartes/watermark.png";
    $image_to_watermark="./images/cartes/YV1_1ereDeCouverture2.png";
    $image_watermarked=watermark($image_to_watermark, $watermarkfile_nathalie);
    echo "<img src = \"";
    echo $image_watermarked;
    echo "\" />";
?>
</p>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您必须将src的{​​{1}}设置为返回图像的PHP,例如

img

答案 1 :(得分:0)

感谢altrisi,我设法做到了。

这是解决方案。

我创建了一个文件“ watermark.php”,该文件从我要为图像加水印的页面中调用。

watermark.php

<?php

/******************************************************************/

function watermark($sourcefile, $watermarkfile) {

    #
    # $sourcefile = Filename of the picture to be watermarked.
    # $watermarkfile = Filename of the 24-bit PNG watermark file.
    #
    //Get the resource ids of the pictures
    $watermarkfile_id = imagecreatefrompng($watermarkfile);

    imageAlphaBlending($watermarkfile_id, false);
    imageSaveAlpha($watermarkfile_id, true);

    $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));

    switch($fileType) {
        case('gif'):
            $sourcefile_id = imagecreatefromgif($sourcefile);
            break;

        case('png'):
            $sourcefile_id = imagecreatefrompng($sourcefile);
            break;

        default:
            $sourcefile_id = imagecreatefromjpeg($sourcefile);
    }

    //Get the sizes of both pix   
    $sourcefile_width=imageSX($sourcefile_id);
    $sourcefile_height=imageSY($sourcefile_id);
    $watermarkfile_width=imageSX($watermarkfile_id);
    $watermarkfile_height=imageSY($watermarkfile_id);

    $dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 );
    $dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 );

    // if a gif, we have to upsample it to a truecolor image
    if($fileType == 'gif') {
        // create an empty truecolor container
        $tempimage = imagecreatetruecolor($sourcefile_width, $sourcefile_height);

        // copy the 8-bit gif into the truecolor image
        imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0, $sourcefile_width, $sourcefile_height);

        // copy the source_id int
        $sourcefile_id = $tempimage;
    }

    imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0, $watermarkfile_width, $watermarkfile_height);

    //Create a jpeg out of the modified picture
    switch($fileType) {

        // remember we don't need gif any more, so we use only png or jpeg.
        // See the upsaple code immediately above to see how we handle gifs
        case('png'):
            header('Content-type: image/png');
            imagepng ($sourcefile_id);
            break;

        default:
            header('Content-type: image/jpg');
            imagejpeg ($sourcefile_id);
    }           

    imagedestroy($sourcefile_id);
    imagedestroy($watermarkfile_id);

}
// Constants
$path_to_images="./image/";
$default_image="YV1_1ereDeCouverture2.png";
$default_watermark="watermark.png";

// Read passing arguments: image to watermark and watermark image
// 1. image to watermark
$img_to_watermark=$_GET["img"];
if($img_to_watermark) {
    /* Validate that img exists */

    /* Set path */
    $img_to_watermark=$path_to_images . $img_to_watermark;
}
else {
    /* Display default image */
    $img_to_watermark=$path_to_images . $default_image;
}
// 2. watermark image
$watermarkfile=$_GET["watermark"];
if($watermarkfile) {
    /* Validate that watermark img exists */

    /* Set path */
    $watermarkfile=$path_to_images . $watermarkfile;
}
else {
    /* Use default watermak image */
    $watermarkfile=$path_to_images . $default_watermark;
}


    $image_watermarked=watermark($img_to_watermark, $watermarkfile);

    return $image_watermarked;
?>

然后调用名为watermark.php的php文件:

<html>
<head>
</head>
<body>
<p>
Hello world
<br />
<?php
    $watermarkfile_nathalie="watermark.png";
    $image_to_watermark="postureDeLaGraineALaFleur_recto.png";
    echo "<img src = \"./watermark.php?img=" . $image_to_watermark . "&watermark=" . $watermarkfile_nathalie . "\" />\n";
?>
<br />
What's up now?
</p>
</body>
</html>

希望这会有所帮助! 不要犹豫,发布您的增强功能:-)。

克里斯。