Write text in center of an image with GD Library
Want to write text in the center of an image.You may have two types of images.
(1) you create a blank image and write some text on it. Or
(2) you create the image with an existing image and write text on it.
If you don't know how to create images in php you may want to read this post
Create images in php with GD Library
(1) you create a blank image and write some text on it. Or
(2) you create the image with an existing image and write text on it.
If you don't know how to create images in php you may want to read this post
Create images in php with GD Library
Write text in center of an image which is created from an existing image.
<?php $im = imagecreatefrompng("image.png"); $color = imagecolorallocate($im, 200, 250,200); // get the width and the height of the image $width = imagesx($im); $height = imagesy($im); $font = 5; $text = "center"; // get width and height of the font $font_width = imagefontwidth($font); $font_height = imagefontheight($font); // get width and height of the twxt $text_width = $font_width * strlen($text); $text_height = $font_height; // calculate center position from left $position_center = ceil(($width - $text_width) / 2); // calculate center position from top $position_middle = ceil(($height - $text_height) / 2); //write the text at calculated position imagestring($im, $font, $position_center, $position_middle, $text, $color); header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?>
Write text in center of a blank image.
<?php $im = imagecreate(200,100); imagecolorallocate($im, 143, 42, 110); $color = imagecolorallocate($im, 122, 250, 200); // width and height are fixed $width = 200; $height = 100; $font = 5; $text = "Text in center"; // get width and height of the font $font_width = imagefontwidth($font); $font_height = imagefontheight($font); // get width and height of the twxt $text_width = $font_width * strlen($text); $text_height = $font_height; // calculate center position from left $position_center = ceil(($width - $text_width) / 2); // calculate center position from top $position_middle = ceil(($height - $text_height) / 2); //write the text at calculated position imagestring($im, $font, $position_center, $position_middle, $text, $color); header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?>
Write text in center of an image with GD Library
Reviewed by JS Pixels
on
April 10, 2012
Rating:
No comments: