FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


ImageMagick - How to add text?





Hogwarts
I just finished installing ImageMagick on my server, and have spent the last half hour searching for how to actually put text into an image :S



I've been trying to get text into the left of the image, and to get the text from http://www.phpinstall.net/frihost.html - Which is maintained by my bot running in that channel.

I just have no idea how to actually do this, though : Confused

(By the way, the point of this image is to dynamically update itself every so often to show how many people are in #frihost on Dal.Net)

Thanks - Tobias Confused
Star Wars Fanatic
You can use several different functions for that, one I frequently use is imagettftext.

Here is a list of Image functions, http://www.php.net/manual/en/ref.image.php
bgillingham
The imagettftext is a GD call if I am not mistaken. ImageMagick brings a lot more power to the party - so, here's some info on it that will hopefully help.

Adding text to an image is easy when you learn to speak to the ImageMagick command interpreter. It is kind of like working on a photoshop image - only one text command at a time. With more complex sets of actions, you will probably need to use some calls to compose on top of calls to convert. Basically, most calls to ImageMagick are going to be the "convert ..." type.

For the ImageMagick calls, one of the toughest things that I have had to deal with is actually getting the calls to reference the fonts that may be on the domain already. There are ways to use the system fonts, but the easiest thing to do would be to reference your own TTF fonts - create a new directory called "fonts" and put all of your TTF files in there.

Be warned that some image types do not support transparency. Also, know that more complex routines are needed for special effects and transparent layers.

With PHP, all of these calls are sent to the DLL via a call to "passthru". Using this method really removes you from any feedback, so your code should check for success of calls that create images by using @file_exists() including the prefix "@" to suppress any warning message if the call fails.

The details of converting an image into an image that has text on it can all be found at ImageMagick's website, but I will post a few lines of code here to get you started:
To load an image and put text into it:
Code:
  // if you develop locally : you might have a function written or a variable set that
  // you can check to set this path based on which environment you're currently in
  $image_magick = (islocal() === TRUE) ? "c:\\ImageMagick\\convert.exe" : "/usr/bin/convert";
  $font_selection = "fonts/times.ttf";

  $source_image = "images/sample.jpg";
  $target_image = "images/created.jpg";
  $text = "something to print";

  $command = $image_magick.' -resize 256x148 "'.$source_image.'" '.
    ' -font "'.$font_selection.'" -pointsize 64 -fill black '.
    ' -draw "text 30, 60 \''.$text.'\'"  "'.$target_image.'"';

  passthru($command);

Code:
function islocal() {
  $host = trim (getenv("HTTP_HOST"));
  $found = strpos($host, '127.0.0.1');
  if ($found === 0)
    return TRUE;
  else
    return FALSE;
}


This code will only create the file - if you need to redirect to that newly created file, add this block of code:
Code:
  if (@file_exists($target_image) ) {
    header("Content-type: image/jpeg");
    header("location: $target_image");
  } else {
    echo "There was a problem creating the file \"".$target_image."\<br>";
  }


Now, to list a set of fonts that you have in a subfolder, here's a couple of functions to create a form's selection options for them:
Code:
function getFontSelections() {
  $retval = "<select size='4' name='font_selection'>";

  $folder=dir("fonts/"); //open directory

  while($font=$folder->read())
    if ( (stristr($font,'.TTF')) || (stristr($font,'.ttf')) ) //only ttf fonts
       $fonts[]=$font;

  $folder->close();

  if (!empty($fonts))
  {
    sort($fonts);
    foreach ($fonts as $font) {
      // you can get a friendly name for the font - as well as display each
      // using their font-family name by using a helper function "getfontStyleName"
      $fnameonly = getfontStyleName($font);
      $retval.= "<option value='$font' style='font-family:$fnameonly'>$fnameonly</option>";
    }
  }
  $retval .= "</select>";
  return $retval;
}

function getfontStyleName($font) {
  $font = strtolower($font);
  $fontstyle='Verdana';
  if ($font == "agencyb.ttf") $fontstyle="Agency FB";
  elseif ($font == "agencyr.ttf") $fontstyle="Agency FB Bold";
  elseif ($font == "agent orange.ttf") $fontstyle="Agent Orange";
  elseif ($font == "alger.ttf") $fontstyle="Algerian";
  elseif ($font == "arial.ttf") $fontstyle="Arial";
  ...
  ...
  return $fontstyle;
}
Related topics
Bookmark Script:
creat web elements with photo impact
PHP Image Upload (MySQL/Folder)
Easy Video Capture 1.30
[RESOLVED] How to add image or text to link in signature?
Gimp - Sig making:N00b to pro!(finally finished)
Website themes in HTML???
PHP image
Freetype Support for GD2
How Do You make that thingo.
Textareas and Javascript
Problem adding text to Longtext field
how to add text link in signechure
Text-to-speech applications
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.