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
25
votes
1 answer

how to create a base64encoded string from image resource

I have sent a base64 encoded string via AJAX to PHP and created an image resource with imagecreatefromstring - all is fine. Now I want to get the base64 encoded string after resizing te image, but i CANT find a function to get the base64encoded…
netzaffin
  • 1,602
  • 3
  • 13
  • 14
16
votes
3 answers

In PHP, imagepng() accepts a filter parameter. How do these filters affect the function's output?

How do these filters affect the output of imagepng() in PHP? PNG_NO_FILTER PNG_FILTER_NONE PNG_FILTER_SUB PNG_FILTER_UP PNG_FILTER_AVG PNG_FILTER_PAETH PNG_ALL_FILTERS The documentation simply says, "A special PNG filter, used by the imagepng()…
Joe Lencioni
  • 10,231
  • 18
  • 55
  • 66
15
votes
6 answers

Face detection in PHP

Does anybody know of a good way to do face detection in PHP? I came across some code here that claims to do this, but I can't seem to get it to work properly. I'd like to make this work (even though it will be slow) and any help you can give me…
Joe Lencioni
  • 10,231
  • 18
  • 55
  • 66
14
votes
1 answer

Rendering an SVG file to a PNG or JPEG in PHP

I've googled as much as I can, but I've only found a PHP class that calls upon Inkscape to render the SVG, so I ask here: I have a valid SVG file generated in some way (or uploaded by a client). I need to render this into a JPG or PNG using just PHP…
DanRedux
  • 9,119
  • 6
  • 23
  • 41
12
votes
6 answers

How to create an image with transparent background

How to create an image with GDlib with a transparent background? header('content-type: image/png'); $image = imagecreatetruecolor(900, 350); imagealphablending($image, true); imagesavealpha($image, true); $text_color = imagecolorallocate($image,…
clarkk
  • 27,151
  • 72
  • 200
  • 340
12
votes
4 answers

Detect the location of an image within a larger image

How do you detect the location of an image within a larger image? I have an unmodified copy of the image. This image is then changed to an arbitrary resolution and placed randomly within a much larger image which is of an arbitrary size. No other…
rook
  • 66,304
  • 38
  • 162
  • 239
8
votes
4 answers

How to give border to text in PHP GD library

How can I give border to text using PHP GD library with multicolored text, where the text color is different from the border color. As you can refer:
Anand Jain
  • 779
  • 2
  • 10
  • 32
7
votes
2 answers

How to convert PNG to 8-bit PNG using PHP GD library

I want to write a routine which takes PNG image path as parameter and convert that image into 8-bit PNG image. I need to use PHP GD library for this.
Nilesh
  • 1,047
  • 1
  • 12
  • 20
7
votes
2 answers

Problem with imagecreatefrompng (PHP)

in one of my PHP projects, imagecreatefrompng complains, that the image "is not a valid PNG file". Warning: imagecreatefrompng() [function.imagecreatefrompng]: '/.../...png' is not a valid PNG file in /.../...php on line ... I'm pretty sure, the…
Timo
  • 663
  • 4
  • 9
  • 17
6
votes
1 answer

GD Library imagettftext getting problem in Gujarati Language Text

I have used GD Library to create "Text on Image". I am facing one issue that passing some Gujarati text but getting wrong output as below: I want like this and getting: My code is: $textBox = imagettfbbox($fontSize, $angle, $font,…
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
6
votes
1 answer

PHP+GD: imagecopymerge not retaining PNG transparencies

I have two PNG files, "red.png" and "blue.png"; they are both mostly transparent, but there is a few pixels of red or blue splotches in various places. I want to make a PHP script that merges the two; it should be as simple as something…
Andy Moore
  • 865
  • 3
  • 16
  • 36
6
votes
2 answers

Using PHP to recreate something similar to VistaPrint / CafePress / Zazzle. Any Ideas?

I am working on a website in which the client wishes to have users upload background images to a printable design, crop to size, add text. Do this to multiple pages then generate a pdf of the 'book' I am running into the following issues/questions…
kilrizzy
  • 2,895
  • 6
  • 40
  • 63
6
votes
3 answers

How would I skew an image with GD Library?

I want to skew an image into a trapezoidal shape. The left and right edges need to be straight up and down; the top and left edges need to be angular. I have no idea what the best way to do this is. I'm using GD Library and PHP. Can anyone point me…
Jason Swett
  • 43,526
  • 67
  • 220
  • 351
6
votes
1 answer

Create a true grayscale PNG in PHP

I need to create a grayscale image in PHP. I am not talking about an indexed image with grayscale values in its palette, but about a TRUE grayscale image. The difference is in the 26th byte of the PNG (color type): 0 - greyscale <-- THIS IS WHAT I…
Jpsy
  • 20,077
  • 7
  • 118
  • 115
6
votes
2 answers

Install GD Library on RedHat machine for twiki

My ultimate goal is to run a twiki website for my research group. I have space on RedHat server that is running Apache, etc., but upon which I do not have root access. Since I cannot install perl modules with the current permissions, I've decided to…
JBWhitmore
  • 11,576
  • 10
  • 38
  • 52
1
2 3
10 11