Contents
PHP Image Processing and GD Functions Complete Reference
PHP Image Processing and GD Functions Complete Reference
Image processing and GD functions enable the creation and manipulation of image files across various formats, such as GIF, PNG, JPEG, WBMP, and XPM. With PHP, these functions can directly output the image to the browser. The GD library is central to implementing image functions for this purpose, though other libraries may be needed depending on the image formats involved.
Example: This demonstrates the use of the imagecolorclosesthwb()
function in PHP.
Output:
Hue, White, and Blackness closest color index: 42
The list of Image processing and GD s are given below:
gd_info()
Description: Retrieve information about the installed GD library.
Example:
print_r(gd_info());
// Output: (array of GD library information)
getimagesize()
Description: Get the dimensions of an image.
Example:
$size = getimagesize("example.jpg");
print_r($size);
// Output: (displays array with width, height, and more details)
getimagesizefromstring()
Description: Determine the size of an image from a binary string.
Example:
$imageData = file_get_contents("example.jpg");
$size = getimagesizefromstring($imageData);
print_r($size);
// Output: (array of image size information)
imagealphablending()
Description: Set the blending mode for an image.
Example:
$image = imagecreatetruecolor(200, 200);
imagealphablending($image, true);
// Output: (enables or disables blending for $image)
imagearc()
Description: Create an arc centered at the specified coordinates.
Example:
$image = imagecreatetruecolor(200, 200);
imagearc($image, 100, 100, 150, 150, 0, 180, 0xFFFFFF);
// Output: (draws a half-circle in the image)
imagechar()
Description: Draw a character horizontally on an image.
Example:
imagechar($image, 5, 50, 50, "A", 0xFFFFFF);
// Output: (draws 'A' at (50, 50) on $image)
imagecharup()
Description: Draw a character vertically on an image.
Example:
imagecharup($image, 5, 50, 50, "B", 0xFFFFFF);
// Output: (draws 'B' vertically at (50, 50) on $image)
imagecolorallocate()
Description: Set a color in an image.
Example:
$color = imagecolorallocate($image, 0, 128, 255);
// Output: (assigns a blue color to $image)
imagecolorallocatealpha()
Description: Allocate color with alpha for transparency.
Example:
$color = imagecolorallocatealpha($image, 255, 0, 0, 50);
// Output: (assigns a semi-transparent red color)
imagecolorat()
Description: Get the color of a specific pixel.
Example:
$color = imagecolorat($image, 10, 10);
echo $color;
// Output: (color index of pixel at (10, 10))
imagecolorclosest()
Description: Find the closest color index in the image.
Example:
$color = imagecolorclosest($image, 100, 100, 200);
echo $color;
// Output: (closest color index for the specified RGB)
imagecolorclosestalpha()
Description: Get the closest color with an alpha value.
Example:
$color = imagecolorclosestalpha($image, 100, 100, 200, 50);
echo $color;
// Output: (closest color index with alpha)
imagecolorclosesthwb()
Description: Find the hue, white, and black closest color index.
Example:
$color = imagecolorclosesthwb($image, 255, 0, 0);
echo $color;
// Output: (closest index for hue, white, and black)
imagecolorexact()
Description: Get the index of a specific color in the image palette.
Example:
$color = imagecolorexact($image, 255, 255, 255);
echo $color;
// Output: (color index of exact match for white)
imagecolormatch()
Description: Match the palette version colors of an image to a true-color version.
Example:
imagecolormatch($image1, $image2);
// Output: (matches colors of $image1 to $image2)
imagecolorresolve()
Description: Get the specified color or closest possible in the palette.
Example:
$color = imagecolorresolve($image, 255, 100, 50);
echo $color;
// Output: (index of exact or nearest color)
imagecolorresolvealpha()
Description: Find specified color and alpha value or closest alternative.
Example:
$color = imagecolorresolvealpha($image, 255, 100, 50, 50);
echo $color;
// Output: (index of color with alpha)
imagecolorset()
Description: Set the color for a specified palette index.
Example:
imagecolorset($image, 5, 100, 50, 150);
// Output: (sets color at index 5 in palette)
imagecolorsforindex()
Description: Get the colors at a specified index.
Example:
$color = imagecolorsforindex($image, 5);
print_r($color);
// Output: (RGB array of colors at index 5)
imagecolorstotal()
Description: Find the number of colors in an image’s palette.
Example:
echo imagecolorstotal($image);
// Output: (total number of colors in the palette)
imagecolortransparent()
Description: Define a color as transparent in the image.
Example:
imagecolortransparent($image, $color);
// Output: (sets $color as transparent)
imageconvolution()
Description: Apply a convolution matrix to the image.
Example:
$matrix = array(
array(0, -1, 0),
array(-1, 4, -1),
array(0, -1, 0)
);
imageconvolution($image, $matrix, 1, 127);
// Output: (applies convolution effect)
imagecopy()
Description: Copy all or part of an image.
Example:
imagecopy($image, $src, 0, 0, 0, 0, 100, 100);
// Output: (copies $src onto $image)
imagecopymerge()
Description: Copy and merge an image with transparency.
Example:
imagecopymerge($image, $src, 0, 0, 0, 0, 100, 100, 50);
// Output: (merges $src onto $image with 50% opacity)
imagecopymergegray()
Description: Merge a grayscale version of part of an image.
Example:
echo IntlChar::isspace(" ");
// Output: 1
imagecreate()
Description: Create a new palette-based image.
Example:
$image = imagecreate(200, 200);
// Output: (creates 200x200 image with color palette)
imagecreatetruecolor()
Description: Create a new true-color image.
Example:
$image = imagecreatetruecolor(200, 200);
// Output: (creates 200x200 true-color image)
imagecrop()
Description: Crop the image to a specified rectangle.
Example:
$image = imagecrop($image, array("x"=>50, "y"=>50, "width"=>100, "height"=>100));
// Output: (crops $image to the given dimensions)
imagedashedline()
Description: Draw a dashed line in the image.
Example:
imagedashedline($image, 0, 0, 200, 200, 0xFFFFFF);
// Output: (draws dashed line from (0, 0) to (200, 200))
imagefilledarc()
Description: Draw a filled partial arc.
Example:
imagefilledarc($image, 100, 100, 100, 100, 0, 180, 0x0000FF, IMG_ARC_PIE);
// Output: (draws a half-circle arc filled in blue)
imagefilledrectangle()
Description: Draw a filled rectangle.
Example:
imagefilledrectangle($image, 0, 0, 100, 50, 0x000000);
// Output: (draws black rectangle at specified area)
imagefilledarc()
Description: Draw a filled partial arc.
Example:
imagefilledarc($image, 100, 100, 100, 100, 0, 180, 0x0000FF, IMG_ARC_PIE);
// Output: (draws a half-circle arc filled in blue)
imagelayereffect()
Description: Set alpha blending flag for layering effects.
Example:
imagelayereffect($image, IMG_EFFECT_OVERLAY);
// Output: (enables overlay effect on $image)
imagepolygon()
Description: Draw a polygon on the image.
Example:
$points = array(50, 0, 100, 100, 0, 100);
imagepolygon($image, $points, 3, 0xFFFFFF);
// Output: (draws white triangle)
imagerectangle()
Description: Draw a rectangle in the image.
Example:
imagerectangle($image, 10, 10, 100, 50, 0xFF0000);
// Output: (draws red rectangle at specified area)
imagetruecolortopalette()
Description: Convert a true-color image to a palette image.
Example:
imagetruecolortopalette($image, false, 256);
// Output: (converts image to 256 color palette)
imagesy()
Description: Return the height of an image.
Example:
echo imagesy($image);
// Output: (height of $image)
imagesx()
Description: Return the width of an image.
Example:
echo imagesx($image);
// Output: (width of $image)