Forum Moderators: coopster

Message Too Old, No Replies

PHP with images

         

romzinho2k7

6:25 am on Mar 27, 2006 (gmt 0)

10+ Year Member



Hey friends, all right?

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


He adds the text, but do not manage to align the text in the image. Would he have as anybody do it help me?

At once I thank to everybody.

Thanks,
Robson

coopster

3:02 pm on Mar 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, romzinho2k7.

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;

romzinho2k7

7:55 pm on Mar 27, 2006 (gmt 0)

10+ Year Member



Thank you very much by the help!

Do to put at the centre I have to divide the height and the width for 2?

coopster

8:18 pm on Mar 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Now you're getting it ;)
// center: 
imagettftext($insert, $size, $angle, $width/2-$bb_width/2, $height/2-$bb_height/2, $col, $font, $text);

romzinho2k7

8:20 pm on Mar 27, 2006 (gmt 0)

10+ Year Member



Thank you very much! By the help and by the attention! =)