| Coding Forum Problems with your code? Let's hear about it. |
08-06-2004, 12:16 PM
|
#1 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Pffffft
Posts: 8,857
Latest Blog: None
|
Adding one image to another image via script
Ok. I have a script to add reviews to my site. I also have to option to add screenshots while I'm adding reviews.
I'm wondering how I can add a little image with my logo/website url to the bottom right corner to prevent people from taking my screenshots.
Like this (see attachment) as opposed to:
http://www.must-have-software.net/sc...ox%200.8_1.jpg
|
|
|
08-06-2004, 12:35 PM
|
#2 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
Use ImageMagic (with PHP or other CGI interface). Available for both Windows and *nix platfroms:
http://www.imagemagick.org/
|
|
|
08-06-2004, 12:43 PM
|
#3 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
btw. if you have no root access to your server you may opt for PHP's function:
imagecopymerge ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct).
PHP needs to be compiled with the GD library of image functions for this to work. Most providers have that.
|
|
|
08-06-2004, 12:44 PM
|
#4 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Pffffft
Posts: 8,857
Latest Blog: None
|
Cool! Never knew that!  Thanks man
BTW: My screenshots are all different sizes, if I speicify:
int dst_x
int dst_y
won't it be weird on some images?
|
|
|
08-06-2004, 12:51 PM
|
#5 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
of course you'll have to test for sizes of both pictures (in code) before letting the code do the merge.
If destination is smaller than your "stamp" then you may resize the stamp, or change it's opacity, or both, or.. well, you get the idea.
|
|
|
08-06-2004, 12:59 PM
|
#6 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Pffffft
Posts: 8,857
Latest Blog: None
|
Hmmm, this seems comlicated...
How will I save the stamp image aswell with a low opacity? that won't work will it?
The only way I can think of to do this right is just applying the stamp to every screenshot that's taken...
|
|
|
08-06-2004, 01:08 PM
|
#7 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
gotta go for a bit, so for now just high level code for you.
Let's define some variables. I like using "stamp" as the word for source image.
(int) SW - stamp_width
(int) SH - stamp_height
(int) PW - width of destination picture
(int) PH - height of destination picture
dest_x = PW - SW -5
dest_y = PH - SH - 5
Using dest_x and dest_y will place your stamp in bottom right corner of the destination graphics, such that there will be a 5 pixel margin between the right edge of the destination picture and the right edge of the stamp. Similar effect will occur for the bottom edges of the dest. picture and stamp.
Of course, kep in mind that you may ned to resize the stamp.
As for opacity, the last parameter of the function mentioned above is actually opacity value of the stamp image.
$opacity = 25;
ImageCopyMerge ($img_des, $img_src, 0, 0, 0, 0, $size[0], $size[1], $opacity);
Will copy two images of the same sizez onto each other with 25% opacity applied.
These are the meanings and function of individual parameters, with positional ordering.
1 = destination image
2 = source image
3 = x co-ordinate of the destination image to copy to
4 = y co-ordinate of the destination image to copy to
5 = x co-ordinate of the source image to start the copy from
6 = y co-ordinate of the source image to start the copy from
7 = how much of the source image in width to copy
8 = how much of the source image in height to copy
9 = opacity value
<edit: mistype>
|
|
|
08-06-2004, 06:13 PM
|
#8 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Pffffft
Posts: 8,857
Latest Blog: None
|
That makes it a lot easier.
Do you have a messenger of any sort so we can talk more?
Thanks a ton!
Phil
|
|
|
08-06-2004, 06:37 PM
|
#9 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
wow!
I must warn you I barelly started with PHP, and I'm also an ugly s.o.b. 
|
|
|
08-06-2004, 07:21 PM
|
#10 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Pffffft
Posts: 8,857
Latest Blog: None
|
 That doesn't matter, just want to talk about how to get this code implemented on my site so my screenshots will start having that Logo/Url
