Questions tagged [gdlib]

GD library is free PHP extension library used for image manipulation.

Introduction

PHP is not limited to creating just HTML output. It can also be used to create and manipulate image files in a variety of different image formats, including GIF, PNG, JPEG, WBMP, and XPM. Even more convenient, PHP can output image streams directly to a browser. You will need to compile PHP with the GD library of image functions for this to work. GD and PHP may also require other libraries, depending on which image formats you want to work with.

You can use the image functions in PHP to get the size of JPEG, GIF, PNG, SWF, TIFF and JPEG2000 images.

With the exif extension, you are able to work with information stored in headers of JPEG and TIFF images. This way you can read meta data generated by digital cameras. The exif functions do not require the GD library.


Requirements

If you have the GD library (available at » https://libgd.github.io/) you will also be able to create and manipulate images.

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.


Installation

To enable GD-support configure PHP --with-gd[=DIR] , where DIR is the GD base install directory. To use the recommended bundled version of the GD library (which was first bundled in PHP 4.3.0), use the configure option --with-gd . GD library requires libpng and libjpeg to compile.

In Windows, you'll include the GD2 DLL php_gd2.dll as an extension in php.ini. The GD1 DLL php_gd.dll was removed in PHP 4.3.2. Also note that the preferred truecolor image functions, such as imagecreatetruecolor(), require GD2.

Enhance the capabilities of GD to handle more image formats by specifying the --with-XXXX configure switch to your PHP configure line.


Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

enter image description here

For further details and definitions of the PHP_INI_* modes, see the Where a configuration setting may be set.

Here's a short explanation of the configuration directives.

gd.jpeg_ignore_warning bool

Ignore warnings created by jpeg2wbmp() and imagecreatefromjpeg().

See also the exif configuration directives.

Warning: Image functions are very memory intensive. Be sure to set memory_limit high enough.


Resource Types

This extension defines two resource types: an image identifier and a font identifier.


Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

  • GD_VERSION (string) - The GD version PHP was compiled against. (Available as of PHP 5.2.4)
  • GD_MAJOR_VERSION (integer) - The GD major version PHP was compiled against. (Available as of PHP 5.2.4)
  • GD_MINOR_VERSION (integer) - The GD minor version PHP was compiled against. (Available as of PHP 5.2.4)
  • GD_RELEASE_VERSION (integer) - The GD release version PHP was compiled against. (Available as of PHP 5.2.4)
  • GD_EXTRA_VERSION (string) - The GD "extra" version (beta/rc..) PHP was compiled against. (Available as of PHP 5.2.4)
  • GD_BUNDLED (integer) - When the bundled version of GD is used this is 1 otherwise its set to 0.
  • IMG_GIF (integer) - Used as a return value by imagetypes()
  • IMG_JPG (integer) - Used as a return value by imagetypes()
  • IMG_JPEG (integer) - Used as a return value by imagetypes() (This constant has the same value as IMG_JPG)
  • IMG_PNG (integer) - Used as a return value by imagetypes()
  • IMG_WBMP (integer) - Used as a return value by imagetypes()
  • IMG_XPM (integer) - Used as a return value by imagetypes()
  • IMG_COLOR_TILED (integer) - Special color option which can be used in stead of color allocated with imagecolorallocate() or imagecolorallocatealpha()
  • IMG_COLOR_STYLED (integer) - Special color option which can be used in stead of color allocated with imagecolorallocate() or imagecolorallocatealpha()
  • IMG_COLOR_BRUSHED (integer) - Special color option which can be used in stead of color allocated with imagecolorallocate() or imagecolorallocatealpha()
  • IMG_COLOR_STYLEDBRUSHED (integer) - Special color option which can be used in stead of color allocated with imagecolorallocate() or imagecolorallocatealpha()
  • IMG_COLOR_TRANSPARENT (integer) - Special color option which can be used in stead of color allocated with imagecolorallocate() or imagecolorallocatealpha()
  • IMG_ARC_ROUNDED (integer) - A style constant used by the imagefilledarc() function (This constant has the same value as IMG_ARC_PIE)
  • IMG_ARC_PIE (integer) - A style constant used by the imagefilledarc() function.
  • IMG_ARC_CHORD (integer) - A style constant used by the imagefilledarc() function.
  • IMG_ARC_NOFILL (integer) - A style constant used by the imagefilledarc() function.
  • IMG_ARC_EDGED (integer) - A style constant used by the imagefilledarc() function.
  • IMG_GD2_RAW (integer) - A type constant used by the imagegd2() function.
  • IMG_GD2_COMPRESSED (integer) - A type constant used by the imagegd2() function.
  • IMG_EFFECT_REPLACE (integer) - Alpha blending effect used by the imagelayereffect() function.
  • IMG_EFFECT_ALPHABLEND (integer) - Alpha blending effect used by the imagelayereffect() function.
  • IMG_EFFECT_NORMAL (integer) - Alpha blending effect used by the imagelayereffect() function.
  • IMG_EFFECT_OVERLAY (integer) - Alpha blending effect used by the imagelayereffect() function.
  • IMG_FILTER_NEGATE (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_GRAYSCALE (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_BRIGHTNESS (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_CONTRAST (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_COLORIZE (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_EDGEDETECT (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_GAUSSIAN_BLUR (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_SELECTIVE_BLUR (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_EMBOSS (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_MEAN_REMOVAL (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_SMOOTH (integer) - Special GD filter used by the imagefilter() function.
  • IMG_FILTER_PIXELATE (integer) - Special GD filter used by the imagefilter() function. (Available as of PHP 5.3.0)
  • IMAGETYPE_GIF (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_JPEG (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_JPEG2000 (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_PNG (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_SWF (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_PSD (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_BMP (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_WBMP (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_XBM (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_TIFF_II (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_TIFF_MM (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_IFF (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_JB2 (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_JPC (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_JP2 (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_JPX (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_SWC (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions.
  • IMAGETYPE_ICO (integer) - Image type constant used by the image_type_to_mime_type() and image_type_to_extension() functions. (Available as of PHP 5.3.0)
  • PNG_NO_FILTER (integer) - A special PNG filter, used by the imagepng() function.
  • PNG_FILTER_NONE (integer) - A special PNG filter, used by the imagepng() function.
  • PNG_FILTER_SUB (integer) - A special PNG filter, used by the imagepng() function.
  • PNG_FILTER_UP (integer) - A special PNG filter, used by the imagepng() function.
  • PNG_FILTER_AVG (integer) - A special PNG filter, used by the imagepng() function.
  • PNG_FILTER_PAETH (integer) - A special PNG filter, used by the imagepng() function.
  • PNG_ALL_FILTERS (integer) - A special PNG filter, used by the imagepng() function.

GD and Image Functions

Visit this link!


References

164 questions
0
votes
1 answer

How to make a color transparent in gd lib after merging images?

I am using gd lib to merge several images, but I'm having problems with the transparency. First I merge several images which each have a transparent background (works fine). Then I want to put another picture on top which consists of three parts:…
Riyuri
  • 28
  • 7
0
votes
1 answer

GD LIB how to convert every uploaded images successfully into jpg type?

please help me out with my current function to make it convert any other image types successfully into a proper jpg image type. here is my function function resize_image($oldimage_name, $new_image_name){ list($owidth,$oheight) =…
Lovelyn
  • 35
  • 4
0
votes
0 answers

merge multiple images and text into single image but not able to arrange properly

How can I arrange a dynamic image? its working fine but i need to put jd logo on upper left corner and when i do that black color appears code that i am using currently createimageinstantly(); function…
0
votes
1 answer

Placing a Transparent PNG onto another Transparent PNG (Bottom Image not showing)

I have two images. I am putting imageA over imageB. However this is causing the bottom image (imageB) colors to not show. Instead the transparent part of imageA is overriding imageB. $dest = imagecreatefrompng(6.png'); (96x96) $src =…
user10577558
0
votes
1 answer

Remove image background is not working properly in php imagick

Hello I try to remove image background using Imagick PHP library but it is not working properly. my code is only working with white background images only. i need to work with any types of background. the final image should with transparent…
Code Embassy
  • 247
  • 2
  • 16
0
votes
1 answer

The image is not shown when merging two images in PHP GD library in wordpress

I got this code to work but after few weeks the code is not working ,i did try to see where the bug come from but i have no idea. I did create image with GD library and it is created successfully but when i want to merge it with another picture it…
0
votes
0 answers

Can't show image with GD library PHP

I'm trying to display an image from another server with the code below
Erick
  • 1
  • 1
0
votes
1 answer

gd library - font size?

I am creating a captcha image (please do not suggest premade ones). It gives you the ability to call $captcha = new captcha(); $captcha->size(30)->getImage(); which sets the font size to 30 and then generates the captcha. my issue is with…
Hailwood
  • 89,623
  • 107
  • 270
  • 423
0
votes
1 answer

imagepng generate black image background

Below is my image generate code: $thumb = imagecreatetruecolor($newwidth, $newheight) or die('Cannot Initialize new GD image stream'); print_r($thumb); $source = imagecreatefrompng($filename) or die('Cannot Initialize new GD image stream');…
nim
  • 509
  • 6
  • 16
0
votes
1 answer

GD Library image generation does not work with mySQL query

Long story short, I have a project that requires creating a user's avatar based on their data from the database. The avatar is generated using the imagepng() and imagecopy() functions. The user's avatar can either be male or female and that…
Pinkernox
  • 23
  • 3
0
votes
0 answers

how to get Random date between 2 date values using php?

how to get Random date between 2 date values using php ? I am coding an application where i need to assign random date between two fixed timestamps how i can achieve this using php i've searched first but only found the answer for php And how can…
Kasun
  • 11
  • 1
0
votes
1 answer

color error in converting a transparent png to jpg in php with glib

I try to convert a partly transparent png to a jpg in php with gdlib. I found two snippets to help me with that, but both methods have the same problem: The half transparent colors are darker and do not look right. Here a enlarged sample from…
Martin
  • 11
  • 2
0
votes
2 answers

Is there a way to output html div to an image using php?

I have a div on my page which I want to output to a jpeg file. Is this possible using PHP? I know there's the GD library for creating images in PHP but I'm not sure if it can do what I want. Because I also want to put a barcode on the image (I used…
0
votes
2 answers

Help to add error checking to this function

I need to add some error checking to this function to make sure that the imagegif() and imagecolorset() functions are successful. The usual cause for failure is a permissions issue where the spot2.gif file is not writable. However, when I change…
Scott B
  • 38,833
  • 65
  • 160
  • 266
0
votes
0 answers

PNG Watermark image on GIF does not appear

Adding water mark to jpg/jpeg and png works perfectly file but strange color appear when the same watermark is added to the gif image the gif image with watermark is here the strange color here is my png watermark image which is shown like this my…
Selvesan Malakar
  • 511
  • 2
  • 7
  • 20