note 67249 deleted from function.imagecopymerge by bjori
| From: | bjori@php.net | Date: | Fri, 27 Oct 2006 10:26:58 +0000 |
| Subject: | note 67249 deleted from function.imagecopymerge by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119175@lists.php.net to get a copy of this message | ||
Note Submitter: yonzie at gmail dot com
----
Watermarking for the complete idiot (that would be me, then)
My task: Image gallery, automatic resizing, watermarking, etc.
I was having huge trouble getting transparency right, but it has finally clicked. This is what
worked for me:
First, I created a picture in Photoshop with a transparent background.
I then copied my logo (except for the background) onto this new transparent picture.
"Image > Mode > Indexed color" and "File > Save for Web". Ignore all
the stuff about dithering, just make sure there's a checkmark in "Transparency" and
PNG-8 is selected. Save and upload.
// Set up
$file = "path/to/picture.jpg"; // Picture to resize
$mark = "path/to/watermark.png"; // The watermark to use.
$newfile = "path/to/new/picture.jpg"; // The resized and watermarked picture that
we'll end up with. Make sure this directory is writeable.
list( $sourcewidth, $sourceheight ) = getimagesize($file); // Size of source photo for resizing
list( $markwidth, $markheight ) = getimagesize($mark); // Size of source photo for resizing
$width = 800; // Width of picture after resizing
$height = 600; // Height of picture after resizing
// Load pictures
$source = imagecreatefromjpeg( $file ); // Source photo
$watermark = imagecreatefrompng( $mark ); // This is our watermark.
// Resizing
$resized = imagecreatetruecolor( $width, $height ); // Create memory space for resized picture
imagecopyresampled( $resized, $source, 0, 0, 0, 0, $width, $height, $sourcewidth, $sourceheight );
// Resize the source picture with a proper algorithm to avoid jaggies. Use imagezopyresized if you
are in a hurry.
// Watermarking
imagecopymerge( $resized, $watermark, 10, $bigheight-$markheight-10, 0, 0, $markwidth, $markheight,
65 ); // Note 65% transparency of the NON-TRANSPARENT parts of the picture. This will put the
watermark at the bottom-left corner of the picture, 10px from the edge.
// Saving
imagejpeg( $resized, "$newfile" ) or die ( 'Could not save picture! Please check
permissions.' ); // Save file on disk
// Cleaning up
imagedestroy( $watermark ); // Clear memory
imagedestroy( $source ); // Clear memory
imagedestroy( $resized ); // Clear memory