Jump to content

Recommended Posts

need some help with resizing a font image . this is a banner script someone helped me with im not really good with php so im looking for help . this script displays a banner with the persons name on it but some people have longer names so i just want it to automatically resize the name in the banner and have it center can anyone help?

 

<?
///:)/////////////////////////////////////////////////////
//            Jamroom Band-Banner Script            //
//               Build by: Gilbert Romero                   //
//               Lizens:                      //
//               Date: today                     //
//////////////////////////////////////////////////////

//The DB details  (change to your details)
$res=mysql_connect("localhost","","") or die();
mysql_select_db("");

//just a little db helper
function SQLSelectRow($SQL) {
$Res = mysql_query( $SQL );
    if ( $Res ) {
        $Row = mysql_fetch_row( $Res );
        return $Row;
    } else {
        return false;
    }
   
}

//Get the DB DATA
//($Loginteile is given in the image name, first number is Login/Band ID (7_2_222_050_050.jpg))
$LoginteileRaw = explode('.', $_SERVER['PATH_INFO']);
$Loginteile = explode('_', substr($LoginteileRaw [0],1));
$BandID = $Loginteile[0];

$SQL="   SELECT    band_name
      FROM    jamroom_band_info
      WHERE    band_active = '1'
      AND    band_id = '$BandID'
      ";

list($dbBandName) = SQLSelectRow($SQL);

$dbBCA = unserialize($dbBandCounts);

//TXT SIZES
$FontSmall = 2.2;
$FontBig=35;

//BACKGROUND (the "Banner")
$image = imagecreatefrompng("wwbad2a.png");

//FONT Types($Loginteile is given in the image name, second number (7_2_222_050_050.jpg))
//change YOURFONT to your chosen TTF fonts
if ($Loginteile[1]=='1' OR !$Loginteile[1]) $fontTTF = "Friday13.ttf";
    elseif ($Loginteile[1]=='2') $fontTTF = "fivecent.ttf";
    elseif ($Loginteile[1]=='3') $fontTTF = "damnarc.ttf";
    else  $fontTTF = "/trab/jamroom/verdanab.ttf";

//NamePosition Top
$nameTop = 45;
//NamePosition Left
$nameLeft = 45;

//TXT COLOR ($Loginteile is given in the image name, last 3 numbers RGB (7_2_222_050_050.jpg))
if ( $Loginteile[2] AND $Loginteile[3] AND $Loginteile[4] ) $TXT_color = imagecolorallocate($image, $Loginteile[2], $Loginteile[3], $Loginteile[4]);
else $TXT_color = imagecolorallocate($image, 154, 180, 166);
// Text Shadow color
$TXT_colorShadow = imagecolorallocate($image, 0, 0, 0);


//The TEXT (Infos)
imagestring($image, $FontSmall, 7, 5, " $dbBCA[FAN_COUNT]", $TXT_color);

imagestring($image, $FontSmall, 7, 17, " $dbBCA[sONG_COUNT]", $TXT_color);

imagestring($image, $FontSmall, 7, 29, " $dbBCA[VIDEO_COUNT]", $TXT_color);

imagestring($image, $FontSmall, 7, 41, " $dbBCA[PHOTO_COUNT]", $TXT_color);

list($Continent,$Country)=split(":",$dbBandLocation);

imagestring($image, $FontSmall, 100, 29, "$Country", $TXT_color);

imagestring($image, $FontSmall, 100, 41, " $dbBandSoundslike", $TXT_color);

imagestring($image, $FontSmall, 417, 41, "$Recorde", $TXT_color);


//build the Band Name (shadow)
for ($ox = -2; $ox <= 2; $ox++) {
   for ($oy = -2; $oy <= 2; $oy++) {
            imagettftext($image, $FontBig , 0, $nameLeft+$ox, $nameTop+$oy, $TXT_colorShadow ,$fontTTF,  $dbBandName);
   }
}
//build the Band Name (foreground)
imagettftext($image, $FontBig , 0, $nameLeft, $nameTop, $TXT_color ,$fontTTF,  $dbBandName);

//Set the header
header("Content-type: image/png");
//DISPLAY IMAGE
imagepng($image); ;
//DELETE IMAGE(from server "CACHE")
imagedestroy($image);

?>

Link to comment
https://forums.phpfreaks.com/topic/131104-image-resize-help/
Share on other sites

This isn't necessarily a cut-and-paste solution, but it should give you an idea of how to go about finding out the values and adjustments etc.  You should consider finding out what maximum number of characters in a name can be printed successfully, then adjust the font size as needed based on the length of the name if it exceeds that number.

 

