Merging images...

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

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Merging images...

Post by Todd_Z »

What is the bare minimal coding that I can use to merge two images together? I'd rather not use some extensive package to do this - Basically I want to have a function that looks like this:

Code: Select all

<?php
function mergeImages ( $img1, $img2, $x, $y ) {
    $newImage = ????;
    ---
    return $newImage;
}
?>
Where $x and $y are the locations for the second image to be placed on $img1.
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

Post by idotcom »

I would recommend using GD functions. Its easy and you can create one simple and fairly small php function to process the task.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Ok... so now I know how to merge images... but what happens if someone right clicks and presses view image... it won't still be watermarked... a fix?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

gd modifies the image. right clicking wont change the image you sent to them
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

I see... how do i do a chdir with a space in the filename? (related issue)
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Todd_Z wrote:I see... how do i do a chdir with a space in the filename? (related issue)
have you tried?
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Whoops - I was using %20 for spaces by accident... didn't catch that til just now - thanks
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Alright... Now the implementation of merging images and the whole javascript thing... http://www.acdrifter.com/Foo/?page=Revi ... e=Returner

So i have that page, and there is a javascript gallery... I need to put a watermark on each one of those images... but the thing is that they are loaded through javascript... The current one and the two in front and in back of it... so anyways... how do i do this? Thanks in advance
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

watermark the images, save them, then load them
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Can i do that on the fly? or should i make a script that goes through each image and watermarks each for me to be used later?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

your choice, whatever works best for your situation


to do it on the fly you would do something like this
<img src="watermarker.php?image=foo.jpg">

then

Code: Select all

$image = $_GET&#1111;'image'];

$type = end(explode('.', $image)); // get file ext.
header('Content-type: image/'.$type); // send proper mime header

// then load $image into gd, watermark it, and output it directly to the browser
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Ahhh thank you rehfeld.
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

Post by idotcom »

I just wanted to post this just incase...

The way I use GD is by creating a simple php file that has all the gd code and in my case, grabs the image from the database with the row ID. This can also be used if the images are stored in folders.

So... I simply feed the php file the id in the image src=""

like: src="myimage.php?image_id=12343&watermark=no&size=1"

if the image comes from a database use image(row) id and if its in a folder use the path: http://www.domain.com/myimages/image_name.jpg

like: src="myimage.php?image_source=http://www.domain.com/myimages/image_na ... =no&size=1"

in my php file:

if(image_id)
{
grab the image
}

if(watermark=="no")
{
dont watermark
}

else
{
yes watermark
}

if(size==1)
{
make the image this preset size
}

if(size==2)
{
make the image this size instead
}

doing this seems to give me a lot of freedom. set as many conditions you want and just tell php what to do with the current image in the image path.

Hope this helps.
Post Reply