开发者

Watermark png keeps showing current URL

I'm working on a script to put watermarks on images uploaded by users. Because every user wants his own user name on his picture, I decided to make a transparent PNG first with the name of the user. After that I use a simple watermark technique to merge the PNG and uploaded files together.

I got the script working, but it keeps showing me my current address every time a PNG is created.

This is the code so far:

 <?开发者_Go百科php
    session_start();

    $username = $_SESSION['login'];
    $filename = "watermarks/$username.png";

    if (file_exists($filename)) {
        exit;
    } elseif ($filename == "undefined") {
        exit;
    }else{


    header("Content-type: image/png"); //Picture Format
    header("Expires: Mon, 01 Jul 2003 00:00:00 GMT"); // Past date
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Consitnuously modified
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Pragma: no-cache"); // NO CACHE

    /*image generation code*/
    //create Image of size 350px x 75px
    $bg = imagecreatetruecolor(500, 100);

    //This will make it transparent
    imagesavealpha($bg, true);

    $trans_colour = imagecolorallocatealpha($bg, 0, 0, 0, 127);
    imagefill($bg, 0, 0, $trans_colour);

    //Text to be written
    $text = $username;

    // White text
    $white = imagecolorallocate($bg, 255, 255, 255);
    // Grey Text
    $grey = imagecolorallocate($bg, 128, 128, 128);
    // Black Text
    $black = imagecolorallocate($bg, 0,0,0);

    $font = 'fonts/LiberationSans.ttf'; //path to font you want to use
    $fontsize = 20; //size of font


    //Writes text to the image using fonts using FreeType 2
    imagettftext($bg, $fontsize, 0, 125, 50, $black, $font, $text);

    imagettftext($bg, $fontsize, 0, 127, 52, $white, $font, $text);




    //Create image
    header( "Content-type: image/png" );
    //imagepng($bg);

    $save = $filename;
    imagepng($bg, $save, 0, NULL);


    //destroy image
    imagedestroy($bg);

    }
    ?>

I'm sure I missed something, but I can't figure out what.

My second problem is that I can not figure out how to get the text to the center and get rid of the white space around the text.

Watermark png keeps showing current URL

Please download the image to see what I mean.

Thanks in advance for your help.


You're not outputting the image. Most browsers will just display the current url when the headers indicate an image, but no data is sent.

Notice how you're providing a filename to imagepng()? If you read the PHP manual on imagepng() it says it will either output the image if no filename to save to is present, OR save the image to the filename specified.

You need to either remove all the arguments after $bg to output the image, or after imagedestroy($bg); you need to open the output file at $save and re-output it.

Something like the following will stream a file to the output:

<?php
$fp = fopen($save, 'r');
while(!feof($fp)) {
    print fread($fp, 1024);
    ob_flush();
    flush();
}
fclose($fp);


// Merge watermark upon the original image

imagecopy($image, $watermark, (($w/2)-($ww/2)), $h-$wh, 0, 0, $ww, $wh);

For centering the watermark

refer this

http://www.nzwhost.com/article/simple-php-watermark

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