PHP 5.3 : Generating Images with PHP

in #utopian-io8 years ago (edited)

Drawing Lines
To draw a line in an image use the imageline() function. A line has both start and end points, so you
must give imageline() two sets of coordinates. For example:
imageline( $myImage, 10, 10, 100, 100, $myColor );

In this simple example you draw a straight line between two points, then output the resulting image
to the browser. Save the following code as line.php in your document root folder:

<?php
$myImage = imagecreate( 200, 100 );
$myGray = imagecolorallocate( $myImage, 204, 204, 204 );
$myBlack = imagecolorallocate( $myImage, 0, 0, 0 );
imageline( $myImage, 15, 35, 120, 60, $myBlack );
header( “Content-type: image/png” );
imagepng( $myImage );
imagedestroy( $myImage );
?>

Now run the script by opening its URL in your Web browser. Figure 17-5 shows the output.

If you’re running PHP on Windows and you receive a Call to undefined function
imagecreate() error message, you need to enable the GD2 extension. To do this, edit your php.ini
file and remove the semicolon from the start of the following line:

;extension=php_gd2.dll

Remember to restart your server after you’ve made this change.

These steps are normally unnecessary on other systems such as Linux and Mac OS X, because the GD2
library is usually bundled with the PHP engine on these systems. For more information see
http://www.php.net/manual/en/image.installation.php.

How It Works
The script first creates a new blank image with imagecreate()and stores the image resource in
$myImage. Then it allocates two colors — a gray and a black. Because the gray is allocated first, it is
used as the background color of the image.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.

Hi, you plagiarised this from Beginning PHP 5.3 by Matt Doyle, and that is completely unacceptable.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.05
TRX 0.32
JST 0.082
BTC 65717.73
ETH 1776.52
USDT 1.00
SBD 0.42