Categories
PHP
Javascript
MySQL
C#
VB
VB.NET
ASP.NET
Regex
Packaging & compression
General Web Tech
Tech Speak


Google


This website looks best on firefox.
 
Resource Center : PHP : <PHP Imaging support & Image manipulation - An overview of possibilities>

PHP Imaging support & Image manipulation - An overview of possibilities

Posted by: Floresense Team
Pages: 1  2  3  4  
    Contents in this article:
  1. How PHP supports Imaging
  2. Support for Font libraries
  3. Creating Images
  4. Getting Image size
  5. Colors
  6. Color Alpha / transparency
  7. Drawing
  8. Image resizing
  9. Image zooming / enlarging
  10. Image cropping
  11. Image rotation
  12. Drawing True-Type Fonts

How PHP supports Imaging:

PHP basically can be used to, > create / modify images, > Output image streams on a webpage. For this PHP uses a GD library of image functions. GD and PHP also work with additional libraries to support more image formats.

Checking whether your PHP version and libraries are compatible:

This step is important if you have not done this before, because some code that we will try may not work if the proper libraries are not installed. Do a phpinfo() in the server or PC on which you want to use PHP's image support.
<?php
   phpinfo();
?>
The output will contain a list of PHP libraries installed with their versions, among other information. If you have PHP version 4.0.x and above, with some GD library version, you would have some amount of imaging support in your PHP environment.

As per documentation:
The format of images you are able to manipulate depend on the version of GD you install, and any other libraries GD might need to access those image formats. Versions of GD older than gd-1.6 support GIF format images, and do not support PNG, where versions greater than gd-1.6 and less than gd-2.0.28 support PNG, not GIF. GIF support was re-enabled in gd-2.0.28. Since PHP 4.3.x there is a bundled version of the GD lib. This bundled version has some additional features like alpha blending, and should be used in preference to the external library since its codebase is better maintained and more stable.
More details on GD library support can be obtained with gdinfo() function.
<?php
   var_dump(gd_info());
?>
Typical output is:
array(9) {
["GD Version"]=>
string(24) "bundled (2.0 compatible)"
["FreeType Support"]=>
bool(false)
["T1Lib Support"]=>
bool(false)
["GIF Read Support"]=>
bool(true)
["GIF Create Support"]=>
bool(false)
["JPG Support"]=>
bool(false)
["PNG Support"]=>
bool(true)
["WBMP Support"]=>
bool(true)
["XBM Support"]=>
bool(false)
}

Support for Font libraries:

If you see a library with name "FreeType" in your phpinfo page, then you probably have font support enabled in your PHP environment. With this you can work with True-Type fonts which could get interesting. If you use PHP to generate charts or graphs you could use true-type font styled texts for popular fonts like Arial, Gothic, Verdana, Comic, Lucida, and others.

Creating Images:


We use imagecreate() function to create images in PHP.

1: <?php
2:  // create a 100*30 image 
3:  $im = imagecreate(100, 30);
4:
5:  // white background and blue text
6:  $bg = imagecolorallocate($im, 255, 255, 255);
7:  $textcolor = imagecolorallocate($im, 0, 0, 255);
8:
9:  // write the string at the top left
10: imagestring($im, 5, 0, 0, "Welcome", $textcolor);
11:
12: // output the image
13: header("Content-type: image/png");
14: imagepng($im);
15: destroy($im);
16: ?>

The above code outputs a PNG format image with the message "Welcome" in it.



The code has the ingredients to print it onto a browser window through lines 13 & 14.

line 3: creates a blank image of size 100*30.

line 6: sets the background color for the image. It might look strange to see that no set method is used or is the variable $bg used. This is because as of version PHP 4.3.x, PHP takes the first imagecolorallocate statement as for setting the background color. And so, executing until line6, php would have created an image and set its background color to white.

line 10: writes a string to the image at location x=0 y=0(from top), using PHP in-built font 5.

line 13: adds the content-type header item. Suppose all the above 16 lines of code is in a file called test.php, then the browser understands from the header that test.php is a png-format image. Anything output becomes part of the png image content.

line 14: sends the image $im as a byte stream to the output.

line 15: frees up space used by $im, since we have finished using $im. Output is through the lines 13 and 14 alone.

We will take up a little more code (continued in next page)


Pages: 1  2  3  4  

Advertisement

2005 - 2008 © Floresense.com