Be a Supporter!

Php: Starting With Gd

  • 1,518 Views
  • 14 Replies
New Topic Respond to this Topic
cherries
cherries
  • Member since: Jun. 7, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Php: Starting With Gd Jun. 27th, 2006 @ 06:02 PM Reply

AS: Main

There are many ways to use GD in PHP, you could use an image as a background or create it by scratch.
Im going to show you how to make this.
So we start the PHP file how we normally start PHP files :P
with:
<?

but since this is an image we need to change the type from php to gif, png, or jpg.
we do that be using the header() function.

<?
header('Content-type: image/gif');

pretty self explanitory right? good.
The way I created it was through an image. So I created a variable $im_url which held the url to my background I wanted to use.
So I added this variable beneath the header:

<?
header('Content-type: image/gif');
$im_url = "http://www.bellybuttontreasure.com/test.g
if";

GD has functions which allow you to use an image as the background, they are called imagecreatefrom*extension*.
so for example: imagecreatefromgif, imagecreatefromjpeg, imagecreatefrompng, ect..

because the image I am using is a *.gif file I will be using imagecreatefromgif.
so the code so far:

<?
header('Content-type: image/gif');
$im_url = "http://www.bellybuttontreasure.com/test.g
if";
$image = imagecreatefromgif($im_url);

