WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Create images on fly using GD-Image Lib
Categories : PHP, Graphics Click here to Update Your Picture
Pradeep Mamgain
Date : Jul 16th 2002
Grade : 3 of 5 (graded 3 times)
Viewed : 8436
File : imageword.zip
Images : No Images for this code example.
Search : More code by Pradeep Mamgain
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Getting the Size of An Image (GetImageSize)
===========================================

GetImageSize generates a an array of information. Array has four elements.
Index/Contents
0/Width of image in pixels.
1/Height
2/flag (type of image, 1-GIF:2-JPEG:3-PNG:4-SWF:5-PSD:6-BMP)
3/a text string in the format "height=ZZZ width=ZZZ"

Ex.1 :
<?php
$size=GetImageSize("trial.png");
foreach($size as $element){
echo $element
}
?>

Output of the above would be like
Width 149
Height 33
Type 3 (PNG - IMAGE)
String width="149" height="33" // which can be used as HTML tag.\

The GetImageSize() function is the part of std PHP library you dont need GD Support
enabled to use this function. If you pass a jpeg to it , function would return two
arrays in associative form. One element "bits" will be no of bits and "channels" no
of channels

<?php
$image_info=GetImageSize("balloon.jpg");
print $image_info['channels']."<br>";
print $image_info['bits'];
?>


Generating Color Identifier for an Image (ImageColorAllocate)
=============================================================
int imagecolorallocate (int im,int red, int green, int blue)

ImageColorAllocate() returns a color identifier representing the color composed of
the given RGB components. The im argument is the return from the imagecreate()
function. im is the identifire to image.

<?
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
$red=ImageColorAllocate ($im, 255, 0, 0);
?>

Drawing String Inside an Image(ImageString)
===========================================

int imagestring (int im, int font, int x, int y, string s, int col)

ImageString() draws the string.

im :: image identifire
s :: string
x :: x coordinate
y :: y coordinate
col :: color
font :: font if font is 1,2,3,4,5 built-in font will be used.

Returns -1 if the allocation failed.

Output an Image to either to browser or to a file (ImagePng)
============================================================
ImagePNG :: Output a PNG image to either the browser or a file

int imagepng (int im [, string filename])

The ImagePNG() outputs a GD image stream (im) in PNG format to standard output
(usually the browser) or, if a filename is given by
the filename it outputs the image to the file.

Destroying Image Indetifier to Free Memory (ImageDestroy)
=========================================================

int imagedestroy (int im)

ImageDestroy() frees any memory associated with image im.

Creating an Image (ImageCreate)
===============================

Create an image with GD
int imagecreate (int x_size, int y_size)

imagecreate returns an image identifier.

Ex.
<?
// print the header telling the web browser that its a png image
header ("Content-type: image/png",false);
// Create an image width 200 and height 30 pixels
$im=imagecreate(200,30) or die("Can not initialize GD Stream");
// BackGround Color of the image
$bgcolor=ImageColorAllocate($im,255,0,0);
// text color of the image
$textcolor=ImageColorAllocate($im,255,255,255);
//write string to image
// 5 : font , 1 : x , 5 : y
imagestring($im,5,1,5,"http://www.netrpx.com",$textcolor);
imagepng($im); // output to browser
//imagepng($im,"trial1.png"); output to file
imagedestroy($im); // Frees Memory
?>

Making an image transparent( ImageColorTransparent)
===================================================
You can make an image transparent using ImageColorTransparent function

ImageColorTransparent(im,color)


Drawing an ARC(ImageArc )
=========================
imagearc generates a partial ellipse.

int imagearc (int im, int cx, int cy, int w, int h, int s, int e,int col)

im :: arc to image im
cx :: X position
cy :: Y position
w :: width
h :: height
s :: start angle
e :: end angle
col :: color


<?
header ("Content-type: image/png");
$im=imagecreate(200,100);
$white=imagecolorallocate($im,255,255,255);
imagecolortransparent($im,$white); // Making Image Transparent
$lightblue=imagecolorallocate($im,20,93,233);
imagearc($im,0,0,199,95,10,180,$lightblue);
imagepng($im);
imagedestroy($im);
?>
ImageArc function can also be used for drawing Circle and ellipse.

Ellipse :::: Start angle 0, End Angle 360 , height and width should not be the same
imagearc($im,90,50,150,90,0,360,$lightblue);

Circle :::: Start angle 0, End Angle 360 , height and width should be equal
imagearc($im,50,50,90,90,0,360,$lightblue);


Filling border of Image created by ImageArc Function (Image fill to border)
===========================================================================

int imagefilltoborder (int im, int x, int y, int border, int col)

ImageFillToBorder() performs a flood fill whose border color is defined by border.
The starting point for the fill is x, y (top left is 0, 0) and the region is filled
with color col.

Add following line below imagearcfunction you have written above imagefilltoborder
($im,50,50,$lightblue,$lightblue);

opning a pre-existing Image( ImageCreateFrompng)
================================================

ImageCreateFromPNG() returns an image identifier representing the image obtained
from the given filename.ImageCreateFromPNG() returns an empty string on failure.

