note 67249 added to function.imagecopymerge
| From: | yonzie at gmail dot com | Date: | Tue, 06 Jun 2006 01:39:35 +0000 |
| Subject: | note 67249 added to function.imagecopymerge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-113340@lists.php.net to get a copy of this message | ||
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
----
Server IP: 195.184.96.15
Probable Submitter: 83.72.194.85
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.imagecopymerge.php
Edit -- http://master.php.net/note/edit/67249
Del: integrated -- http://master.php.net/note/delete/67249/integrated
Del: useless -- http://master.php.net/note/delete/67249/useless
Del: bad code -- http://master.php.net/note/delete/67249/bad+code
Del: spam -- http://master.php.net/note/delete/67249/spam
Del: non-english -- http://master.php.net/note/delete/67249/non-english
Del: in docs -- http://master.php.net/note/delete/67249/in+docs
Del: other reasons-- http://master.php.net/note/delete/67249
Reject -- http://master.php.net/note/reject/67249
Search -- http://master.php.net/manage/user-notes.php