* @link http://pear.php.net/package/Image_Graph */ // SPECIFY HERE WHERE A TRUETYPE FONT CAN BE FOUND $testFont = 'c:/windows/fonts/Arial.ttf'; if (!file_exists($testFont)) { die('The font specified cannot be found (' . $testFont . ')! Please specify an existing font'); } // create a true color image (requires GD2) $image = ImageCreateTrueColor(600, 200); ImageAlphaBlending($image, true); // allocate some colors $black = ImageColorAllocate($image, 0, 0, 0); $red = ImageColorAllocate($image, 0xff, 0, 0); $green = ImageColorAllocate($image, 0, 0xff, 0); $blue = ImageColorAllocate($image, 0, 0, 0xff); $white = ImageColorAllocate($image, 0xff, 0xff, 0xff); // create a frame ImageFilledRectangle($image, 0, 0, 599, 199, $white); ImageRectangle($image, 0, 0, 599, 199, $black); // output some text using the specified font $y = 20; $text = 'Your Freetype installation with GD works'; for ($i = 12; $i <= 20; $i++) { $box = ImageTTFBbox($i, 0, $testFont, $text); $x = 300 - (max($box[0], $box[2], $box[4], $box[6]) - min($box[0], $box[2], $box[4], $box[6])) / 2; ImageTTFText($image, $i, 0, $x, $y, $black, $testFont, $text); $y += max($box[1], $box[3], $box[5], $box[7]) - min($box[1], $box[3], $box[5], $box[7]); } // output the test image header('Content-Type: image/png'); ImagePNG($image);
<?php Header('Content-Type: image/png'); $height = 600; $txt = 'Carl hacking'; $size = ImageTTFBbox(25, 0, 'timesi', $txt); $txt_w = abs($size[2] - $size[0]); $txt_h = abs($size[6] - $size[1]); $bg = ImageCreateFromJpeg('chs.jpg'); $img_width = imagesx($bg); $img_height = imagesy($bg); $width = $height / $img_height * $img_width; $sizing = "Original image size {$img_width} x {$img_height}\r\n"; $sizing .= " New image size {$width} x {$height}"; $im = ImageCreateTrueColor($width, $height); ImageCopyResampled($im, $bg, 0, 0, 0, 0, $width, $height, $img_width, $img_height); $white = ImageColorAllocate($im, 255, 255, 255); $black = ImageColorAllocate($im, 0, 0, 0); $col = ImageColorResolveAlpha($im, 10, 10, 10, 50); ImageFilledRectangle($im, 10, 5, 470, 90, 0x30ffffff); ImageTTFText($im, 25, 0, 20, 40, $black, 'timesi', $sizing); ImageDestroy($bg); $box = ($width - $txt_w) / 2; ImageFilledRectangle($im, $box - 10, $height - $txt_h - 30, $width - $box, $height - 5, $col); $yellow = ImageColorAllocate($im, 255, 255, 10); ImageTTFText($im, 25, 0, $box, $height - $txt_h - 5, $yellow, 'timesi', $txt); ImagePNG($im);
public function text($text, $x = 0, $y = 0, $font = '', $font_size = 18) { $box = ImageTTFBbox($font_size, 0, $font, $text); $text_width = abs($box[4] - $box[0]); $text_height = abs($box[5] - $box[1]); $wm = @ImageCreateTrueColor($text_width, $text_height); ImageAlphaBlending($wm, FALSE); if ($this->background_color) { $bgcolor = $this->background_color; } else { $bgcolor = ImageColorAllocateAlpha($wm, 0, 0, 0, 127); } ImageFilledRectangle($wm, 0, 0, $text_width, $text_height, $bgcolor); ImageAlphaBlending($wm, TRUE); if ($this->text_color) { $color = $this->text_color; } else { $color = ImageColorAllocateAlpha($wm, 0, 0, 0, 0); } ImageTTFText($wm, $font_size, 0, 0, $text_height - 2, $color, $font, $text); ImageAlphaBlending($this->im, TRUE); ImageCopy($this->im, $wm, $x, $y, 0, 0, $text_width, $text_height); }
<?php Header('Content-Type: image/png'); $height = 600; $txt = 'My nephew Marcio, the future Linux kernel hacker'; $font = '/usr/X11R6/lib/X11/fonts/TTF/luximri.ttf'; if (!file_exists($font)) { $font = 'timesi'; } $size = ImageTTFBbox(14, 0, $font, $txt); $txt_w = abs($size[2] - $size[0]); $txt_h = abs($size[6] - $size[1]); $bg = ImageCreateFromJpeg('../images/jmc_nephew.jpg'); $img_width = imagesx($bg); $img_height = imagesy($bg); $width = $height / $img_height * $img_width; $sizing = "Original image size {$img_width} x {$img_height}\r\n"; $sizing .= " New image size {$width} x {$height}"; $im = ImageCreateTrueColor($width, $height); ImageCopyResampled($im, $bg, 0, 0, 0, 0, $width, $height, $img_width, $img_height); $white = ImageColorAllocate($im, 255, 255, 255); $black = ImageColorAllocate($im, 0, 0, 0); $col = ImageColorResolveAlpha($im, 10, 10, 10, 50); ImageFilledRectangle($im, 10, 5, 470, 90, 0x30ffffff); ImageTTFText($im, 14, 0, 20, 40, $black, $font, $sizing); ImageDestroy($bg); $box = ($width - $txt_w) / 2; ImageFilledRectangle($im, $box - 10, $height - $txt_h - 30, $width - $box, $height - 5, $col); $yellow = ImageColorAllocate($im, 255, 255, 10); ImageTTFText($im, 14, 0, $box, $height - $txt_h - 5, $yellow, $font, $txt); ImagePNG($im);