Jump to content

PHP Image Generation

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Hey Guy's

Iv no idea how to do this at all, but I'm looking for a piece of code that generates an image.

Like the Facebook/MySpace profile badges, it generates a dynamic name ect.

But for the moment I would just like a piece of code, that generated a small piece of text, e.g 'Hello World'

If then like it to be able to save it into a directory, so it can be embedded into other websites.

Thanks guys.

#2
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
To create images in PHP you'll want to use GD. Though I have used it in the past Im not quite familiar to GD anymore but it's not too hard, an example of a tutorial: PHP tutorial: Create images . Just look it up on google like 'PHP GD tutorial'.

#3
madison12

madison12

    Newbie

  • Members
  • Pip
  • 1 posts
The easiest way to understand how to create an image is by looking at some sample code.

<?php
$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, "thesitewizard.com",
$text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );

header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>

The above code creates a 200x80 PNG image with a blue background and yellow text. It can be called from within your web page simply by referencing the php file. For example, if the PHP file that contains the above code is called myimage.php, then the HTML code to invoke it can simply be:

<img src="myimpage.php" alt="Image created by a PHP script" width="200" height="80">

web development services