Forum Moderators: coopster
I am with a small problem here. I want to develop an images uploads system and that in each image be possible to put a sentence. It wanted was possible to add a sentence in the top, low, left superior, inferior left, right superior or inferior right).
I did the following code:
<?php
header("Content-type: image/jpeg");$insert = imagecreatefromjpeg("cars.jpg");
$text = "TEST";
$font = "arial.ttf";
//choose textcolor (white)
$col = imagecolorallocate ($insert, 255, 255, 255);
//check width of the text
$bbox = imagettfbbox (12, 0, $font, $text);
$xcorr = 0 + $bbox[0];
$mase = $bbox[0] + $xcorr;
//check width of the image
$width = imagesx($insert);
$height = imagesy($insert);
//calculate x coordinates for text
$new = ($width - $mase)/2;
$new2 = ($height - $mase)/2;
//write text
imagettftext($insert, 12, 0, $new, $new2, $col, $font, $text);
//output picture
imagejpeg($insert,"",100);
?>
At once I thank to everybody.
Thanks,
Robson
You need to calculate where you want the text strings in the x and y coordinates of the image. To do so, you need to know the size of the text box itself (this must include any text rotation/angle). This is not perfect but should get you started.
$text = "TEST";
$font = "arial.ttf";
$size = 12;
$angle = 0;
$insert = imagecreatefromjpeg("cars.jpg");
//choose textcolor (white)
$col = imagecolorallocate ($insert, 255, 255, 255);
//check width of the text
$bbox = imagettfbbox ($size, $angle, $font, $text);
$bbox["left"] = 0- min($bbox[0],$bbox[2],$bbox[4],$bbox[6]);
$bbox["top"] = 0- min($bbox[1],$bbox[3],$bbox[5],$bbox[7]);
$bbox["width"] = max($bbox[0],$bbox[2],$bbox[4],$bbox[6]) - min($bbox[0],$bbox[2],$bbox[4],$bbox[6]);
$bbox["height"] = max($bbox[1],$bbox[3],$bbox[5],$bbox[7]) - min($bbox[1],$bbox[3],$bbox[5],$bbox[7]);
extract ($bbox, EXTR_PREFIX_ALL, 'bb');
//check width of the image
$width = imagesx($insert);
$height = imagesy($insert);
$pad = 0;
//write text
// top:
imagettftext($insert, $size, $angle, $width/2-$bb_width/2, $bb_top+$pad, $col, $font, $text);
// left:
imagettftext($insert, $size, $angle, $bb_left+$pad, $height/2-$bb_height/2, $col, $font, $text);
// bottom:
imagettftext($insert, $size, $angle, $width/2-$bb_width/2, $height-$bb_height-$pad, $col, $font, $text);
// right:
imagettftext($insert, $size, $angle, $width-$bb_width-$pad, $height/2-$bb_height/2, $col, $font, $text);
//output picture
header("Content-type: image/jpeg");
imagejpeg($insert,"",100);
imagedestroy($insert);
exit;