Jump to content

Using different fonts in PHP generated images


cactuscake

Recommended Posts

Me again! Received some useful help from these forums in the last couple of days so I'm back with another question.

 

The snippet of code below pieces together an image using an uploaded image file, a bunch of user-defined form field values, and some server side image resources. It works, but I've been wanting to change the font from the default (rather ugly) to something like arial or courier, and maybe at a later stage give the user the option to choose the font from a short list.

 

Not asking someone to do it for me, but if you've done this sort of thing before perhaps point me in the direction of any resources that helped?

 

Thanks.

 

<?php
$imagepath = $_FILES['fileatt']['tmp_name'];
    $now = time();
    // Load image
    $image = open_image($imagepath);

    if ($image == false) {
        die ('<strong>Invalid type/size or no image file specified. Please go back and try again with an image file under 2Mb.</strong>');		}

    // Get original width and height
    $width = imagesx($image);
    $height = imagesy($image);

if ($width > $height) {
        $new_width = floatval($_POST['new_width']);
        $new_height = $height * ($new_width/$width);		}

    // New height? Calculate new width
    else {
        $new_height = floatval($_POST['new_height']);
        $new_width = $width * ($new_height/$height);
    }      

    // Resample
    $image_resized = imagecreatetruecolor($new_width + 358, $new_height + ;	$bgColor = imagecolorallocate($image_resized, 255,255,255);
$trans = imagecolorallocatealpha  ($image_resized, 255, 255, 255, 127);	imagefill($image_resized, 0,0 , $trans);
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);	// Merge with bottom shadow	
$shadowbot = imagecreatefromgif ( "card_graphics/shadowbot.gif" );
imagecopy($image_resized, $shadowbot, 0, $new_height, 0, 0, $new_width, ;	// Merge with side shadow	
$shadowrit = imagecreatefromgif ( "card_graphics/shadowrit.gif" );
imagecopy($image_resized, $shadowrit, $new_width + 350, 0, 0, 0, 8, $new_height);	// Merge with corner shadow	
$shadowcor = imagecreatefromgif ( "card_graphics/shadowcor.gif" );
imagecopy($image_resized, $shadowcor, $new_width, $new_height, 0, 0, 358, ;	// Merge with postmark	
$postmark = imagecreatefromgif ( "card_graphics/postmark.gif" );
imagecopy($image_resized, $postmark, $new_width, 0, 0, 0, 350, 75);	// Merge with message	
$string = stripslashes($_POST['text']);                                             
$font  = 5;
$line_height = ImageFontheight($font) + 5;
$padding = (is_numeric($padding)) ? $padding : 0;
$text = wordwrap($string, ((315 - (1.5 * $padding))/ImageFontWidth($font)));
$lines = explode("\n", $text);
imagefill($image_resized, 0, 0, $trans);
$i = 90;
foreach($lines as $line){
imagestring($image_resized, $font, ($new_width +15), $i, trim($line), $colour);
$i += $line_height; }	$targetpath = "sentcards/". $now . basename( $_FILES['fileatt']['name']);	imagejpeg ($image_resized, $targetpath, 100);
?>

Link to comment
Share on other sites

Ah, now I remember why I couldn't sort this out before - I don't think the required GD stuff is enabled on my server (php config below).

 

Are there any workarounds for this?

 

'./configure' '--enable-memory-limit' '--disable-debug' '--with-regex=php' '--disable-rpath' '--disable-static' '--with-pic' '--with-layout=GNU' '--with-pear=/usr/share/php' '--enable-calendar' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-track-vars' '--enable-trans-sid' '--enable-bcmath' '--with-bz2' '--enable-ctype' '--with-db4' '--with-iconv' '--enable-exif' '--enable-filepro' '--enable-ftp' '--with-gettext' '--enable-mbstring' '--with-pcre-regex=/usr' '--enable-shmop' '--enable-sockets' '--enable-wddx' '--disable-xml' '--with-expat-dir=/usr' '--with-xmlrpc' '--enable-yp' '--enable-dio' '--with-zlib' '--without-pgsql' '--with-kerberos=/usr' '--with-openssl=/usr' '--with-zip=/usr' '--enable-dbx' '--with-mime-magic=/usr/share/misc/file/magic.mime' '--with-exec-dir=/usr/lib/php4/libexec' '--prefix=/usr' '--with-apxs2=/usr/bin/apxs2' '--with-config-file-path=/etc/php4/apache2' '--with-jpeg-dir=/usr/lib' '--with-gd' '--with-curl' '--with-mysql'

Link to comment
Share on other sites

I get this:

 

Fatal error: Call to undefined function imagettftext() in /www/110mb.com/j/o/e/m/a/l/p/a/joemalpass/htdocs/fonttest.php on line 22

 

(using the following test script)

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

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

 

Other sources suggest I need other GD thingies enabled, but I don't have control over the configuration so I can't enable them.

Link to comment
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.