note 94674 deleted from function.imagettfbbox by danbrown
| From: | danbrown@php.net | Date: | Tue, 17 Nov 2009 19:34:28 +0000 |
| Subject: | note 94674 deleted from function.imagettfbbox by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163009@lists.php.net to get a copy of this message | ||
Note Submitter: David Ariel Bloomberg
----
<?php
// script: _autofontsizer.php
// is the inverse of "autoblocsizer.php" - it automatically matches the correct font size
// regarding the dimension you provided for your bloc.
// written by David Ariel Bloomberg - november 2009
// 1. IGNIT :
header('Content-type: image/png');
// 2. DEFINE VALUES :
// 2.1. Text string to generate (auto or $_GET)
if(isset($_GET['str']) and $_GET['str'] != ""){
$text = $_GET['str'];
}else{
$text = "Lorem ipsum dolor...";
}
// 2.2. Font to be used (auto or $_GET) - TTF only
// correct with your own path in place !
if(isset($_GET['font']) and $_GET['font'] != ""){
$font = "../phpFonts/TTF/". $_GET['font'] .".ttf";
}else{
$font = "../phpFonts/TTF/". "bauhausb" .".ttf";
}
// 2.3. Bloc width (auto or $_GET)
if(isset($_GET['w']) and $_GET['w'] != ""){
$BLOCwidth = $_GET['w'];
}else{
$BLOCwidth = 640;
}
// 2.4. Bloc height (auto or $_GET)
if(isset($_GET['h']) and $_GET['h'] != ""){
$BLOCheight = $_GET['h'];
}else{
$BLOCheight = 480;
}
// 2.5. Angle (auto or $_GET) - Not used in the example...
if(isset($_GET['angle']) and $_GET['angle'] != ""){
$angle = $_GET['angle'];
}else{
$angle = 0;
}
// 2.6. Alpha output (auto or $_GET) - Boolean
if(isset($_GET['alpha']) and $_GET['alpha'] == "TRUE"){
$_alpha = "TRUE";
}else{
$_alpha = "FALSE";
}
// 3. CALCULATE THE FONT SIZE : (loop till we exceed permitted dimensions
$tryfontsize = 1;
for ($i = 1; $i <= 250; $i++) {
$tryfontsize++;
$zh = imagettfbbox($tryfontsize, $angle, $font, $text);
$zh_4 = $zh[4];
$zh_6 = $zh[6];
$textbloc_width = ($zh_4 - $zh_6);
$textbloc_marginleft = ($zh_6 +1);
$BLOCTRYwidth = ($textbloc_width +(+$textbloc_marginleft *2));
if($BLOCTRYwidth > $BLOCwidth){
break;
}
$fontsize = $tryfontsize;
}
$zh = imagettfbbox($fontsize, $angle, $font, $text);
$zh_0 = $zh[0];
$zh_1 = $zh[1];
$zh_2 = $zh[2];
$zh_3 = $zh[3];
$zh_4 = $zh[4];
$zh_5 = $zh[5];
$zh_6 = $zh[6];
$zh_7 = $zh[7];
$textbloc_width = ($zh_4 - $zh_6);
$textbloc_height = ($zh_1 -($zh_7) );
$textbloc_marginleft = ($zh_6 +1); // +1 is a correction to match math with reality
$textbloc_marginright = $textbloc_marginleft;
$bloc_v_margin = $textbloc_marginleft; // for layout, to have equal margins on the 4 sides
$textbloc_up_low_char = $zh_3;
$BLOCTRYwidth = ($textbloc_width +$textbloc_marginleft +$textbloc_marginright);
$BLOCTRYheight = ($textbloc_height +($bloc_v_margin *2));
$inputTop = ($BLOCheight -($textbloc_up_low_char +$bloc_v_margin) );
// 4. CREATE NEW PNG IMAGE:
$im = imagecreatetruecolor($BLOCwidth, $BLOCheight);
if($_alpha == "TRUE"){
imagealphablending($im, false);
$colorTransparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $colorTransparent);
imagesavealpha($im, true);
}
// define some colors :
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$C25ACC7 = imagecolorallocate($im, 37, 172, 199);
$C69C7DD = imagecolorallocate($im, 105, 199, 221);
if($_alpha != "TRUE"){
imagefilledrectangle($im, 0, 0, $BLOCwidth, $BLOCheight, $C69C7DD);
}
imagettftext($im, $fontsize, 0, 0, $inputTop, $C25ACC7, $font, $text);
// 5. Output to browser
imagepng($im);
imagedestroy($im);
?>