note 68145 added to function.imagecopyresampled
| From: | caioatdaisukidotcomdotbr at osu1 dot php dot net | Date: | Fri, 14 Jul 2006 21:51:04 +0000 |
| Subject: | note 68145 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-114796@lists.php.net to get a copy of this message | ||
Ok guys, the ultimate image merging tool, enjoy:
function watermark($sourcefile, $watermarkfiles, $destination, $quality=90) {
/*
$sourcefile = Filename of the picture to be watermarked.
$watermarkfile = files to add. Each is: array ( 'filename' => filename ,
'position' => position to show(array), 'resample' => new dimension(array)
)
You can omit any except filename
$destination = jpg or png file to output (jpg if source is jpg, png if source is png). Send
blank to direct output
Sample:
$batch = array ( array ( 'filename' => "watermark.png",
'position' => array(100,0),
),
array ( 'filename' => "brilho.png",
'position' => array(200,200),
'resample' => array(30,30)
)
);
watermark("picture.jpg", $batch, "", 90); <-- will add both images in
bath on top of picture, preserving transparency
*/
$ih = getimagesize($sourcefile);
$ispng = $ih[2] == 3;
if ($ispng)
$sourcefile_id = imagecreatefrompng($sourcefile);
else
$sourcefile_id = imagecreatefromjpeg($sourcefile);
$sourcefile_width=imageSX($sourcefile_id);
$sourcefile_height=imageSY($sourcefile_id);
foreach ($watermarkfiles as $x => $watermarkfile) {
$watermarkfile_id = imagecreatefrompng($watermarkfile['filename']);
imageAlphaBlending($watermarkfile_id, false);
imageSaveAlpha($watermarkfile_id, true);
$watermarkfile_width=imageSX($watermarkfile_id);
$watermarkfile_height=imageSY($watermarkfile_id);
// resample?
if (isset($watermarkfile['resample'])) {
$im_dest = imagecreatetruecolor ($watermarkfile['resample'][0],
$watermarkfile['resample'][1]);
imagealphablending($im_dest, false);
imagecopyresampled($im_dest, $watermarkfile_id, 0, 0, 0, 0,
$watermarkfile['resample'][0], $watermarkfile['resample'][1],
$watermarkfile_width, $watermarkfile_height);
imagesavealpha($im_dest, true);
imagedestroy($watermarkfile_id);
$watermarkfile_id = $im_dest;
$watermarkfile_width = $watermarkfile['resample'][0];
$watermarkfile_height = $watermarkfile['resample'][1];
}
// position ? if none given, centered
if (isset($watermarkfile['position']))
list($dest_x,$dest_y) = $watermarkfile['position'];
else {
$dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 ); // centered
$dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 ); // centered
}
imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0, $watermarkfile_width,
$watermarkfile_height);
imagedestroy($watermarkfile_id);
}
if ($ispng)
imagepng($sourcefile_id,$destination);
else
imagejpeg($sourcefile_id,$destination,$quality);
imagedestroy($sourcefile_id);
}
----
Server IP: 66.163.161.117
Probable Submitter: 201.11.83.122
----
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.imagecopyresampled.php
Edit -- http://master.php.net/note/edit/68145
Del: integrated -- http://master.php.net/note/delete/68145/integrated
Del: useless -- http://master.php.net/note/delete/68145/useless
Del: bad code -- http://master.php.net/note/delete/68145/bad+code
Del: spam -- http://master.php.net/note/delete/68145/spam
Del: non-english -- http://master.php.net/note/delete/68145/non-english
Del: in docs -- http://master.php.net/note/delete/68145/in+docs
Del: other reasons-- http://master.php.net/note/delete/68145
Reject -- http://master.php.net/note/reject/68145
Search -- http://master.php.net/manage/user-notes.php