Schrift ändern :-(

  • Themenstarter Themenstarter fercules
  • Beginndatum Beginndatum
F

fercules

Endlich ist es mir gelungen, transparente PNGs herzustellen und so das GIF-Lizenzierungsproblem (gruß an unisys) zu umgehen.

das script dass ich im internet aufgetan habe, müsste jetzt nur noch in der lange sein, den text in einer bestimmten schriftart auszugeben. derzeit wird eine "editor" standardschrift verwendet, lieber jedoch hätte ich arial. leider konnte ich in meinem php buch nicht feststellen, wie ich diese feature in dises script implementiere. hat jemand ne idee? hier die sourcen:

example.php:
<?
/*
Pull in all the requirements
*/
require_once("class.img_add_txt.php");


/*
coupon image
*/
$config=array(
"text" => "Test",
"text_colors" => "0 0 0", // RGB Seperated by spaces
"image_loc" => "alt.png",
"image_type" => "PNG", // PNG and GIF Supported
// Optional arguments; default is center area on image
"x_pos" => "",
"y_pos" => "",

);

$graphic=new img_add_txt($config);
?>


class.php:
<?
class img_add_txt{


/*
Created by Richard Sumilang
http://www.richard-sumilang.com


object img_add_txt($array);
------------------------------------
This function adds text on top of an image

Usage:
------------------------------------
$config=array(
"text" => "Coupon: Here is your free coupon",
"text_colors" => "255 68 0", // RGB Seperated by spaces
"image_loc" => "example.png",
"image_type" => "PNG", // PNG and GIF Supported
// Optional arguments; default is center area on image
"x_pos" => "",
"y_pos" => "",

);

$graphic=new img_add_txt($config);
*/

function img_add_txt($config){

// header
//header("Content-Type: image/gif");
$this->func_header($config['image_type']);

// set up image
$im = ImageCreateFromPNG($config['image_loc']);

// Set up text colors
$text_colors=explode(" ", $config['text_colors']);
$text_color = ImageColorAllocate($im, $text_colors['0'], $text_colors['1'], $text_colors['2']);

// get font dimensiona
$font_height = ImageFontHeight(3);
$font_width = ImageFontWidth(3);

// get image dimensiona
$image_height = ImageSY($im);
$image_width = ImageSX($im);

// get string length
$length = $font_width * strlen($config['text']);

// set the x, y cords of where the text will be placed
if(empty($config['x_pos'])){
// calculate start coordinates for string
$image_center_x = ($image_width/2)-($length/2);
}else{
$image_center_x = $config['x_pos'];
}
if(empty($config['y_pos'])){
// calculate start coordinates for string
$image_center_y = ($image_height/2)-($font_height/2);
}else{
$image_center_y = $config['y_pos'];
}

// write string
ImageString($im, 3, $image_center_x, $image_center_y, $config['text'], $text_color);

// output to browser
$this->output_image($config['image_type'], $im);

}// End img_add_txt


/*
Output the correct header based
on file type
*/
function func_header($var){

switch($var){
case "PNG":
header("Content-Type: image/png");
break;

case "GIF":
header("Content-Type: image/gif");
break;

case "JPEG":
header("Content-Type: image/jpeg");
break;
}

}// End func_header


/*
Output the correct image type
based on type
*/
function output_image($var, $pointer){

switch($var){
case "PNG":
ImagePNG($pointer);
break;

case "GIF":
ImageGIF($pointer);
break;

case "JPEG":
ImageJPEG($pointer);
break;
}

} // End out output image

} // End of class
?>

liebe grüße
stephan
 
Diese Zeile ist für dein Problem relevant:
PHP:
// write string
ImageString($im, 3, $image_center_x, $image_center_y, $config['text'], $text_color);

Wenn du eine eigene Schrift definieren willst, dann verwende statt [phpf]imagestring[/phpf] lieber [phpf]imagettftext[/phpf], bei dieser kannst du eine TrueType-Schriftart angeben, mit der dann der Text auf dein Bild geschrieben wird.

Beispiel:
PHP:
imagettftext($im, 12, 0, $image_center_x, $image_center_y, $text_color, '/dein/pfad/zur/arial. ttf',  $config['text']);
Die arial. ttf musst du zuvor natürlich auf deinen Webserver laden.


PS: Das Leerzeichen zwischen arial. und ttf musste ich nur wegen des Boards machen, das schreibt sonst arialf :eek:
 
genial!

hallo ludz,

wir hatten doch schonmal das vergnügen :-) vielen dank für deine schnelle hilfe, hat mir sehr geholfen. demnächst weiß ichs jetzt besser. und dass ich arial zuvor hochladen musste, war selbst mir klar :-))

also, herzlichen dank! :-)

grüße
stephan
 
Zurück