Senin, 09 Februari 2009

Watermark Images on the Fly in PHP

Let's suppose that you've just been given five screenshots from the biggest upcoming video game of the year. You want to share your screenshots, but credit has to be given where credit is due -- after all, you haven't been sweet-talking public relations representatives for fun, right?

The solution is to watermark the images. But the standard watermarking procedure of editing the image in a photo-editing application is time consuming. PHP offers a far better solution.

PHP 4+ and GD 2.0+ represent a powerful combination when it comes to creating or altering images dynamically. In this tutorial, we'll look at some of the GD and standard PHP functions as we watermark images on the fly.

The Script
The script, just 13 lines in length, begins with a header() call to tell the Web browser that we are going to output an image in JPEG format. Without this line, a page calling the script via the HTML tag will receive a broken image and, if the script is accessed directly, the user will see the image data as plain text.

Specifying the content type is unnecessary with most PHP scripts because "text/html" is the default content type for PHP scripts.

header('content-type: image/jpeg');

Now that the browser is ready for an image, we can start to calculate the mathematics of the watermark placement and load the images.

The Watermark
A file titled 'watermark.png' (notice use of the PNG format; GD 2.0+ has removed compatibility with GIF images) should be located in the same directory as the script. If not, change the file path in the following function call, which will load the watermark image. The file should be a PNG-8 format file, not PNG-24. There's a bug in the current version of GD, which doesn't support PNG-24 correctly.

$watermark = imagecreatefrompng('watermark.png');

Our operations used later in the script will need to know the exact height and width, in pixels, of this watermark image. These dimensions will be used to place the watermark in a relative position on the image. Let's use imagesx() and imagesy() (not GD functions, but standard PHP functions):

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

The Image
Now, we'll create from the JPEG file an image that we want to have watermarked, and set it as a variable, $image. By using the imagecreatefromjpeg() function, we load the file into the PHP script using the GD library. This gives us the opportunity to manipulate and/or output the image using our script.

$image = imagecreatefromjpeg($_GET['src']);


---------------------------------


Related :

UsingCrystalReports6
VerifyUserEmailAddressPHP-1
VerifyUserEmailAddressPHP-2
viewinformationvb2005
WatermarkImagesFlyPHP-1
WatermarkImagesFlyPHP-2
WhatAretheIssues
What-isMySQL
WhatSQLServerExpress
writefileinvb2005
WritingFileDialogBox
SimpleDatabasevb6
SQLServer2005fromVisualBasic6
SQLServer2005withPHP
SQLServerSecurityGuidelines
TransferringFiles
UsingCrystalReports6