<?
/*// Opening an Pre-Existing Image
header ("Content-type: image/png");
$img_png=ImageCreateFromPng("trial.png");
if ($img_png==" "){
echo "Error !!! could not open trial.png";
exit;}
$tc=ImageColorAllocate($img_png,0,4,232);
ImageString($img_png,5,5,5,"WWW.NETRPX.COM",$tc);
ImagePng($img_png);*/
?>

Drawing Rectangles (ImageRectangle ::: ImageFilledRectangle)
============================================================

int imagerectangle (int im, int x1, int y1, int x2, int y2, int col)

ImageRectangle() creates a rectangle of color col in image im starting at upper left
coordinate x1, y1 and ending at bottom right coordinate x2, y2. (0, 0 is the top
left corner of the image).

<?
header ("Content-type: image/png");
$im=imagecreate(500,100);
$white=imagecolorallocate($im,255,255,255);
$lightblue=imagecolorallocate($im,20,93,233);
ImageRectangle($im,0,0,90,50,$lightblue);
imagepng($im);
imagedestroy($im);
?>

to create a filled rectangle use ImageFilledRectangle function. In the above code
replace ImageRectangle function with ImageFilledRectangle Function

ImageFilledRectangle($im,0,0,90,50,$lightblue);

Above function can also be used to draw lines. Generally line is a rectangle with
nonexistent with but in real word it sould be one pixel wide

ImageFilledRectangle($im,0,0,90,1,$lightblue);.

Getting the total no. of Colors in an image (ImageColorsTotal)
==============================================================

ImageColorsTotal(file indetifier)

This returns the number of colors in the specified image's palette.

header ("Content-type: image/png");
$im=ImageCreateFromPng("trial.png");
echo "No of colors in image : " . ImageColorsTotal($im);
ImageDestroy($im);

Getting Color of a Certain path of an Image ( ImageColorAt, ImageColorsForIndex )
=================================================================================

ImageColorAt

int imagecolorat (int im, int x, int y)

Returns the index of the color of the pixel at the specified location
in the image.

ImageColorsForIndex

array imagecolorsforindex (int im, int index)

This returns an associative array with red, green, and blue keys that contain the
appropriate values for the specified color index.

<?
header ("Content-type: image/png");
$im=ImageCreateFromPng("trial.png");
$indexis=ImageColorAt($im,5,8); // color index of image at x=5,y=8
$rgbarray=ImageColorsForIndex($im,$indexis); // associative array of red,green,blue
echo "Green : " . $rgbarray['green'];
echo " Blue : " . $rgbarray['blue'] ;
echo " Red : " . $rgbarray['red'] ;
?>

Drawing Polygons (ImagePolygon)
===============================

int imagepolygon (int im, array points, int num_points, int col)

ImagePolygon() creates a polygon in image id. Points is a PHP array containing the
polygon's vertices. Num_points is the total number of vertices.

<?
header ("Content-type: image/png");
$im=imagecreate(500,100);
$white=imagecolorallocate($im,255,255,255);
$lightblue=imagecolorallocate($im,20,93,233);
$points=array(10,16,12,70,20,50,80,30); // Eight points for four vertices
ImagePolygon($im,$points,4,$lightblue); // draw ploygon
ImagePng($im);
ImageDestroy($im);
?>

To create a filled polygon use ImageFilledPolygon Function

<?
ImageFilledPolygon($im,$points,4,$lightblue);
?>



Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality.
Categories : PHP, GD image library, Graphics
Random Image Display
Categories : PHP, Filesystem, Graphics, HTML and PHP
PHP Round Clock - Must have Gif support to use this.
Categories : PHP, Date Time, Graphics
Creating thumbnails from MySQL Blobs online
Categories : PHP, MySQL, Graphics, HTML and PHP, Databases
webcam cam view image ispy browser independant
Categories : Graphics, HTML, HTML and PHP, PHP
Diffusion-Limited Aggregation visualization
Categories : PHP, Graphics, Algorithms, Math.
This is a script that list all image files on a given directory, and displays the thumbnails nicely formated within an HTML table. It also make use of JavScript to open pop up windows when the users want to see the full photo.
Categories : Graphics, PHP, Complete Programs, Java Script
Functions for loading images into a MySQL database and displaying them.
Categories : Graphics, HTML and PHP, MySQL, PHP, Databases
Render TTF Text to PNG. Text message, font, size, rotation, padding, color, background, and transparency can all be defined via URL.
Categories : PHP, PHP Classes, Graphics
Annual Bar Chart
Categories : Graphics, PHP, Charts and Graphs
Display a bar chart based on random values.
Categories : Graphics, PHP, GD image library, Charts and Graphs
This program implements hot link prevention in php. It is useful for webmasters who do not have access to the server at a level where they can control hot linking can still supply some type of hot link prevention for thier site by using php.
Categories : PHP, Filesystem, Graphics, Content Management
Upload images restricted by pixel size (Picture width and Picture Height)
Categories : PHP, HTML and PHP, Graphics
How to create charts for php using Rchart
Categories : PHP, Java, JSP, Graphics, Charts and Graphs
How to check if a file is of type gif or jpg?
Categories : PHP, Regexps, Graphics