In this tutorial we will learn to learn to create simple image (i.e simple rectangular)
  • Problems

    This line enable URL Rewriting . It must be enabled via the RewriteEngine directive! and if your .htaccess file is going to use rewrite rules, you should always include this line. Otherwise, you can’t be sure if its enabled or not. The string “on” is case insensitive.

For this tutorial we will create two file
1)index.php
2)image.php

First file(i.e index.php) will show image created by image.php file: index.php will contain following code

<img src="image.php">


Second file will contain following code:

<?php
$im = ImageCreate(200,200);
$white = ImageColorAllocate($im,0xFF,0xFF,0xFF);
$black = ImageColorAllocate($im,0x00,0x00,0x00);
ImageFilledRectangle($im,50,50,150,150,$black);
header('Content-Type: image/png');
ImagePNG($im);
?>

The Structure of a Graphics Program
Most dynamic image-generation programs followthe same basic steps outlined in
above code(i.e image.php)
You can create a 256-color image with the ImageCreate( ) function, which returns an
image handle:
$image = ImageCreate(width, height);
All colors used in an image must be allocated with the ImageColorAllocate( ) function.
The first color allocated becomes the background color for the image.*
$color = ImageColorAllocate(image, red, green, blue);
The arguments are the numeric RGB (red, green, blue) components of the color. In
above code (i.e image.php), we wrote the color values in hexadecimal, to bring the function call
closer to the HTML color representation "#FFFFFF" and "#000000".
There are many drawing primitives in GD. Above code (i.e image.php) uses ImageFilledRectangle( ),
in which you specify the dimensions of the rectangle by passing the coordinates of the
top-left and bottom-right corners:
ImageFilledRectangle(image, tlx, tly, brx, bry, color);
The next step is to send a Content-Type header to the browser with the appropriate
content type for the kind of image being created. Once that is done, we call the
appropriate output function. The ImageJPEG( ), ImagePNG( ), and ImageWBMP( ) functions
create JPEG, PNG, and WBMP files from the image, respectively:
ImageJPEG(image [, filename [, quality ]]);
ImagePNG(image [, filename ]);
ImageWBMP(image [, filename ]);
If no filename is given, the image is sent to the browser. The quality argument for
JPEGs is a number from 0 (worst-looking) to 10 (best-looking). The lower the quality,
the smaller the JPEG file. The default setting is 7.5.
 Above code (i.e image.php), we set the HTTP header immediately before calling the outputgenerating
function ImagePNG( ). If, instead, you set the Content-Type at the very
start of the script, any errors that are generated are treated as image data and the
browser displays a broken image icon.

No comments:

Post a Comment