PHP script help for complete novice Please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaxx
Forum Newbie
Posts: 3
Joined: Sat May 22, 2010 4:36 pm

PHP script help for complete novice Please

Post by jaxx »

Hi guys and gals...I wonder could somebody kindly help me out?

I make forum signatures for an online baby forum, like this:

Image

I add the age ticker using a code somebody did for me and then upload it to my server....he made it pretty simple so I can understand how to change the font, colour, alignment etc.... but I am wanting to go a little further and add 2 different age tickers to one image...for example i make a signature with 2 little boys on, I want to put 2 tickers underneath relevant to each child..

can this be done?

This the script I am using:

Code: Select all

<?php
header('content-type: image/png');

// ---------- DO NOT EDIT ANYTHING ABOVE ----------

$image = "IMAGE NAME HERE.png"; 

$born_day = 17;
$born_month = 4;
$born_year = 2009;

$font = 'comic.ttf';
$fontsize = 12;
$slant = 0;
$margin_left = 55;
$margin_top = 250;

// ignore these, go down 17 lines to change the font colour
$image = imagecreatefrompng($image); 
$red = ImageColorAllocate($image, 255, 0, 0);
$green = ImageColorAllocate($image, 0, 153, 0);
$blue = ImageColorAllocate($image, 0, 0, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$white = ImageColorAllocate($image, 255, 255, 255);
$yellow = ImageColorAllocate($image, 255, 255, 0);
$aqua = ImageColorAllocate($image, 0, 255, 255);
$fuschia = ImageColorAllocate($image, 255, 0, 255);
$grey = ImageColorAllocate($image, 153, 153, 153);
$silver = ImageColorAllocate($image, 204, 204, 204);
$teal = ImageColorAllocate($image, 0, 153, 153);
$lime = ImageColorAllocate($image, 0, 255, 0);
$navy = ImageColorAllocate($image, 0, 0, 153);
$purple = ImageColorAllocate($image, 153, 0, 153);
$maroon = ImageColorAllocate($image, 153, 0, 0);
$olive = ImageColorAllocate($image, 153, 153, 0);

// don't forget the font colour
$fontcolor = $red;


// ---------- DO NOT EDIT ANYTHING BELOW ----------

//gets the difference between today and $bornon
$today = mktime();
$bornon = mktime(0, 0, 0, $born_month, $born_day, $born_year);
$diff = ($today - $bornon);

// calculate days, months, years
$years = floor($diff/(60*60*24*365));
$months = floor( ($diff - ($years * (60*60*24*365)))/(60*60*24*(365/12)));
$days = floor(($diff - ($years * (60*60*24*365)) - ($months * (60*60*24*(365/12))))/(60*60*24));

// text to superimpose onto the image
$text = sprintf("CHILDS NAME OR ANY TEXT HERE is %s years, %s months and %s days old",$years, $months, $days); 

// make image
imagettftext($image, $fontsize, $slant, $margin_left, $margin_top, $fontcolor, $font, $text);

// display image
imagepng($image);

// all done...
imagedestroy($image);
?>
Thank you in advance :D
Last edited by Benjamin on Sun May 23, 2010 6:01 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: PHP script help for complete novice Please

Post by manohoo »

Just replace the code in the correspondig section with this:

Code: Select all

// text to superimpose onto the image
$text = sprintf("FIRST CHILDS NAME OR ANY TEXT HERE is %s years, %s months and %s days old",$years, $months, $days); 
$text2 = sprintf("SECOND CHILDS NAME OR ANY TEXT HERE is %s years, %s months and %s days old",$years, $months, $days);

// make image
$verticalGap = 40;
imagettftext($image, $fontsize, $slant, $margin_left, $margin_top, $fontcolor, $font, $text);
imagettftext($image, $fontsize, $slant, $margin_left, $margin_top+$verticalGap, $fontcolor, $font, $text2);
You will have to play with the actual positioning of $text2
jaxx
Forum Newbie
Posts: 3
Joined: Sat May 22, 2010 4:36 pm

Re: PHP script help for complete novice Please

Post by jaxx »

Thankyou, just another Q, where does the 2nd DOB get entered?
Do I add a 2nd code for one at the top?
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: PHP script help for complete novice Please

Post by manohoo »

Not the most elegant solution, but it will work:

Code: Select all

<?php
header('content-type: image/png');

// ---------- DO NOT EDIT ANYTHING ABOVE ----------

$image = "IMAGE NAME HERE.png"; 

$born_day = 17;
$born_month = 4;
$born_year = 2009;

$born_day2 = 1;
$born_month2 = 1;
$born_year2 = 2010;

$gap = 20; // this is the distance bewteen the two lines, change value to adjust

$font = 'comic.ttf';
$fontsize = 12;
$slant = 0;
$margin_left = 55;
$margin_top = 250;

// ignore these, go down 17 lines to change the font colour
$image = imagecreatefrompng($image); 
$red = ImageColorAllocate($image, 255, 0, 0);
$green = ImageColorAllocate($image, 0, 153, 0);
$blue = ImageColorAllocate($image, 0, 0, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$white = ImageColorAllocate($image, 255, 255, 255);
$yellow = ImageColorAllocate($image, 255, 255, 0);
$aqua = ImageColorAllocate($image, 0, 255, 255);
$fuschia = ImageColorAllocate($image, 255, 0, 255);
$grey = ImageColorAllocate($image, 153, 153, 153);
$silver = ImageColorAllocate($image, 204, 204, 204);
$teal = ImageColorAllocate($image, 0, 153, 153);
$lime = ImageColorAllocate($image, 0, 255, 0);
$navy = ImageColorAllocate($image, 0, 0, 153);
$purple = ImageColorAllocate($image, 153, 0, 153);
$maroon = ImageColorAllocate($image, 153, 0, 0);
$olive = ImageColorAllocate($image, 153, 153, 0);

// don't forget the font colour
$fontcolor = $red;


// ---------- DO NOT EDIT ANYTHING BELOW ----------

//gets the difference between today and $bornon
$today = mktime();
$bornon = mktime(0, 0, 0, $born_month, $born_day, $born_year);
$diff = ($today - $bornon);

$bornon2 = mktime(0, 0, 0, $born_month2, $born_day2, $born_year2);
$diff2 = ($today - $bornon2);


// calculate days, months, years
$years = floor($diff/(60*60*24*365));
$months = floor( ($diff - ($years * (60*60*24*365)))/(60*60*24*(365/12)));
$days = floor(($diff - ($years * (60*60*24*365)) - ($months * (60*60*24*(365/12))))/(60*60*24));

$years2 = floor($diff2/(60*60*24*365));
$months2 = floor( ($diff2 - ($years2 * (60*60*24*365)))/(60*60*24*(365/12)));
$days2 = floor(($diff2 - ($years2 * (60*60*24*365)) - ($months2 * (60*60*24*(365/12))))/(60*60*24));

// text to superimpose onto the image
$text = sprintf("CHILDS NAME OR ANY TEXT HERE is %s years, %s months and %s days old",$years, $months, $days); 
$text2 = sprintf("CHILDS NAME OR ANY TEXT HERE is %s years, %s months and %s days old",$years2, $months2, $days2); 

// make image
imagettftext($image, $fontsize, $slant, $margin_left, $margin_top, $fontcolor, $font, $text);
imagettftext($image, $fontsize, $slant, $margin_left, $margin_top+$gap, $fontcolor, $font, $text2);

// display image
imagepng($image);

// all done...
imagedestroy($image);
?>
Post Reply