|
|
|
08-06-2004, 08:33 PM
|
#11 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
OK, this code is somewhat improved over what I sent you, but mostly in terms of clarity.
<?php
header ("Content-type: image/jpeg");
//assign picture to show
$filename = 'sample.jpg';
$img_des = ImageCreateFromJpeg ($filename);
//assign stamp picture
$filename2 = 'a.jpg';
$img_src = ImageCreateFromJpeg ($filename2);
//calc the size of the stamp image
$size = getimagesize($filename2);
//set opacity value of the stamp
$opacity = 15;
//set the margins size for the stamp image. Our stamp image is 50 by 50 pixels
// make sure this value is >= stamp width
// or else the stamp's right edge will be chopped
//Here we will have 60-50=10px margin to the right of the stamp
$rightmargin = 60;
// make sure the value is >= stamp height
// or else the stamp's bottom edge will be chopped
//Here we will have 60-50=10px margin rom the bottom of the stamp
$bottommargin = 60;
//merge the two images
ImageCopyMerge($img_des, $img_src, imagesx($img_des)-$rightmargin, imagesy($img_des)-$bottommargin, 0, 0, $size[0], $size[1], $opacity);
//display the image in browser
ImageJPEG ($img_des);
//destroy memory allocated to image handles
ImageDestroy ($img_src);
ImageDestroy ($img_des);
?>
This code is not a marvel, but as I said, I'm still trying to get the feel of that PHP thingie.
Also, bear in mind that this code will NOT modify any of the pictures used in the process. The stamp is dynamically superimposed onto the destination image and the result streamed to the browser without savin the modifications on the server.
Important: the PHP on the server must be compiled with GD library. Otherwise will error out with "function not found" message.
Sample pictures will look like this:
<edit: clarified comments on margins>
|
|
|
08-06-2004, 08:40 PM
|
#12 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Pffffft
Posts: 8,857
Latest Blog: None
|
Thanks a TON man!
Quote:
|
Originally Posted by littleFella
Also, bear in mind that this code will NOT modify any of the pictures used in the process. The stamp is dynamically superimposed onto the destination image and the result streamed to the browser without savin the modifications on the server.
|
So the images really dont have that stamp if lookin at them through FTP? And if people save the image, it'll have the stamp, correct?
|
|
|
08-06-2004, 08:44 PM
|
#13 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
Quote:
|
Originally Posted by Pimpen 2010
So the images really dont have that stamp if lookin at them through FTP? And if people save the image, it'll have the stamp, correct?
|
That's correct. In order to achieve that you would have to use ImageMagic functions in the following manner:
1. merge pictures
2. delete/move unstamped destination picture.
ImageMagic is available for Linux (of course) and is free.. You need to use command line functions in PHP to be able to do it.
|
|
|
08-06-2004, 08:46 PM
|
#14 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Pffffft
Posts: 8,857
Latest Blog: None
|
Cool. You have been a big help :cool:
Now I just need to get my coder to impliment this 
|
|
|
08-06-2004, 08:48 PM
|
#15 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
That's pretty much implemented, just ask him to turn it into a function. Don't be shy when it's done. Brag all you want. I'd be glad to see the final result.
|
|
|
08-06-2004, 08:53 PM
|
#16 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Pffffft
Posts: 8,857
Latest Blog: None
|
 I'll make sure to post here.
I'm making a contributors section on my links page, would you like me to add a link to your site? for the wonderful help?
|
|
|
08-06-2004, 09:03 PM
|
#17 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
Quote:
|
Originally Posted by Pimpen 2010
 I'll make sure to post here.
I'm making a contributors section on my links page, would you like me to add a link to your site? for the wonderful help?
|
1. I am still scrathing my head here. The idea being this:
If you run that code dynamically everytime someone looks at the picture then it means more resources required by the server. So I thought that perhaps I would play with it some more and do it this way:
- get a picture from iuser (via browser upload)
- modify it immediatelly after the upload to include the stamp.
- delete (or move elsewhere) the originally uploaded image
I can't promise I'll do it anytime soon though. Still, at the very least you got what you got now.
Also, it appears that PHP has this function:
imagejpeg ( resource image [, string filename [, int quality]])
It allows to save the image to a file, which would solve your ftp worry, and no need to install ImageMagic.
2. I have to revamp my site a bit (it's attrocious right now), and I will gladly accept the offer. Thanks.
|
|
|
08-06-2004, 09:17 PM
|
#18 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Pffffft
Posts: 8,857
Latest Blog: None
|
NP, Thank you for the help.
No rush for that. I'll be adding them manually for now, until the one you gave me gets put in the coding, then I'll switch to the one you're talking about now.
|
|
|
08-06-2004, 10:55 PM
|
#19 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
OK, got your ftp situation somewhat solved:
I am including only a porti | |