web 2.0

How to use PHP & Create Text in Image

How to use PHP & Create Text in image The guideline/example scripts how to using PHP and GD library create text in image.

ShotDev Focus:
- PHP & GD Library (Create Text in Image)

Example 1 (Create Text in Image)


<?
echo "<img src=MyResize/image.png>";
$images = ImageCreate(300,200);
$color = ImageColorAllocate($images,255,0,0);
$photo = ImageColorAllocate($images,0,0,0);
ImageRectangle($images, 0, 0, 299, 199, $photo);
ImageString($images, 5, 10, 20, "www.ShotDev.Com Version 2010", $photo);
ImageString($images, 5, 10, 40, "Community By @W_IN", $photo);
ImageStringUp($images, 5, 10, 190, "My Name is Win", $photo);
ImagePng($images,"MyResize/image.png");
ImageDestroy($images);
?>

PHP & GD (Create Text in Image)

Example 2 (Using Font)


<?
echo "<img src=MyResize/image.png>";
$images = ImageCreate(300,200);
$color = ImageColorAllocate($images,255,0,0);
$photo = ImageColorAllocate($images,0,0,0);
ImageRectangle($images, 0, 0, 299, 199, $photo);

$text = 'My Name Is Win';
$font = 'ANGSAZ.TTF';

$white = ImageColorAllocate($images, 255, 255, 255);
$blue = ImageColorAllocate($images, 0, 0, 255);
ImagettfText($images, 20, 0, 11, 21, $blue, $font, $text);

ImagePng($images,"MyResize/image.png");
ImageDestroy($images);
?>

PHP & GD (Create Text in Image)

Example 3 (Font and Text Center)


<?php

$text = "ShotDev.Com";
$font_size = 15;
$height = 200;
$width = 400;

$im = ImageCreate($width, $height);
$grey = ImageColorAllocate($im, 230, 230, 230);
$black = ImageColorAllocate($im, 0, 0, 0);

$text_bbox = ImageTTFBBox($font_size, 0, "ANGSAZ.TTF", $text);
$image_centerx = $width / 2;
$image_centery = $height / 2;
$text_x = $image_centerx - round(($text_bbox[4]/2));
$text_y = $image_centery;

ImageTTFText($im, $font_size, 0, $text_x, $text_y, $black, "ANGSAZ.TTF", $text);
ImagePng($im,"MyResize/image.png");
ImageDestroy ($im);

echo "<img src=MyResize/image.png>";
?>

PHP & GD (Create Text in Image)

Example 4 (Check size image and write text)


<?php
$height = 200;
$width = 200;
$fontsize = 25;
if (!isset($String))
$String = "Welcome To ShotDev.Com";
$im = imagecreate($width, $height );
$blue = imagecolorallocate($im,0,0,255);
$green = imagecolorallocate($im,0,255,0);
$font = "ANGSAZ.TTF";
$textwidth = $width;
while (1){
$box = imageTTFbbox( $fontsize, 0, $font, $String );
$textwidth =  abs( $box[2] );
$textbodyheight = (abs($box[7]))-2;
if ( $textwidth < $width - 20 )
break;
$fontsize--;
}
$Xcenter = (int)($width/2 );
$Ycenter = (int)($height/2 );
imageTTFtext($im, $fontsize, 0,(int) ($Xcenter-($textwidth/2)),(int)($Ycenter+(($textbodyheight)/2) ),
$green, $font, $String );
imagegif($im,"MyResize/image.png");
echo "<img src=MyResize/image.png>";
?>

PHP & GD (Create Text in Image)

Example 5


<?php

$text = "ShotDev.Com";
$font_size = 15;
$height = 200;
$width = 400;

$im = ImageCreate($width, $height);
$grey = ImageColorAllocate($im, 230, 230, 230);
$black = ImageColorAllocate($im, 0, 0, 0);

$text_bbox = ImageTTFBBox($font_size, 0, "ANGSAZ.TTF", $text);
$image_centerx = $width / 2;
$image_centery = $height / 2;
$text_x = $width - ($width - 20);
$text_y = $height - 20;

ImageTTFText($im, $font_size, 0, $text_x, $text_y, $black, "ANGSAZ.TTF", $text);
ImagePng($im,"MyResize/image.png");
ImageDestroy($im);

echo "<img src=MyResize/image.png>";
?>

PHP & GD (Create Text in Image)

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.