Solved

Image Merge

Posted on 2007-11-13
7
1,189 Views
Last Modified: 2013-12-25
See the sample site. I need to "merge" the two images so that the picture of the boy is overlyed with the frame, with the inner part of the frame transparent & the outer part overcovering the "edge" portions of the picture.

At the moment, the Pticture of the boy is a jpg & the frame is a transparent gif. I can make the frame a jpg.

Is there a way to do this using the php image functions, or, if not, using something else?
0
Comment
Question by:Richard Korts
  • 4
  • 3
7 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 20275393
Try this

<?
 
 //soource image change accordingly
$image = imagecreatefromjpeg("myimage.jpg");      

//overlay image change accordingly
$overlay = imagecreatefromgif("imgoverlay.gif");

// Get X and Y size of existing image
$width = imagesx($image);
$height = imagesy($image);

// Get X and Y size of overlay
$width_o = imagesx($overlay);
$height_o = imagesy($overlay);


//Set X & Y  of overlay image
$new_width = $width_o ;
$new_height = ($height * ($new_width/$width)) ;


//Get destination coordinates
$dst_w = round(($width_o - $new_width)/2);
$dst_h = round(($height_o - $new_height)/2);
                             
//create new image
$image_resized = imagecreatetruecolor($width_o, $height_o);

//allocate color
$bg = imagecolorallocate($image_resized,255,255,255);

//fill image with white background
imagefilledrectangle($image_resized, 0, 0, $width_o , $height_o , $bg);

//copy source image to new image
imagecopyresampled($image_resized, $image, $dst_w, $dst_h , 0, 0, $new_width, $new_height, $width, $height);

//copy new image to overlay
imagecopy($image_resized, $overlay, 0,0,0,0,$width_o,$height_o);

imagejpeg($image_resized);

?>
0
 

Author Comment

by:Richard Korts
ID: 20275874
To steelseth12:

EXCELLENT! The only thing that didn't work from your php was: imagejpeg($image_resized);, that produces a bunch of gibberish characters in the browser. But no issue, I just saved it to a file & displayed the file in the browser.
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20275939
Glad i could help.
For imagejpeg() to work correctly i would need no other data to be outputed to the browser .... so the browser sees the file as an image ..

Normally the way i do it is put all the image creation code in a file by itself and then give it as a source to the <img> tag

i.e

<img src='create_image.php'>
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Richard Korts
ID: 20276044
To steelseth12,

So there is another thing I perhaps did NOT address in the original question. I'll put it here, in case you can answer directly, but what I need is to be able to "blot out" all aspects of the original image that are OUTSIDE the overlay frame. Think of the overlay as a picture frame, we want the excess outside the frame cut off. I can of course make the outside black or a light gray or ?? but any color other than white will look strange.

If you want I will start a new question & you can look for it. I'll put it in the same categories, etc.

Let me know.

Thanks,

rkorts
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20276158
Why not make the outsite of the border white
e.g https://filedb.experts-exchange.com/incoming/ee-stuff/5564-imgoverlay.gif
There are ways to calculate it in php but it requires alot of work and it will put unnecessary strain on the server.
0
 

Author Comment

by:Richard Korts
ID: 20276205
To steelseth12,

I got the image, thanks. But the inside is now black. Don't I have to change something in the php code?

Thanks,

rkorts
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20276239
Black ? No you shouldnt have to change anything ... it displays fine on my local host.

Thats strange ... try it with a png https://filedb.experts-exchange.com/incoming/ee-stuff/5565-imgoverlay.png

change

$overlay = imagecreatefromgif("imgoverlay.gif");
to
$overlay = imagecreatefrompng("imgoverlay.png");
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question