Building on Ashley's example, here's an example of some code that grabs an existing image file and outputs an html img tag with the contents. I found it useful because I needed to not broadcast the filenames.
<?php
$file = 'images/01.png';
$image = imagecreatefrompng($file);
imagealphablending($image, false);
imagesavealpha($image, true);
// start buffering
ob_start();
imagepng($image);
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/png;base64,".base64_encode($contents)."' />";
imagedestroy($image);
?>
Image
- Einführung
- Installation/Konfiguration
- Vordefinierte Konstanten
- Beispiele
- Image-Funktionen
- gd_info — Retrieve information about the currently installed GD library
- getimagesize — Ermittelt die Größe einer Grafik
- image_type_to_extension — Get file extension for image type
- image_type_to_mime_type — Get Mime-Type for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype
- image2wbmp — Output image to browser or file
- imagealphablending — Set the blending mode for an image
- imageantialias — Should antialias functions be used or not
- imagearc — Draws an arc
- imagechar — Stellt ein Zeichen horizontal dar
- imagecharup — Zeichnet einen vertikal ausgerichteten Charakter
- imagecolorallocate — Bestimmt die Farbe einer Grafik
- imagecolorallocatealpha — Allocate a color for an image
- imagecolorat — Ermittelt den Farbwert eines Bildpunktes
- imagecolorclosest — Ermittelt den Farbwert-Index, der den angegebenen Farben am nächsten liegt
- imagecolorclosestalpha — Get the index of the closest color to the specified color + alpha
- imagecolorclosesthwb — Get the index of the color which has the hue, white and blackness
- imagecolordeallocate — Löscht eine Farbdefinition
- imagecolorexact — Ermittelt den Index-Wert der angegebenen Farbe
- imagecolorexactalpha — Get the index of the specified color + alpha
- imagecolormatch — Makes the colors of the palette version of an image more closely match the true color version
- imagecolorresolve — Ermittelt den Index-Wert der angegebenen Farbe oder die nächst mögliche Alternative dazu
- imagecolorresolvealpha — Get the index of the specified color + alpha or its closest possible alternative
- imagecolorset — Setzt die Farbe für den angegebenen Paletten-Index
- imagecolorsforindex — Ermittelt die Farbwerte einer angegebenen Farb-Palette
- imagecolorstotal — Ermittelt die Anzahl der definierten Farben eines Bildes
- imagecolortransparent — Definiert eine Farbe als transparent
- imageconvolution — Apply a 3x3 convolution matrix, using coefficient and offset
- imagecopy — Kopiert einen Bildausschnitt
- imagecopymerge — Copy and merge part of an image
- imagecopymergegray — Copy and merge part of an image with gray scale
- imagecopyresampled — Copy and resize part of an image with resampling
- imagecopyresized — Kopieren und Ändern der Größe eines Bild-Teiles
- imagecreate — Erzeugt ein neues Bild
- imagecreatefromgd2 — Create a new image from GD2 file or URL
- imagecreatefromgd2part — Create a new image from a given part of GD2 file or URL
- imagecreatefromgd — Create a new image from GD file or URL
- imagecreatefromgif — Erzeugt ein neues Bild im GIF-Format, welches aus einer Datei oder von einer URL gelesen wird
- imagecreatefromjpeg — Erzeugt ein neues Bild im JPEG-Format, welches aus einer Datei oder von einer URL gelesen wird
- imagecreatefrompng — Erzeugt ein neues Bild im PNG-Format, welches aus einer Datei oder von einer URL gelesen wird
- imagecreatefromstring — Create a new image from the image stream in the string
- imagecreatefromwbmp — Create a new image from file or URL
- imagecreatefromxbm — Create a new image from file or URL
- imagecreatefromxpm — Create a new image from file or URL
- imagecreatetruecolor — Create a new true color image
- imagedashedline — Zeichnen einer gestrichelten Linie
- imagedestroy — Löscht ein Bild
- imageellipse — Draw an ellipse
- imagefill — Füllen mit Farbe ("flood fill")
- imagefilledarc — Draw a partial arc and fill it
- imagefilledellipse — Draw a filled ellipse
- imagefilledpolygon — Zeichnet ein gefülltes Vieleck (Polygon)
- imagefilledrectangle — Zeichnet ein gefülltes Rechteck
- imagefilltoborder — Flächen-Farbfüllung ("flood fill") mit einer angegebenen Farbe
- imagefilter — Applies a filter to an image
- imagefontheight — Ermittelt die Font-Höhe
- imagefontwidth — Ermittelt die Font-Breite
- imageftbbox — Give the bounding box of a text using fonts via freetype2
- imagefttext — Write text to the image using fonts using FreeType 2
- imagegammacorrect — Anwendung einer Gamma-Korrektur auf ein GD-Bild
- imagegd2 — Output GD2 image to browser or file
- imagegd — Output GD image to browser or file
- imagegif — Ausgabe eines Bildes an den Browser oder in eine Datei
- imagegrabscreen — Captures the whole screen
- imagegrabwindow — Captures a window
- imageinterlace — Schaltet die Interlaced-Darstellung eines Bildes an oder aus
- imageistruecolor — Finds whether an image is a truecolor image
- imagejpeg — Ausgabe des Bildes im Browser oder als Datei
- imagelayereffect — Set the alpha blending flag to use the bundled libgd layering effects
- imageline — Zeichnen einer Linie
- imageloadfont — Lädt einen neuen Font
- imagepalettecopy — Copy the palette from one image to another
- imagepng — Ausgabe eines Bildes an den Browser oder in eine Datei
- imagepolygon — Zeichnen eines Vielecks (Polygon)
- imagepsbbox — Ermittelt die Ausmaße des Rechtecks, das für die Ausgabe eines Textes unter Verwendung eines PostScript-Fonts (Typ 1) notwendig ist.
- imagepsencodefont — Ändert die Vektor-Beschreibung eines Fonts
- imagepsextendfont — Vergrößert oder komprimiert einen Font
- imagepsfreefont — Gibt den durch einen Typ 1 PostScript-Font belegten Speicher wieder frei
- imagepsloadfont — Lädt einen Typ 1 PostScript-Font aus einer Datei
- imagepsslantfont — Setzt einen Font schräg
- imagepstext — Ausgabe eines Textes auf einem Bild unter Verwendung von Typ 1 PostScript-Fonts
- imagerectangle — Zeichnet ein Rechteck
- imagerotate — Rotate an image with a given angle
- imagesavealpha — Set the flag to save full alpha channel information (as opposed to single-color transparency) when saving PNG images
- imagesetbrush — Set the brush image for line drawing
- imagesetpixel — Setzt ein einzelnes Pixel
- imagesetstyle — Set the style for line drawing
- imagesetthickness — Set the thickness for line drawing
- imagesettile — Set the tile image for filling
- imagestring — Zeichnet einen horizontalen String
- imagestringup — Zeichnet einen vertikalen String
- imagesx — Ermittelt die Bild-Breite
- imagesy — Ermittelt die Bild-Höhe
- imagetruecolortopalette — Convert a true color image to a palette image
- imagettfbbox — Ermittelt die Rahmenmaße für die Ausgabe eines Textes im True-Type-Format
- imagettftext — Erzeugt TTF-Text im Bild
- imagetypes — Gibt die von der aktuell verwendeten PHP-Version unterstützten Grafik-Formate zurück
- imagewbmp — Output image to browser or file
- imagexbm — Output XBM image to browser or file
- iptcembed — Embeds binary IPTC data into a JPEG image
- iptcparse — Parse a binary IPTC block into single tags.
- jpeg2wbmp — Convert JPEG image file to WBMP image file
- png2wbmp — Convert PNG image file to WBMP image file
Image
jordan at jkdesign dot org
07-Sep-2009 10:09
07-Sep-2009 10:09
scurvysquid at yahoo dot com
04-May-2009 07:08
04-May-2009 07:08
to address Thomas' note about exif data, exif is only valid for .tiff and .jpg files. As always, it's up to the individual to make the call but I personally would prefer to create an abstraction layer to work for all images, and relying on exif data means you need to limit your allowed image types. I've heard some great things about the GD library and found them to all be true in my own use of it, losing the nominally useful data specific to jpegs and pngs is not a sufficient reason alone to convince me to use imagemagick instead.
Thomas
10-Dec-2008 10:05
10-Dec-2008 10:05
You know, maybe this goes without saying, but I thought I would drop a note in here. When developing code to resize images, it is best not to use GD. When using the current GD methodologies, you are reading content from an image and manipulating it. By then writing that content to a brand new file, you are losing the EXIF data.
For purposes when you want to retain EXIF data, it is recommended that you compile in and use the PECL Imagick extension. It has great resizing methods built right in and the EXIF data is retained.
code at ashleyhunt dot co dot uk
24-Oct-2008 01:02
24-Oct-2008 01:02
I have been looking to send the output from GD to a text string without proxying via a file or to a browser.
I have come up with a solution.
This code buffers the output between the ob_start() and ob_end() functions into ob_get_contents()
See the example below
<?php
// Create a test source image for this example
$im = imagecreatetruecolor(300, 50);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// start buffering
ob_start();
// output jpeg (or any other chosen) format & quality
imagejpeg($im, NULL, 85);
// capture output to string
$contents = ob_get_contents();
// end capture
ob_end_clean();
// be tidy; free up memory
imagedestroy($im);
// lastly (for the example) we are writing the string to a file
$fh = fopen("./temp/img.jpg", "a+" );
fwrite( $fh, $contents );
fclose( $fh );
?>
Enjoy!
Ashley