<?php
$fontsize=10;
$maxprintable=6;
$name='This Is A Long Name.';
$namelength=strlen($name);
if($namelength>$maxprintable){
  $fontsize=($maxprintable/$namelength)*$fontsize;
}
echo $fontsize;
?>

Link to comment
https://forums.phpfreaks.com/topic/131104-image-resize-help/#findComment-681581
Share on other sites

Here's an article on how to center text in an image (which includes a section on determining the length of text in an image).

 

http://www.bitrepository.com/web-programming/php/how-to-center-a-text-on-an-image-using-gd.html

 

However, I question the validity of imagefontwidth() to determine the width of a string of text. imagefontwidth() only takes one parameter (the font size). So while it would work fine for a fixed-width font such as courier, I don't see how it could work for a non fixed-width font such as times.

Link to comment
https://forums.phpfreaks.com/topic/131104-image-resize-help/#findComment-682065
Share on other sites

It would seem the recommendation to test with the font's widest character is a valid one.  Otherwise, measure the pixel width of each character, store that in an array, then use array_count_values on preg_match_all output and add up the pixel widths of the character counts found (seems more work than necessary though).

Link to comment
https://forums.phpfreaks.com/topic/131104-image-resize-help/#findComment-682069
Share on other sites

Here's a quick script that will reduce the font so the text will fit within the image width (including defined margins) and will also center the text.

 

The result seems to be a little off center to the right. Not sure why and I need to get back to work so I don't have time to investigate further. Adding a space to the end of the text makes it look better for a quick fix.

 

$image = imagecreatefrompng("wwbad2a.png");
$image_size = getimagesize("wwbad2a.png");

$fontTTF = "verdanab.ttf";
$text_color = imagecolorallocate($image, 255, 255, 255);

$font_max_size = 35;
$angle = 0;
$x_position = 20;
$y_margins = 30;
$y_width = $image_size[0] - (2 * $y_margins);

$band_name = "Michael";
$font_size = $font_max_size;

$text_sizes = imagettfbbox($font_size, $angle, $fontTTF, $band_name);
$text_width = ($text_sizes[2] - $text_sizes[0]);

//Decrease font size until it will fit in image
while ($text_width > $y_width)
{
    $font_size--;
    $text_sizes = imagettfbbox($font_size, $angle, $fontTTF, $band_name);
    $text_width = ($text_sizes[2] - $text_sizes[0]);
}


//Center horizontally
$y_position = ($image_size[0] - $text_width)/2;

imagettftext($image, $font_size , $angle, $y_position, 250, $text_color, $fontTTF,  $band_name);


//Set the header
header("Content-type: image/png");
//DISPLAY IMAGE
imagepng($image); ;
//DELETE IMAGE(from server "CACHE")
imagedestroy($image);

Link to comment
https://forums.phpfreaks.com/topic/131104-image-resize-help/#findComment-684722
Share on other sites

heres what it looks like so far with a long profile name

 

http://worldwidebeats.net/wwbadway.php/37_3_243_252_3.jpg

 

So is that what you want? It looks like there is a letter at the very end that supposed to start a new word, so I'm guessing not. Post the current code if you want help.

Link to comment
https://forums.phpfreaks.com/topic/131104-image-resize-help/#findComment-685308
Share on other sites

<?
///:)/////////////////////////////////////////////////////
//            Jamroom Band-Banner Script               //
//               Build by: Gilbert Romero                        //
//               Lizens:                             //
//               Date: today                          //
//////////////////////////////////////////////////////

//The DB details  (change to your details)
$res=mysql_connect("localhost","","") or die();
mysql_select_db("");

//just a little db helper
function SQLSelectRow($SQL) {
$Res = mysql_query( $SQL );
    if ( $Res ) {
        $Row = mysql_fetch_row( $Res );
        return $Row;
    } else {
        return false;
    }
   
}

//Get the DB DATA
//($Loginteile is given in the image name, first number is Login/Band ID (7_2_222_050_050.jpg))
$LoginteileRaw = explode('.', $_SERVER['PATH_INFO']);
$Loginteile = explode('_', substr($LoginteileRaw [0],1));
$BandID = $Loginteile[0];

$SQL="   SELECT    band_name
      FROM    jamroom_band_info
      WHERE    band_active = '1'
      AND    band_id = '$BandID'
      ";

list($dbBandName) = SQLSelectRow($SQL);

$dbBCA = unserialize($dbBandCounts);

//TXT SIZES
$FontSmall = 2.2;
$FontBig=30;

//BACKGROUND (the "Banner")
$image = imagecreatefrompng("wwbad2a.png");

