2008-08-24 19 views
6

是否可以使用PHP創建圖像(而不是簡單地通過HTML鏈接到它們),如果是這樣,我應該先去哪裏瞭解這樣的事情?PHP中的圖像

回答

12

我更喜歡GD library - 退房the Examples,和這個例子:

<?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); 
?> 

輸出:

imagecreatetrucolor example http://uk2.php.net/manual/en/figures/image.imagecreatetruecolor.png

imagecreatetruecolor

+0

你應該總是嘗試使用header()函數在最後時刻可能的(如,在imagepng前()功能可按)。現在的方式是,如果php腳本「無法初始化新的GD圖像流」,瀏覽器將嘗試將其解釋爲gif,因此它不會被理解。 – stalepretzel 2008-09-07 00:25:07

6

是的,這是可能的。我相信有多個庫可以實現這一點。使用最廣泛的可能是ImageMagick,它實際上不是PHP專用的,但帶有適當的綁定。

另請參閱PHP documentation

3

結賬GD。它包含了大量用於圖像創建,操作和審問的功能。您的PHP安裝只需使用可能的GD庫進行構建。

0

MagickWand是針對相當不錯的,以及,非常強大。

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

這個片段將拍攝圖像,線制的維拉「升至」,或什麼字體可用,並且圖像刷新到瀏覽器。

$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); 
} 
0

你可以使用它的不同功能的gd庫。 並與代碼中創建良好形象

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); 

顏色爲負面

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