In GD it doesn't know colors, you have to set colors yourself using the imagecolorallocate function.
The function works with both RGB(255, 255, 255) and Hex(#FFFFFF) codes.
But with Hex colors you have to add an "0x" before the actual numbers.
So for white I would do:

$white = imagecolorallocate($image, 0xFF(the red), 0xFF(the green), 0xFF(the blue));

and now the RGB:

$white = imagecolorallocate($image, 255, 255, 255);

I personally like the second way better, but whatever you want to use..
and because the image Im using only needs the color white I will just stop at that.
I just set a couple variables to make the image cooler :P

$ip = $_SERVER['REMOTE_ADDR']; // ip address(duh)
$br = $_SERVER['HTTP_USER_AGENT']; //browser

and put it all together with what I had so far.

<?
header('Content-type: image/gif');
$im_url = "http://www.bellybuttontreasure.com/test.g
if";
$image = imagecreatefromgif("$im_url");

$white = imagecolorallocate($image, 255, 255, 255);
$ip = $_SERVER['REMOTE_ADDR'];
$br = $_SERVER['HTTP_USER_AGENT'];

now onto writing text onto the image, to do this we use PHP's imagestring function.
using image string:

the first paremeter is the link to the image in this case we used $image.
the next number represents the font size 1-5 for the default font.
the next number is the x.
the next is the y.
then we have our string "I am cool :P".
and lastly the color($white).

so first up I am going to write the ip to the image:

imagestring($image, 2, 86, 5, "$ip", $white);

it takes a lot of playing with the X and Y cooridinates to get them just right.
and now the browser:

imagestring($image, 1, 74, 23, "$br", $white);

The script is nearly done, and so far yours should look similar to this:

<?
header('Content-type: image/gif');
$im_url = "http://www.bellybuttontreasure.com/test.g
if";
$image = imagecreatefromgif("$im_url");

$white = imagecolorallocate($image, 255, 255, 255);
$ip = $_SERVER['REMOTE_ADDR'];
$br = $_SERVER['HTTP_USER_AGENT'];

imagestring($image, 2, 86, 5, "$ip", $white);
imagestring($image, 1, 74, 23, "$br", $white);

Now we just need to actually create the image :D
using the imagegif function we can do that.
the image gif function only has one parameter, which is the link to the image($image).
so:

imagegif($image);

and lastly we must say good bye to all the image data ;-(

imagedestroy($image);

and that ends the code!
so the whole code now:

<?
header('Content-type: image/gif');
$im_url = "http://www.bellybuttontreasure.com/test.g
if";
$image = imagecreatefromgif("$im_url");

$white = imagecolorallocate($image, 255, 255, 255);
$ip = $_SERVER['REMOTE_ADDR'];
$br = $_SERVER['HTTP_USER_AGENT'];

imagestring($image, 2, 86, 5, "$ip", $white);
imagestring($image, 1, 74, 23, "$br", $white);

imagegif($image);
imagedestroy($image);

Thats it :P

More on GD
imagearc
imagefill
imageline
imagesetpixel
php.net/GD

<3

cherries
cherries
  • Member since: Jun. 7, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to Php: Starting With Gd Jun. 27th, 2006 @ 06:08 PM Reply

lol I forgot the ending ?>

mynamewontfitin
mynamewontfitin
  • Member since: Sep. 1, 2003
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to Php: Starting With Gd Jun. 27th, 2006 @ 06:27 PM Reply

Sweet man! Definately going to try this.

eWhabs
eWhabs
  • Member since: May. 4, 2006
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to Php: Starting With Gd Jun. 27th, 2006 @ 09:58 PM Reply

NICE!! Thats EXACTLY what I needed !


-Disregard females (fuck bitches)
-Acquire currency (get money)
-Ignite cannabis frequently (smoke sum with your homies)

BBS Signature
JimmyDallas
JimmyDallas
  • Member since: May. 17, 2003
  • Offline.
Forum Stats
Member
Level 31
Gamer
Response to Php: Starting With Gd Jun. 27th, 2006 @ 10:19 PM Reply

this is actually easy for me to even understand.
me likes.


[PHP: Main]-[AVGN Club]
"You know a dame is classy when she got some jiggle in her assy." - Unknown

BBS Signature
elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Php: Starting With Gd Jun. 28th, 2006 @ 08:03 AM Reply

Hrmm, I'd recommend outputting PNGs instead of GIFs ;) They give much, much better quality.
With that said, you can do really shiny things with GD ^^


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature
NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Starting With Gd Jun. 28th, 2006 @ 03:30 PM Reply

At 6/27/06 06:02 PM, -cherries- wrote: AS: Main

lmao

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Starting With Gd Jun. 28th, 2006 @ 03:39 PM Reply

A question about font sizes: you said you can choose from 1 to 5. But is there a way of expanding these?

Like, creating bigger fonts, which have got like 30 pixels? Or 50?

This is what I put though, but I need bigger fonts:

<?
header("Content-type: image/png");
$string = "Hello my friend! Whassup!";
$len = strlen($string);
$im = imagecreatefrompng("../pmgrounds/images/gr
aphics/title.png");
$orange = imagecolorallocate($im, 250, 5, 100);
$px = (imagesx($im) - 7.5 * strlen($string)) / 20;
imagestring($im, 100, $px, 10, $string, $orange);
imagepng($im);
imagedestroy($im);
?>

I used PNG though, I think it is much better than PNG. Even though PNG does not have Index transparency...

ludnix
ludnix
  • Member since: Feb. 4, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Php: Starting With Gd Jun. 29th, 2006 @ 12:45 AM Reply

What do you mean by index transparency?

It has pretty alpha channels!

Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Php: Starting With Gd Jun. 29th, 2006 @ 01:01 AM Reply

At 6/28/06 03:39 PM, Nino_JoJ wrote: I used PNG though, I think it is much better than PNG. Even though PNG does not have Index transparency...

Yeah, of course PNG is better then PNG. how could nobody know that?

elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Php: Starting With Gd Jun. 29th, 2006 @ 05:55 AM Reply

At 6/28/06 03:39 PM, Nino_JoJ wrote: A question about font sizes: you said you can choose from 1 to 5. But is there a way of expanding these?

Use imagettftext() ;) It allows you to load a font and specify the size like you would in Word...


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature
cherries
cherries
  • Member since: Jun. 7, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to Php: Starting With Gd Jun. 29th, 2006 @ 12:34 PM Reply

At 6/28/06 03:30 PM, Nino_JoJ wrote:
At 6/27/06 06:02 PM, -cherries- wrote: AS: Main
lmao

I thought nobody had noticed that :P

Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Php: Starting With Gd Jun. 29th, 2006 @ 12:40 PM Reply

At 6/29/06 12:34 PM, -cherries- wrote:
At 6/28/06 03:30 PM, Nino_JoJ wrote:
At 6/27/06 06:02 PM, -cherries- wrote: AS: Main
lmao
I thought nobody had noticed that :P

I just did when I came into this thread now (before I got down here). I was just going to say something.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Starting With Gd Jun. 30th, 2006 @ 09:59 AM Reply

At 6/29/06 01:01 AM, Craige wrote:
At 6/28/06 03:39 PM, Nino_JoJ wrote: I used PNG though, I think it is much better than PNG. Even though PNG does not have Index transparency...
Yeah, of course PNG is better then PNG. how could nobody know that?

My bad ;)

At 6/29/06 05:55 AM, elbekko wrote:
At 6/28/06 03:39 PM, Nino_JoJ wrote: A question about font sizes: you said you can choose from 1 to 5. But is there a way of expanding these?
Use imagettftext() ;) It allows you to load a font and specify the size like you would in Word...

Thanks for that.

imagettftext -- Write text to the image using TrueType fonts
Description
array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )

image

The image resource. See imagecreatetruecolor().
size

The font size. Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2).
angle

The angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text.
x

The coordinates given by x and y will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the imagestring(), where x and y define the upper-left corner of the first character. For example, "top left" is 0, 0.
y

The y-ordinate. This sets the position of the fonts baseline, not the very bottom of the character.
color

The color index. Using the negative of a color index has the effect of turning off antialiasing. See imagecolorallocate().
fontfile

The path to the TrueType font you wish to use.

Yea!

At 6/29/06 12:34 PM, -cherries- wrote:
At 6/28/06 03:30 PM, Nino_JoJ wrote:
At 6/27/06 06:02 PM, -cherries- wrote: AS: Main
lmao
I thought nobody had noticed that :P

I did, but not instantly!