//FONT Types($Loginteile is given in the image name, second number (7_2_222_050_050.jpg))
//change YOURFONT to your chosen TTF fonts
if ($Loginteile[1]=='1' OR !$Loginteile[1]) $fontTTF = "Friday13.ttf";
    elseif ($Loginteile[1]=='2') $fontTTF = "fivecent.ttf";
    elseif ($Loginteile[1]=='3') $fontTTF = "damnarc.ttf";
    else  $fontTTF = "/trab/jamroom/verdanab.ttf";

//NamePosition Top
$nameTop = 45;
//NamePosition Left
$nameLeft = 100;

//TXT COLOR ($Loginteile is given in the image name, last 3 numbers RGB (7_2_222_050_050.jpg))
if ( $Loginteile[2] AND $Loginteile[3] AND $Loginteile[4] ) $TXT_color = imagecolorallocate($image, $Loginteile[2], $Loginteile[3], $Loginteile[4]);
else $TXT_color = imagecolorallocate($image, 154, 180, 166);
// Text Shadow color
$TXT_colorShadow = imagecolorallocate($image, 0, 0, 0);


//The TEXT (Infos)
imagestring($image, $FontSmall, 7, 5, " $dbBCA[FAN_COUNT]", $TXT_color);

imagestring($image, $FontSmall, 7, 17, " $dbBCA[sONG_COUNT]", $TXT_color);

imagestring($image, $FontSmall, 7, 29, " $dbBCA[VIDEO_COUNT]", $TXT_color);

imagestring($image, $FontSmall, 7, 41, " $dbBCA[PHOTO_COUNT]", $TXT_color);

list($Continent,$Country)=split(":",$dbBandLocation);

imagestring($image, $FontSmall, 100, 29, "$Country", $TXT_color);

imagestring($image, $FontSmall, 100, 41, " $dbBandSoundslike", $TXT_color);

imagestring($image, $FontSmall, 417, 41, "$Recorde", $TXT_color);


//build the Band Name (shadow)
for ($ox = -2; $ox <= 2; $ox++) {
   for ($oy = -2; $oy <= 2; $oy++) {
            imagettftext($image, $FontBig , 0, $nameLeft+$ox, $nameTop+$oy, $TXT_colorShadow ,$fontTTF,  $dbBandName);
   }
}
//build the Band Name (foreground)
imagettftext($image, $FontBig , 0, $nameLeft, $nameTop, $TXT_color ,$fontTTF,  $dbBandName);

//Set the header
header("Content-type: image/png");
//DISPLAY IMAGE
imagepng($image); ;
//DELETE IMAGE(from server "CACHE")
imagedestroy($image);

// The file
$filename = 'damnarc.ttf';

// Set a maximum height and width
$width = 100;
$height = 45;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);

$image = imagecreatefrompng("wwbad2a.png");
$image_size = getimagesize("wwbad2a.png");

$fontTTF = "verdanab.ttf";
$text_color = imagecolorallocate($image, 255, 255, 255);

$font_max_size = 35;
$angle = 0;
$x_position = 20;
$y_margins = 30;
$y_width = $image_size[0] - (2 * $y_margins);

$band_name = "Michael the artist raise";
$font_size = $font_max_size;

$text_sizes = imagettfbbox($font_size, $angle, $fontTTF, $band_name);
$text_width = ($text_sizes[2] - $text_sizes[0]);

//Decrease font size until it will fit in image
while ($text_width > $y_width)
{
    $font_size--;
    $text_sizes = imagettfbbox($font_size, $angle, $fontTTF, $band_name);
    $text_width = ($text_sizes[2] - $text_sizes[0]);
}


//Center horizontally
$y_position = ($image_size[0] - $text_width)/2;

imagettftext($image, $font_size , $angle, $y_position, 250, $text_color, $fontTTF,  $band_name);


//Set the header
header("Content-type: image/png");
//DISPLAY IMAGE
imagepng($image); ;
//DELETE IMAGE(from server "CACHE")
imagedestroy($image);

?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/131104-image-resize-help/#findComment-685844
Share on other sites

It look like all you did was tack the code I provided at the end of the code you already had?! The code I provided isn't even getting displayed becuase you have already sent the data to the browser. The logic in that code is all over the palce. Have the code build one piece of the puzzle in a logical sequence. For example the code to determine the band name font size & color comes before you put the other text on the page.

 

I tried to rewite it to work accordingly, but since I dont have your database at my disposal I have not tested it.

 

