Ok, so here is our code.
watermark.php
- Code: Select all
// set the image url
$image = "/path/to/your/image/";
$watermark = "Watermark String";
list($width, $height) = getimagesize($image);
$new_img = imagecreatefrompng($image);
$axisy = $height - 10;
$axisx = 10;
// create text colour in rgb, currently white
$fcolour = imagecolorallocate($new_img, 255, 255, 255);
imagestring($new_img, 1, $axisx, $axisy, $watermark, $fcolour);
header("Content-Type: image/png");
imagepng($new_img);
imagedestroy($new_img);
return $new_img;
Ok, firstly, we assign the $image variable to the path to your image you want to watermark. Then, we create a string containing what you want your watermark to show. Then, we assign the variables $width and $height to a getimagesize of the image.
php.net wrote: array getimagesize ( string $filename [, array &$imageinfo ] )
The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondant HTTP content type.
getimagesize() can also return some more information in imageinfo parameter.
Note: Note that JPC and JP2 are capable of having components with different bit depths. In this case, the value for "bits" is the highest bit depth encountered. Also, JP2 files may contain multiple JPEG 2000 codestreams. In this case, getimagesize() returns the values for the first codestream it encounters in the root of the file.
Note: The information about icons are retreived from the icon with the highest bitrate.
Then we use gd to create a new image from the image you specified in the $image variable. After that we assign the x and y axis variables depending on where we want the text to show. Here is what you want the variables to be depending on where your text is.
Top Left:
$axisy = 10;
$axisx = 10;
Bottom Left:
$axisy = $height - 10;
$axisx = 10;
Top Right:
$axisy = 10;
$axisx = $width - 10;
Bottom Right:
$axisy = $height - 10;
$axisx = $width - 10;
The we create the $fcolour variable, this is the colour of the text in rgb. 255, 255, 255 is white. 0, 0, 0 is black. See here for more rgb colours.
Then we write the watermark string into the image. Firstly, we assign the image we want to write it into. Then what font we want to use (this is 1 through 5). Next we set it's x and y co-ordinates, these are the $axisy and $axisx variables we assigned earlier. Finally, we specify the string we want to write and the colour of the text (again, we assigned these to variables earlier).
Then this bit:
- Code: Select all
header("Content-Type: image/png");
imagepng($new_img);
imagedestroy($new_img);
return $new_img;
We tell the browser that the output is an image. Then we write out the image that we have created using imagepng. Then we destroy the image to free memory associated with it. And finally, we return the image.
Thankyou for reading and I hope this helped.
