'How to create images in PHP [closed]

Is it possible to create images with PHP (as opposed to simply linking to them via HTML) and if so, where should I go first to learn about such a thing?



Solution 1:[1]

I prefer the GD library - check out the Examples, and this example:

<?php
header ("Content-type: image/png");
$im = @imagecreatetruecolor(120, 20)
      or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

Outputs:

imagecreatetrucolor example
(source: php.net)

See imagecreatetruecolor.

Solution 2:[2]

Yes this is possible. I believe there are multiple libraries to accomplish this. The most widely used is probably ImageMagick which is actually not PHP specific but comes with appropriate bindings.

See also in the PHP documentation.

Solution 3:[3]

Check out GD. It contains a ton of functions for image creation,manipulation and interrogation. Your PHP install just has to built with the GD library which it probably was.

Solution 4:[4]

For decent tutorials on image generation using PHP:

GD - http://devzone.zend.com/node/view/id/1269

ImageMagick - http://www.sitepoint.com/article/dynamic-images-imagemagick

Solution 5:[5]

PHP GD

Pear Image_Canvas (and Image_Graph for graphs)

Those are the two I know of.

Solution 6:[6]

MagickWand is pretty good for that as well, and pretty powerful.

http://www.bitweaver.org/doc/magickwand/index.html

This snippet will take an image, wrie the 'rose' in Vera, or whatever fonts are available, and flush the image to the browser.

$drawing_wand=NewDrawingWand();
DrawSetFont($drawing_wand,"/usr/share/fonts/bitstream-vera/Vera.ttf");
DrawSetFontSize($drawing_wand,20);
DrawSetGravity($drawing_wand,MW_CenterGravity);
$pixel_wand=NewPixelWand();
PixelSetColor($pixel_wand,"white");
DrawSetFillColor($drawing_wand,$pixel_wand);
if (MagickAnnotateImage($magick_wand,$drawing_wand,0,0,0,"Rose") != 0) {
    header("Content-type: image/jpeg");
MagickEchoImageBlob( $magick_wand );
} else {
echo MagickGetExceptionString($magick_wand);
}

Solution 7:[7]

you can use gd library with different function of it. and create good image with the code

header("Content-Type: image/png");

//try to create an image
$im = @imagecreate(800, 600)
or die("Cannot Initialize new GD image stream");

//set the background color of the image
$background_color = imagecolorallocate($im, 0xFF, 0xCC, 0xDD);

//set the color for the text 
$text_color = imagecolorallocate($im, 133, 14, 91);

//adf the string to the image
imagestring($im, 5, 300, 300,  "I'm a pretty picture:))", $text_color);

//outputs the image as png
imagepng($im);

//frees any memory associated with the image 
imagedestroy($im);

color to Negative

if(!file_exists('dw-negative.png')) {
    $img = imagecreatefrompng('dw-manipulate-me.png');
    imagefilter($img,IMG_FILTER_NEGATE);
    imagepng($img,'db-negative.png');
    imagedestroy($img);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel
Solution 2 Konrad Rudolph
Solution 3
Solution 4
Solution 5 genesis
Solution 6 Subdigger
Solution 7 Manoj Sharma