PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Mon May 23, 2005 2:51 pm
Hi all
I'm merging two images in one, but problem remains with watermark size. How to make size of watermark to be 20% of whole image?
Here is code for making watermark
Code: Select all
<?php
header('content-type: image/jpeg');
$slika = "../uploadedfiles/apartmani/2386339050/261004velikaRABPOG~1.JPG";
$watermark = imagecreatefrompng('../slike/strana/watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
//$image = imagecreatefromjpeg($_GET['src']);
$image = imagecreatefromjpeg($slika);
//$size = getimagesize($_GET['src']);
$size = getimagesize($slika);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 45);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
Skara
Forum Regular
Posts: 703 Joined: Sat Mar 12, 2005 7:13 pm
Location: US
Post
by Skara » Mon May 23, 2005 8:52 pm
Code: Select all
$watermark_x = floor($orig_image_x * 0.20);
$watermark_y = floor($orig_image_y * 0.20);
Problem with that is that if the image is long and skinny, so will the watermark be.
To fix that,
Code: Select all
$scale = min($orig_image_x * 0.20 / $orig_image_y * 0.20);
$watermark_x = floor($scale * $orig_image_x);
$watermark_y = floor($scale * $orig_image_y);
Pretty sure that'll work.
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Tue May 24, 2005 11:26 am
Thank you for reply, but I'm confused.
Where should I put these lines?
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Tue May 24, 2005 12:54 pm
I can resize watermark image at x percent of whole image, but I always get dark rectangle around watermark?
Can somebody help me with this?
Here is code
Code: Select all
<?php
header('content-type: image/jpeg');
$watermark = '../slike/strana/watermark.png';
$slika = "../uploadedfiles/apartmani/2386339050/261004velikaRABPOG~1.JPG";
$posto = 0.2;
$size = getimagesize($slika); // Read the size
$width = $size[0] * $posto;
$height = $size[1] * $posto;
list($width_orig, $height_orig) = getimagesize($watermark);
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($watermark);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$watermark = $image_p;
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($slika);
$size = getimagesize($slika);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 45);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
Skara
Forum Regular
Posts: 703 Joined: Sat Mar 12, 2005 7:13 pm
Location: US
Post
by Skara » Tue May 24, 2005 1:29 pm
I think your dark rectangle comes from this:
no clue why you put that there. Your code's a bit confusing with all the "image"s.
For the above code, replace this:
Code: Select all
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
#flip here
//$image = imagecreatefromjpeg($_GET['src']);
$image = imagecreatefromjpeg($slika);
//$size = getimagesize($_GET['src']);
$size = getimagesize($slika);
with this:
Code: Select all
$image = imagecreatefromjpeg($slika);
//$image = imagecreatefromjpeg($_GET['src']);
$size = getimagesize($slika);
//$size = getimagesize($_GET['src']);
#flipped here
$scale = min($size[0] * 0.20 / $size[1] * 0.20);
$watermark_width = floor($scale * $size[0]);
$watermark_height = floor($scale * $size[1]);
$watermark_image = imagecreatetruecolor($watermark_width, $watermark_height);