Jump to content

How Do You Directly Embed An Image Into Another PHP GD Image?


webster08

Recommended Posts

I am trying to create a completely PHP GD image based advert, so far I am able to add shapes, lines, and text. The next step is to directly embed an image(s) into my base PHP GD image advert. Here's where the problem comes in; I'm not sure how to go about doing this. I don't want to create a separate php file to watermark the main image with external images. I want to be able to directly embed images into the base image. Can anyone advise as to what would be way to accomplish this; my code is below?

 

<?php

// set the HTTP header type to PNG
header("Content-type: image/png"); 

// set the width and height of the new image in pixels
$width = 1000;
$height = 1000;

// create a pointer to a new true colour image
$im = imagecreatetruecolor($width, $height);

// text
$mainTitle="2011 Chrystler Town And Country LX";

// font path
$font = 'arial.ttf';

// sets background to white
$white = ImageColorAllocate($im, 255, 255, 255); 
ImageFillToBorder($im, 0, 0, $white, $white);

// define a black colour
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 525, 100, $white);

// display text on image
imagettftext($im, 35, 0, 100, 70, $black, $font, $mainTitle);

// make a new line and add it to the image
ImageLine($im, 0, 100, 1000, 100, $black);

// draw rectangle
ImageRectangle($im, 0, 0, 999, 999, $black);

// add image

$pic1="2011-Chysler-Town-And-Country-pic1.jpg";

imagecreatefromjpeg($im, $pic1, 0, 0, 640, 480, 50);

// send the new PNG image to the browser
ImagePNG($im);

// destroy the reference pointer to the image in memory to free up resources
imagepng($im);
ImageDestroy($im);

?>

 

This is the piece of code that I know is not correct; this is where I need help at.

 

// add image

$pic1="2011-Chysler-Town-And-Country-pic1.jpg";

imagecreatefromjpeg($im, $pic1, 0, 0, 640, 480, 50);

Link to comment
Share on other sites

Hey AlexWD, thank you for the reply. Yeah... I looked at that function, but I am having problems figuring out how to incorporate that into my current code. The example in the manual is rather vague; when it comes to inserting an image into your current image. Would I need to create a totally different php file to do this or can I incorporate something like the example (that the manual gives) into my current code?

Link to comment
Share on other sites

First you'll need to create the image resource for the image you want to merge onto your existing GD image:

 

$pic1="2011-Chysler-Town-And-Country-pic1.jpg";
$pic = imagecreatefromjpeg($pic1);

 

Now you can merge your two resources: $im and $pic using imagecopymerge:

 

imagecopymerge($im, $pic, 0, 0, 0, 0, 640, 480, 50);

 

Look at the manual to see what the parameters mean to get exactly what you're after.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.