///:)/////////////////////////////////////////////////////
//            Jamroom Band-Banner Script               //
//               Build by: Gilbert Romero                        //
//               Lizens:                             //
//               Date: today                          //
//////////////////////////////////////////////////////

//The DB details  (change to your details)
$res=mysql_connect("localhost","","") or die();
mysql_select_db("");

//just a little db helper
function SQLSelectRow($SQL) {
$Res = mysql_query( $SQL );
    if ( $Res ) {
        $Row = mysql_fetch_row( $Res );
        return $Row;
    } else {
        return false;
    }
}

//Get the DB DATA
//($Loginteile is given in the image name, first number is Login/Band ID (7_2_222_050_050.jpg))
$LoginteileRaw = explode('.', $_SERVER['PATH_INFO']);
$Loginteile = explode('_', substr($LoginteileRaw[0],1));
$BandID = $Loginteile[0];

$SQL="SELECT band_name
      FROM jamroom_band_info
      WHERE band_active = '1'
        AND band_id = '$BandID' ";

list($dbBandName) = SQLSelectRow($SQL);

$dbBCA = unserialize($dbBandCounts);

//BACKGROUND (the "Banner")
$image = imagecreatefrompng("wwbad2a.png");

//TXT SIZE
$FontSmall = 2.2;

//TXT COLOR ($Loginteile is given in the image name, last 3 numbers RGB (7_2_222_050_050.jpg))
if ( $Loginteile[2] AND $Loginteile[3] AND $Loginteile[4] )
    $TXT_color = imagecolorallocate($image, $Loginteile[2], $Loginteile[3], $Loginteile[4]);
else
    $TXT_color = imagecolorallocate($image, 154, 180, 166);

//The TEXT (Infos)
imagestring($image, $FontSmall, 7, 5,  " $dbBCA[FAN_COUNT]",   $TXT_color);
imagestring($image, $FontSmall, 7, 17, " $dbBCA[sONG_COUNT]",  $TXT_color);
imagestring($image, $FontSmall, 7, 29, " $dbBCA[VIDEO_COUNT]", $TXT_color);
imagestring($image, $FontSmall, 7, 41, " $dbBCA[PHOTO_COUNT]", $TXT_color);
list($Continent,$Country)=split(":",$dbBandLocation);
imagestring($image, $FontSmall, 100, 29, "$Country", $TXT_color);
imagestring($image, $FontSmall, 100, 41, " $dbBandSoundslike", $TXT_color);
imagestring($image, $FontSmall, 417, 41, "$Recorde", $TXT_color);


//FONT Types($Loginteile is given in the image name, second number (7_2_222_050_050.jpg))
//change YOURFONT to your chosen TTF fonts
if ($Loginteile[1]=='1' OR !$Loginteile[1]) $fontTTF = "Friday13.ttf";
    elseif ($Loginteile[1]=='2') $fontTTF = "fivecent.ttf";
    elseif ($Loginteile[1]=='3') $fontTTF = "damnarc.ttf";
    else  $fontTTF = "/trab/jamroom/verdanab.ttf";

// Text Shadow color
$TXT_colorShadow = imagecolorallocate($image, 0, 0, 0);
//Name max font size
$font_max_size = 35;
//Angle of name text
$angle = 0;
//NamePosition Top
$y_position = 45;
//Left and right margins
$x_margins = 100;
//NamePosition Left
$image_size = getimagesize("wwbad2a.png");
$x_width = $image_size[0] - (2 * $x_margins);

$font_size = $font_max_size;

$text_sizes = imagettfbbox($font_size, $angle, $fontTTF, $dbBandName);
$text_width = ($text_sizes[2] - $text_sizes[0]);

while ($text_width > $x_width)
{
    $font_size--;
    $text_sizes = imagettfbbox($font_size, $angle, $fontTTF, $dbBandName);
    $text_width = ($text_sizes[2] - $text_sizes[0]);
}

//Center horizontally
$x_position = ($image_size[0]-$text_width)/2;

imagettftext($image, $font_size , $angle, $x_position, $y_position, $text_color, $fontTTF,  $band_name);

//Set the header
header("Content-type: image/png");
//DISPLAY IMAGE
imagepng($image); ;
//DELETE IMAGE(from server "CACHE")
imagedestroy($image

Link to comment
https://forums.phpfreaks.com/topic/131104-image-resize-help/#findComment-686206
Share on other sites

OK, I've tried to incorporate my script into your original script (although I really don't like the flow of the page)

 

<?php
///:)/////////////////////////////////////////////////////
//            Jamroom Band-Banner Script            //
//               Build by: Gilbert Romero                   //
//               Lizens:                      //
//               Date: today                     //
//////////////////////////////////////////////////////

