Jump to content

combining php scripts and gettin dynamic watermarks to work


lisa-b

Recommended Posts

Hi there,

I wonder if anyone can help me with this: I want to set up a page that shows a random image from a directory when loaded and to have a random piece of text from a txt file overlay it, similar to the watermark effect, but with randomly selected overlaying text.

What I'm thinking of doing is using a generic "on the fly" php text watermarking script and replacing the watermarking text with a piece of php code that calls a line of text from a flat file, like so: replace

var $waterMarkText = "COPYRIGHT";

with

var $waterMarkText = " $textfile ="random.txt"; $items = file("$textfile"); $item = rand(0, sizeof($items)-1); echo $items[$item];"; 

Does any body know whether this can work? And if not does anybody know a better way to achieve this effect? The reason I'm asking here rather than testing it is because I havent managed to get a watermarking script to function yet.. something wrong with the .htaccess I think. It looks like:

RewriteEngine on
RewriteBase /my/path/to/images 
RewriteRule (.*jpg) /path-to/watermark.php?image=$1

I have no idea if that's right or wrong? Could anybody tell me? I can post the watermark script code if there's anybody who's had experience with text watermark scripts and would like to help.

I know that's a long post but I'm totally stuck, any help would be very appreciated.

Thankyou,

Lisa

Hello Lisa,

I'm not sure if I can help you completely. I have played with generating dynamic text, and creating an image with it using true type fonts I place in the "fonts" folder next to the script. Perhaps the following will inspire something? It probably wouldn't be hard to change the alpha of the text and add the image. Let me know what you come up with as I am interested.

 

<?php
function showFonts($text, $dir) {
$fontInt = 1;
foreach (glob($dir . "*.ttf") as $filename) {
	$im = @imagecreate(450, 70) or die("Cannot Initialize new GD image stream");
	$background_color = imagecolorallocate($im, 255, 255, 255);
	$text_color = imagecolorallocate($im, 0, 0, 0);
	$font = $filename;
	imagettftext($im, 30, 0, 10, 40, $text_color, $font, $text);
	$imgFile = "fonts/font" . $fontInt . ".png";
	imagepng($im, $imgFile);
	imagedestroy($im);
	echo "<p><img src=$imgFile><br />$filename</p>\n\n";
	$fontInt++;
}
}

showFonts("This is way cool", "fonts/");
?> 

 

This script creates an image for each font in the fonts folder. Make sure your directory permissions are set to 777.

 

Hope this helps!

Hi, thanks for your reply, I'm hoping the scripts I already have will be enough to accomplish what I want What I have at the moment is a folder of jpg images which I want to display on a page with one selected randomly on each page load but before it appears it passes through the watermark php script which leaves a piece of text on the image and the text is selected at random from a .txt file. Does that sound possible to you? I'm very inexperienced with php. the .htaccess code above is supposed to revert the url domain.com/watermark/watermark,php?image=DSC123.jpg back to domain.com/watermark/DSC123.jpg so the image can be displayed using a very simple random image script. But it seems to stop the image displaying at all at the moment (regardless of url).

here's the code I have:

Imagemarker.php

<?php
class waterMarker
{
var $imagePath = "/path/to/image/directory/"; 
var $waterMarkText = "THE TEXT"; 
var $signature = "version 1.0"; 
var $font = "/path/to/font/arial.ttf";
var $size = 14; 
var $angle = 45; 
var $hSpacing = 120; 
var $vSpacing = 120;
var $imageResource = "/home/wonderfu/public_html/watermark/"; 
var $imageType="jpg"; 
var $shadow = false; 
var $antialiased = true; 

function mark()
{
                list($width, $height, $type, $attr) = getimagesize($this->imagePath); //get basic properties of the image file

	switch ($this->imageType)
	{

	        case "png":
	        $createProc = "imagecreatefrompng";
	        $outputProc = "imagepng";
	        break;
	        case "gif";
	        $createProc = "imagecreatefromgif";
                        $outputProc = "imagepng";
                        break;
	        case "bmp";
	        $createProc = "imagecreatefrombmp";
                        $outputProc = "imagebmp";
                        break;
	        case "jpeg":
		case "jpg":
	        $createProc = "imagecreatefromjpeg";
                        $outputProc = "imagejpeg";
                        break;
                        
	}

	$im = $createProc($this->imagePath); 
	$grey = imagecolorallocate($im, 180, 180, 180); 
	$shadowColor = imagecolorallocate($im, 130, 130, 130); 
	if (!$this->antialiased)

	{
		$grey *= -1; //grey = grey * -1
		$shadowColor *= -1; //shadowColor = shadowColor * -1
	}


        
        for ($x=20; $x<$width; $x+=$this->hSpacing)

        {
                for ($y=20; $y<$width; $y+=$this->vSpacing)
                {
                        if ($this->shadow) imagettftext($im, $this->size, $this->angle, $x+1, $y+1, $shadowColor, $this->font, $this->waterMarkText); //draw shadow text over image
                        imagettftext($im, $this->size, $this->angle, $x, $y, $grey, $this->font, $this->waterMarkText);
                }
        }
        $this->imageResource= $outputProc($im); 
        
}
}
?>

watermark.php

<?php
include_once("imageMarker.php");

$imagePath = $_REQUEST['code'];
//echo $imagePath;
$imark = new waterMarker();
$imark->waterMarkText = "bdtemplates";
$imark->imagePath = $imagePath;
$imark->font="/absolute/path/to/font/arial.ttf";
$imark->size = 14;
$imark->hSpacing=110;
$imark->vSpacing=110;
$imark->shadow=true;
$imark->angle = 45;
header("Content-type: image/jpg");
$imark->mark(); 
echo $imark->imageResource;
?>

.htaccess

RewriteEngine on
RewriteBase /my/path/to/images 
RewriteRule (.*jpg) /path-to/watermark.php?image=$1

Is this enough to do what I want? Will I be able to modify imagemarker.php to display text from a .txt file in the way in the way I described above? I wanted a more direct way of doing it but it appears there's no established technique of how to do it to be found and I dont have the skill to create one I'm sure.

Hmmm. Once you get the script working, adding the random "text" from a txt file would not be a problem. I would look for a ".csv parser" class to get a comma delimeted txt file into an array, which can be randomly chosen from. Dispite the annoying advertising, I bet you could find one at phpclasses.org. Once you get the text data into the array, you could randomly pick one like this:

 

<?php
//above this somewhere, $textArray gets filled with txt file contents
//for testing, let's just put some stuff in the array below:
$textArray = array("Yay I'm random.", "Holy random!", "Randarific", "Randtacular!", "Random here");

//get random text
$randomNum = rand(0, (count($textArray) - 1));
$randomText = $textArray[$randomNum];

//see the results
echo $randomText;

?>

 

On the rest of it, I would not have time to test it until way later. I would make sure that your arial or whatever font is available, and all the paths are set correctly. Also make sure that your folder permissions is set at 777, wherever the new images are being stored (if they are being stored somewhere). I wish you luck on this.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.