//The DB details  (change to your details)
$res=mysql_connect("localhost","","") or die();
mysql_select_db("");

//just a little db helper
function SQLSelectRow($SQL) {
$Res = mysql_query( $SQL );
    if ( $Res ) {
        $Row = mysql_fetch_row( $Res );
        return $Row;
    } else {
        return false;
    }
}

//Get the DB DATA
//($Loginteile is given in the image name, first number is Login/Band ID (7_2_222_050_050.jpg))
$LoginteileRaw = explode('.', $_SERVER['PATH_INFO']);
$Loginteile = explode('_', substr($LoginteileRaw [0],1));
$BandID = $Loginteile[0];

$SQL="SELECT band_name
      FROM jamroom_band_info
      WHERE band_active = '1'
        AND band_id = '$BandID'";

list($dbBandName) = SQLSelectRow($SQL);

$dbBCA = unserialize($dbBandCounts);


//BACKGROUND (the "Banner")
$image_file = "wwbad2a.png";
$image = imagecreatefrompng($image_file);

//FONT Types($Loginteile is given in the image name, second number (7_2_222_050_050.jpg))
//change YOURFONT to your chosen TTF fonts
if ($Loginteile[1]=='1' OR !$Loginteile[1]) $fontTTF = "Friday13.ttf";
    elseif ($Loginteile[1]=='2') $fontTTF = "fivecent.ttf";
    elseif ($Loginteile[1]=='3') $fontTTF = "damnarc.ttf";
    else  $fontTTF = "/trab/jamroom/verdanab.ttf";


//TXT COLOR ($Loginteile is given in the image name, last 3 numbers RGB (7_2_222_050_050.jpg))
if ( $Loginteile[2] AND $Loginteile[3] AND $Loginteile[4] ) $TXT_color = imagecolorallocate($image, $Loginteile[2], $Loginteile[3], $Loginteile[4]);
else $TXT_color = imagecolorallocate($image, 154, 180, 166);
// Text Shadow color
$TXT_colorShadow = imagecolorallocate($image, 0, 0, 0);

//TXT SIZES
$FontSmall = 2.2;

//The TEXT (Infos)
imagestring($image, $FontSmall, 7, 5, " $dbBCA[FAN_COUNT]", $TXT_color);
imagestring($image, $FontSmall, 7, 17, " $dbBCA[sONG_COUNT]", $TXT_color);
imagestring($image, $FontSmall, 7, 29, " $dbBCA[VIDEO_COUNT]", $TXT_color);
imagestring($image, $FontSmall, 7, 41, " $dbBCA[PHOTO_COUNT]", $TXT_color);
list($Continent,$Country)=split(":",$dbBandLocation);
imagestring($image, $FontSmall, 100, 29, "$Country", $TXT_color);
imagestring($image, $FontSmall, 100, 41, " $dbBandSoundslike", $TXT_color);
imagestring($image, $FontSmall, 417, 41, "$Recorde", $TXT_color);


//NamePosition Top
$nameTop = 45;
//NamePosition Left
$nameMargin = 45;
//Image Size
$image_size = getimagesize($image_file);
//Available text width
$nameWidthAvail = $image_size[0] - (2 * $nameMargin);
//Font size for band name
$band_font=35;

$font_size = $font_max_size;
$text_sizes = imagettfbbox($font_size, 0, $fontTTF, $dbBandName);
$text_width = ($text_sizes[2] - $text_sizes[0]);

//Decrease font size until it will fit in image
while ($text_width > $nameWidthAvail)
{
    $font_size--;
    $text_sizes = imagettfbbox($font_size, 0, $fontTTF, $dbBandName);
    $text_width = ($text_sizes[2] - $text_sizes[0]);
}

//Center horizontally
$nameLeft = ($image_size[0] - $text_width)/2;

//build the Band Name (shadow)
for ($ox = -2; $ox <= 2; $ox++) {
   for ($oy = -2; $oy <= 2; $oy++) {
    imagettftext($image, $band_font, 0, $nameLeft+$ox, $nameTop+$oy, $TXT_colorShadow ,$fontTTF,  $dbBandName);
   }
}
//Write the band name text
imagettftext($image, $band_font, 0, $nameLeft, $nameTop, $TXT_color ,$fontTTF,  $dbBandName);


//Set the header
header("Content-type: image/png");
//DISPLAY IMAGE
imagepng($image); ;
//DELETE IMAGE(from server "CACHE")
imagedestroy($image);

?>

Link to comment
https://forums.phpfreaks.com/topic/131104-image-resize-help/#findComment-688791
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.