3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
8 * This is the main file of the Image_Text package. This file has to be
9 * included for usage of Image_Text.
11 * This is a simple example script, showing Image_Text's facilities.
13 * -------- Start example --------
15 * require_once 'Image/Text.php';
23 * $text = "EXTERIOR: DAGOBAH -- DAY\nWith Yoda\nstrapped to\n\nhis back, Luke climbs up one of the many thick vines that grow in the swamp until he reaches the Dagobah statistics lab. Panting heavily, he continues his exercises -- grepping, installing new packages, logging in as root, and writing replacements for two-year-old shell scripts in PHP.\nYODA: Code! Yes. A programmer's strength flows from code maintainability. But beware of Perl. Terse syntax... more than one way to do it... default variables. The dark side of code maintainability are they. Easily they flow, quick to join you when code you write. If once you start down the dark path, forever will it dominate your destiny, consume you it will.\nLUKE: Is Perl better than PHP?\nYODA: No... no... no. Orderless, dirtier, more seductive.\nLUKE: But how will I know why PHP is better than Perl?\nYODA: You will know. When your code you try to read six months from now...";
26 * 'canvas' => array('width'=> 600,'height'=> 600), // Generate a new image 600x600 pixel
27 * 'cx' => 300, // Set center to the middle of the canvas
29 * 'width' => 300, // Set text box size
31 * 'line_spacing' => 1, // Normal linespacing
32 * 'angle' => 45, // Text rotated by 45
33 * 'color' => $colors, // Predefined colors
34 * 'background_color' => '#FF0000', //red background
35 * 'max_lines' => 100, // Maximum lines to render
36 * 'min_font_size' => 2, // Minimal/Maximal font size (for automeasurize)
37 * 'max_font_size' => 50,
38 * 'font_path' => './', // Settings for the font file
39 * 'font_file' => 'Vera.ttf',
40 * 'antialias' => true, // Antialiase font rendering
41 * 'halign' => IMAGE_TEXT_ALIGN_RIGHT, // Alignment to the right and middle
42 * 'valign' => IMAGE_TEXT_ALIGN_MIDDLE
45 * // Generate a new Image_Text object
46 * $itext = new Image_Text($text, $options);
48 * // Initialize and check the settings
51 * // Automatically determine optimal font size
52 * $itext->autoMeasurize();
60 * -------- End example --------
62 * PHP versions 4 and 5
64 * LICENSE: This source file is subject to version 3.0 of the PHP license
65 * that is available through the world-wide-web at the following URI:
66 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
67 * the PHP License and are unable to obtain it through the web, please
68 * send a note to license@php.net so we can mail you a copy immediately.
72 * @author Tobias Schlitt <toby@php.net>
73 * @copyright 1997-2005 The PHP Group
74 * @license http://www.php.net/license/3_0.txt PHP License 3.0
75 * @version CVS: $Id: Text.php 235913 2007-05-19 02:54:19Z ssttoo $
76 * @link http://pear.php.net/package/Net_FTP2
77 * @since File available since Release 0.0.1
81 * Require PEAR file for error handling.
83 require_once 'PEAR.php';
86 * Regex to match HTML style hex triples.
88 define("IMAGE_TEXT_REGEX_HTMLCOLOR", "/^[#|]([a-f0-9]{2})?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i");
91 * Defines horizontal alignment to the left of the text box. (This is standard.)
93 define("IMAGE_TEXT_ALIGN_LEFT", "left");
96 * Defines horizontal alignment to the center of the text box.
98 define("IMAGE_TEXT_ALIGN_RIGHT", "right");
101 * Defines horizontal alignment to the center of the text box.
103 define("IMAGE_TEXT_ALIGN_CENTER", "center");
106 * Defines vertical alignment to the to the top of the text box. (This is standard.)
108 define("IMAGE_TEXT_ALIGN_TOP", "top");
111 * Defines vertical alignment to the to the middle of the text box.
113 define("IMAGE_TEXT_ALIGN_MIDDLE", "middle");
116 * Defines vertical alignment to the to the bottom of the text box.
118 define("IMAGE_TEXT_ALIGN_BOTTOM", "bottom");
121 * TODO: This constant is useless until now, since justified alignment does not work yet
123 define("IMAGE_TEXT_ALIGN_JUSTIFY", "justify");
126 * Image_Text - Advanced text maipulations in images
128 * Image_Text provides advanced text manipulation facilities for GD2
129 * image generation with PHP. Simply add text clippings to your images,
130 * let the class automatically determine lines, rotate text boxes around
131 * their center or top left corner. These are only a couple of features
132 * Image_Text provides.
134 * @license http://www.php.net/license/3_0.txt PHP License 3.0
137 * @author Tobias Schlitt <toby@php.net>
138 * @copyright 1997-2005 The PHP Group
139 * @version Release: @package_version@
140 * @link http://pear.php.net/package/Net_FTP
147 * Options array. these options can be set through the constructor or the set() method.
149 * Possible options to set are:
152 * 'x' | This sets the top left coordinates (using x/y) or the center point
153 * 'y' | coordinates (using cx/cy) for your text box. The values from
154 * 'cx' | cx/cy will overwrite x/y.
157 * 'canvas' | You can set different values as a canvas:
158 * | - A gd image resource.
159 * | - An array with 'width' and 'height'.
160 * | - Nothing (the canvas will be measured after the real text size).
162 * 'antialias' | This is usually true. Set it to false to switch antialiasing off.
164 * 'width' | The width and height for your text box.
167 * 'halign' | Alignment of your text inside the textbox. Use alignmnet constants to define
168 * 'valign' | vertical and horizontal alignment.
170 * 'angle' | The angle to rotate your text box.
172 * 'color' | An array of color values. Colors will be rotated in the mode you choose (linewise
173 * | or paragraphwise). Can be in the following formats:
174 * | - String representing HTML style hex couples (+ unusual alpha couple in the first place, optional).
175 * | - Array of int values using 'r', 'g', 'b' and optionally 'a' as keys.
177 * 'color_mode' | The color rotation mode for your color sets. Does only apply if you
178 * | defined multiple colors. Use 'line' or 'paragraph'.
180 * 'background_color' | defines the background color. NULL sets it transparent
181 * 'enable_alpha' | if alpha channel should be enabled. Automatically
182 * | enabled when background_color is set to NULL
184 * 'font_path' | Location of the font to use. The path only gives the directory path (ending with a /).
185 * 'font_file' | The fontfile is given in the 'font_file' option.
187 * 'font_size' | The font size to render text in (will be overwriten, if you use automeasurize).
189 * 'line_spacing' | Measure for the line spacing to use. Default is 0.5.
191 * 'min_font_size' | Automeasurize settings. Try to keep this area as small as possible to get better
192 * 'max_font_size' | performance.
194 * 'image_type' | The type of image (use image type constants). Is default set to PNG.
196 * 'dest_file' | The destination to (optionally) save your file.
201 * @see Image_Text::Image_Text(), Image_Text::set()
204 var $options = array(
217 // text alignment inside the clipping
218 'halign' => IMAGE_TEXT_ALIGN_LEFT,
219 'valign' => IMAGE_TEXT_ALIGN_TOP,
221 // angle to rotate the text clipping
225 'color' => array( '#000000' ),
227 'color_mode' => 'line',
229 'background_color' => '#000000',
230 'enable_alpha' => false,
236 'line_spacing' => 0.5,
238 // automasurizing settings
239 'min_font_size' => 1,
240 'max_font_size' => 100,
242 //max. lines to render
246 'image_type' => IMAGETYPE_PNG,
251 * Contains option names, which can cause re-initialization force.
257 var $_reInits = array('width', 'height', 'canvas', 'angle', 'font_file', 'font_path', 'font_size');
260 * The text you want to render.
269 * Resource ID of the image canvas.
278 * Tokens (each word).
284 var $_tokens = array();
287 * Fullpath to the font.
296 * Contains the bbox of each rendered lines.
305 * Defines in which mode the canvas has be set.
314 * Color indeces returned by imagecolorallocatealpha.
320 var $colors = array();
323 * Width and height of the (rendered) text.
329 var $_realTextSize = array('width' => false, 'height' => false);
341 * Fontsize for which the last measuring process was done.
347 var $_measurizedSize = false;
350 * Is the text object initialized?
361 * Set the text and options. This initializes a new Image_Text object. You must set your text
362 * here. Optinally you can set all options here using the $options parameter. If you finished switching
363 * all options you have to call the init() method first befor doing anything further! See Image_Text::set()
364 * for further information.
366 * @param string $text Text to print.
367 * @param array $options Options.
369 * @see Image_Text::set(), Image_Text::construct(), Image_Text::init()
372 function __construct($text, $options = null)
374 $this->set('text', $text);
376 if (!empty($options)) {
377 $this->options = array_merge($this->options, $options);
383 * Construct and initialize an Image_Text in one step.
384 * This method is called statically and creates plus initializes an Image_Text object.
385 * Beware: You will have to recall init() if you set an option afterwards manually.
387 * @param string $text Text to print.
388 * @param array $options Options.
391 * @see Image_Text::set(), Image_Text::Image_Text(), Image_Text::init()
394 function &construct ( $text, $options ) {
395 $itext = new Image_Text($text, $options);
396 $res = $itext->init();
397 if (PEAR::isError($res)) {
406 * Set a single or multiple options. It may happen that you have to reinitialize the Image_Text
407 * object after changing options. For possible options, please take a look at the class options
411 * @param mixed $option A single option name or the options array.
412 * @param mixed $value Option value if $option is string.
413 * @return bool True on success, otherwise PEAR::Error.
415 * @see Image_Text::Image_Text()
418 function set($option, $value=null)
420 $reInits = array_flip($this->_reInits);
421 if (!is_array($option)) {
422 if (!isset($value)) {
423 return PEAR::raiseError('No value given.');
425 $option = array($option => $value);
427 foreach ($option as $opt => $val) {
430 $this->setColors($val);
433 if (is_array($val)) {
434 $this->_text = implode('\n', $val);
440 $this->options[$opt] = $val;
443 if (isset($reInits[$opt])) {
444 $this->_init = false;
453 * Using this method you can set multiple colors for your text.
454 * Use a simple numeric array to determine their order and give
455 * it to this function. Multiple colors will be
456 * cycled by the options specified 'color_mode' option. The given array
457 * will overwrite the existing color settings!
459 * The following colors syntaxes are understood by this method:
460 * - "#ffff00" hexadecimal format (HTML style), with and without #.
461 * - "#08ffff00" hexadecimal format (HTML style) with alpha channel (08), with and without #.
462 * - array with 'r','g','b' and (optionally) 'a' keys, using int values.
463 * - a GD color special color (tiled,...).
465 * A single color or an array of colors are allowed here.
467 * @param mixed $colors Single color or array of colors.
468 * @return bool True on success, otherwise PEAR::Error.
470 * @see Image_Text::setColor(), Image_Text::set()
473 function setColors($colors)
476 if (is_array($colors) &&
477 (is_string($colors[0]) || is_array($colors[0]))
479 foreach ($colors as $color) {
480 $res = $this->setColor($color,$i++);
481 if (PEAR::isError($res)) {
486 return $this->setColor($colors, $i);
494 * This method is used to set a color at a specific color ID inside the
497 * The following colors syntaxes are understood by this method:
498 * - "#ffff00" hexadecimal format (HTML style), with and without #.
499 * - "#08ffff00" hexadecimal format (HTML style) with alpha channel (08), with and without #.
500 * - array with 'r','g','b' and (optionally) 'a' keys, using int values.
502 * @param mixed $color Color value.
503 * @param mixed $id ID (in the color array) to set color to.
504 * @return bool True on success, otherwise PEAR::Error.
506 * @see Image_Text::setColors(), Image_Text::set()
509 function setColor($color, $id=0)
511 if(is_array($color)) {
512 if (isset($color['r']) && isset($color['g']) && isset($color['b'])) {
513 $color['a'] = isset($color['a']) ? $color['a'] : 0;
514 $this->options['colors'][$id] = $color;
515 } else if (isset($color[0]) && isset($color[1]) && isset($color[2])) {
516 $color['r'] = $color[0];
517 $color['g'] = $color[1];
518 $color['b'] = $color[2];
519 $color['a'] = isset($color[3]) ? $color[3] : 0;
520 $this->options['colors'][$id] = $color;
522 return PEAR::raiseError('Use keys 1,2,3 (optionally) 4 or r,g,b and (optionally) a.');
524 } elseif (is_string($color)) {
525 $color = $this->_convertString2RGB($color);
527 $this->options['color'][$id] = $color;
529 return PEAR::raiseError('Invalid color.');
533 $aaFactor = ($this->options['antialias']) ? 1 : -1;
534 if (function_exists('imagecolorallocatealpha') && isset($color['a'])) {
535 $this->colors[$id] = $aaFactor * imagecolorallocatealpha($this->_img,
536 $color['r'],$color['g'],$color['b'],$color['a']);
538 $this->colors[$id] = $aaFactor * imagecolorallocate($this->_img,
539 $color['r'],$color['g'],$color['b']);
541 if ($this->colors[$id] == 0 && $aaFactor == -1) {
542 // correction for black with antialiasing OFF
543 // since color cannot be negative zero
544 $this->colors[$id] = -256;
551 * Initialiaze the Image_Text object.
553 * This method has to be called after setting the options for your Image_Text object.
554 * It initializes the canvas, normalizes some data and checks important options.
555 * Be shure to check the initialization after you switched some options. The
556 * set() method may force you to reinitialize the object.
559 * @return bool True on success, otherwise PEAR::Error.
560 * @see Image_Text::set()
565 // Does the fontfile exist and is readable?
566 // todo: with some versions of the GD-library it's also possible to leave font_path empty, add strip ".ttf" from
567 // the fontname; the fontfile will then be automatically searched for in library-defined directories
568 // however this does not yet work if at this point we check for the existance of the fontfile
570 $font_file = rtrim($this->options['font_path'], '/\\');
571 $font_file.= (OS_WINDOWS) ? '\\' : '/';
572 $font_file.= $this->options['font_file'];
573 $font_file = realpath($font_file);
574 if (empty($font_file) || !is_file($font_file) || !is_readable($font_file)) {
575 return PEAR::staticRaiseError('Fontfile not found or not readable: ' . $font_file);
577 $this->_font = $font_file;
580 // Is the font size to small?
581 if ($this->options['width'] < 1) {
582 return PEAR::raiseError('Width too small. Has to be > 1.');
585 // Check and create canvas
586 $image_canvas = false;
588 case (empty($this->options['canvas'])):
590 // Create new image from width && height of the clipping
591 $this->_img = imagecreatetruecolor(
592 $this->options['width'], $this->options['height']);
594 return PEAR::raiseError('Could not create image canvas.');
598 case (is_resource($this->options['canvas']) &&
599 get_resource_type($this->options['canvas'])=='gd'):
600 // The canvas is an image resource
601 $image_canvas = true;
602 $this->_img = $this->options['canvas'];
605 case (is_array($this->options['canvas']) &&
606 isset($this->options['canvas']['width']) &&
607 isset($this->options['canvas']['height'])):
609 // Canvas must be a width and height measure
610 $this->_img = imagecreatetruecolor(
611 $this->options['canvas']['width'],
612 $this->options['canvas']['height']
617 case (is_array($this->options['canvas']) &&
618 isset($this->options['canvas']['size']) &&
619 ($this->options['canvas']['size'] = 'auto')):
621 case (is_string($this->options['canvas']) &&
622 ($this->options['canvas'] = 'auto')):
623 $this->_mode = 'auto';
627 return PEAR::raiseError('Could not create image canvas.');
634 $this->options['canvas'] = array();
635 $this->options['canvas']['width'] = imagesx($this->_img);
636 $this->options['canvas']['height'] = imagesy($this->_img);
639 if ($this->options['enable_alpha']) {
640 imagesavealpha($this->_img, true);
641 imagealphablending($this->_img, false);
644 if ($this->options['background_color'] === null) {
645 $this->options['enable_alpha'] = true;
646 imagesavealpha($this->_img, true);
647 imagealphablending($this->_img, false);
648 $colBg = imagecolorallocatealpha($this->_img, 255, 255, 255, 127);
650 $arBg = $this->_convertString2RGB($this->options['background_color']);
651 if ($arBg === false) {
652 return PEAR::raiseError('Background color is invalid.');
654 $colBg = imagecolorallocatealpha($this->_img, $arBg['r'], $arBg['g'], $arBg['b'], $arBg['a']);
656 if ($image_canvas === false) {
657 imagefilledrectangle(
660 $this->options['canvas']['width'] - 1, $this->options['canvas']['height'] - 1,
666 // Save and repair angle
667 $angle = $this->options['angle'];
672 $angle = $angle % 360;
674 $this->options['angle'] = $angle;
676 // Set the color values
677 $res = $this->setColors($this->options['color']);
678 if (PEAR::isError($res)) {
682 $this->_lines = null;
684 // Initialization is complete
690 * Auto measurize text
692 * Automatically determines the greatest possible font size to
693 * fit the text into the text box. This method may be very resource
694 * intensive on your webserver. A good tweaking point are the $start
695 * and $end parameters, which specify the range of font sizes to search
696 * through. Anyway, the results should be cached if possible. You can
697 * optionally set $start and $end here as a parameter or the settings of
698 * the options array are used.
701 * @param int $start Fontsize to start testing with.
702 * @param int $end Fontsize to end testing with.
703 * @return int Fontsize measured or PEAR::Error.
704 * @see Image_Text::measurize()
707 function autoMeasurize($start=false, $end=false)
710 return PEAR::raiseError('Not initialized. Call ->init() first!');
713 $start = (empty($start)) ? $this->options['min_font_size'] : $start;
714 $end = (empty($end)) ? $this->options['max_font_size'] : $end;
717 // Run through all possible font sizes until a measurize fails
718 // Not the optimal way. This can be tweaked!
719 for ($i = $start; $i <= $end; $i++) {
720 $this->options['font_size'] = $i;
721 $res = $this->measurize();
723 if ($res === false) {
725 $this->options['font_size'] = -1;
726 return PEAR::raiseError("No possible font size found");
728 $this->options['font_size'] -= 1;
729 $this->_measurizedSize = $this->options['font_size'];
732 // Always the last couple of lines is stored here.
733 $this->_lines = $res;
735 return $this->options['font_size'];
739 * Measurize text into the text box
741 * This method makes your text fit into the defined textbox by measurizing the
742 * lines for your given font-size. You can do this manually before rendering (or use
743 * even Image_Text::autoMeasurize()) or the renderer will do measurizing
747 * @param bool $force Optionally, default is false, set true to force measurizing.
748 * @return array Array of measured lines or PEAR::Error.
749 * @see Image_Text::autoMeasurize()
752 function measurize($force=false)
755 return PEAR::raiseError('Not initialized. Call ->init() first!');
757 $this->_processText();
759 // Precaching options
760 $font = $this->_font;
761 $size = $this->options['font_size'];
763 $line_spacing = $this->options['line_spacing'];
764 $space = (1 + $this->options['line_spacing']) * $this->options['font_size'];
766 $max_lines = (int)$this->options['max_lines'];
768 if (($max_lines<1) && !$force) {
772 $block_width = $this->options['width'];
773 $block_height = $this->options['height'];
775 $colors_cnt = sizeof($this->colors);
776 $c = $this->colors[0];
781 $tokens_cnt = sizeof($this->_tokens);
791 $beginning_of_line = true;
793 // Run through tokens and order them in lines
794 foreach($this->_tokens as $token) {
795 // Handle new paragraphs
797 $bounds = imagettfbbox($size, 0, $font, $text_line);
798 if ((++$lines_cnt>=$max_lines) && !$force) {
801 if ($this->options['color_mode']=='paragraph') {
802 $c = $this->colors[$para_cnt%$colors_cnt];
805 $c = $this->colors[$i++%$colors_cnt];
808 'string' => $text_line,
809 'width' => $bounds[2]-$bounds[0],
810 'height' => $bounds[1]-$bounds[7],
811 'bottom_margin' => $bounds[1],
812 'left_margin' => $bounds[0],
815 $text_width = max($text_width, ($bounds[2]-$bounds[0]));
816 $text_height += (int)$space;
817 if (($text_height > $block_height) && !$force) {
822 $beginning_of_line = true;
828 if ($beginning_of_line) {
830 $text_line_next = $token;
831 $beginning_of_line = false;
833 $text_line_next = $text_line.' '.$token;
835 $bounds = imagettfbbox($size, 0, $font, $text_line_next);
836 $prev_width = isset($prev_width)?$width:0;
837 $width = $bounds[2]-$bounds[0];
839 // Handling of automatic new lines
840 if ($width>$block_width) {
841 if ((++$lines_cnt>=$max_lines) && !$force) {
844 if ($this->options['color_mode']=='line') {
845 $c = $this->colors[$i++%$colors_cnt];
847 $c = $this->colors[$para_cnt%$colors_cnt];
852 'string' => $text_line,
853 'width' => $prev_width,
854 'height' => $bounds[1]-$bounds[7],
855 'bottom_margin' => $bounds[1],
856 'left_margin' => $bounds[0],
859 $text_width = max($text_width, ($bounds[2]-$bounds[0]));
860 $text_height += (int)$space;
861 if (($text_height > $block_height) && !$force) {
866 $bounds = imagettfbbox($size, 0, $font, $text_line);
867 $width = $bounds[2]-$bounds[0];
868 $beginning_of_line = false;
870 $text_line = $text_line_next;
873 // Store remaining line
874 $bounds = imagettfbbox($size, 0, $font,$text_line);
875 if ($this->options['color_mode']=='line') {
876 $c = $this->colors[$i++%$colors_cnt];
878 $c = $this->colors[$para_cnt%$colors_cnt];
882 'string'=> $text_line,
883 'width' => $bounds[2]-$bounds[0],
884 'height'=> $bounds[1]-$bounds[7],
885 'bottom_margin' => $bounds[1],
886 'left_margin' => $bounds[0],
890 // add last line height, but without the line-spacing
891 $text_height += $this->options['font_size'];
893 $text_width = max($text_width, ($bounds[2]-$bounds[0]));
895 if (($text_height > $block_height) && !$force) {
899 $this->_realTextSize = array('width' => $text_width, 'height' => $text_height);
900 $this->_measurizedSize = $this->options['font_size'];
906 * Render the text in the canvas using the given options.
908 * This renders the measurized text or automatically measures it first. The $force parameter
909 * can be used to switch of measurizing problems (this may cause your text being rendered
910 * outside a given text box or destroy your image completely).
913 * @param bool $force Optional, initially false, set true to silence measurize errors.
914 * @return bool True on success, otherwise PEAR::Error.
917 function render($force=false)
920 return PEAR::raiseError('Not initialized. Call ->init() first!');
923 if (!$this->_tokens) {
924 $this->_processText();
927 if (empty($this->_lines) || ($this->_measurizedSize != $this->options['font_size'])) {
928 $this->_lines = $this->measurize( $force );
930 $lines = $this->_lines;
932 if (PEAR::isError($this->_lines)) {
933 return $this->_lines;
936 if ($this->_mode === 'auto') {
937 $this->_img = imagecreatetruecolor(
938 $this->_realTextSize['width'],
939 $this->_realTextSize['height']
942 return PEAR::raiseError('Could not create image cabvas.');
945 $this->setColors($this->_options['color']);
948 $block_width = $this->options['width'];
949 $block_height = $this->options['height'];
951 $max_lines = $this->options['max_lines'];
953 $angle = $this->options['angle'];
954 $radians = round(deg2rad($angle), 3);
956 $font = $this->_font;
957 $size = $this->options['font_size'];
959 $line_spacing = $this->options['line_spacing'];
961 $align = $this->options['halign'];
965 $offset = $this->_getOffset();
967 $start_x = $offset['x'];
968 $start_y = $offset['y'];
970 $end_x = $start_x + $block_width;
971 $end_y = $start_y + $block_height;
973 $sinR = sin($radians);
974 $cosR = cos($radians);
976 switch ($this->options['valign']) {
977 case IMAGE_TEXT_ALIGN_TOP:
980 case IMAGE_TEXT_ALIGN_MIDDLE:
981 $valign_space = ($this->options['height'] - $this->_realTextSize['height']) / 2;
983 case IMAGE_TEXT_ALIGN_BOTTOM:
984 $valign_space = $this->options['height'] - $this->_realTextSize['height'];
990 $space = (1 + $line_spacing) * $size;
992 // Adjustment of align + translation of top-left-corner to bottom-left-corner of first line
993 $new_posx = $start_x + ($sinR * ($valign_space + $size));
994 $new_posy = $start_y + ($cosR * ($valign_space + $size));
996 $lines_cnt = min($max_lines,sizeof($lines));
998 // Go thorugh lines for rendering
999 for($i=0; $i<$lines_cnt; $i++){
1001 // Calc the new start X and Y (only for line>0)
1002 // the distance between the line above is used
1004 $new_posx += $sinR * $space;
1005 $new_posy += $cosR * $space;
1008 // Calc the position of the 1st letter. We can then get the left and bottom margins
1009 // 'i' is really not the same than 'j' or 'g'.
1010 $bottom_margin = $lines[$i]['bottom_margin'];
1011 $left_margin = $lines[$i]['left_margin'];
1012 $line_width = $lines[$i]['width'];
1014 // Calc the position using the block width, the current line width and obviously
1015 // the angle. That gives us the offset to slide the line.
1017 case IMAGE_TEXT_ALIGN_LEFT:
1020 case IMAGE_TEXT_ALIGN_RIGHT:
1021 $hyp = $block_width - $line_width - $left_margin;
1023 case IMAGE_TEXT_ALIGN_CENTER:
1024 $hyp = ($block_width-$line_width)/2 - $left_margin;
1031 $posx = $new_posx + $cosR * $hyp;
1032 $posy = $new_posy - $sinR * $hyp;
1034 $c = $lines[$i]['color'];
1037 $bboxes[] = imagettftext ($im, $size, $angle, $posx, $posy, $c, $font, $lines[$i]['string']);
1039 $this->bbox = $bboxes;
1044 * Return the image ressource.
1046 * Get the image canvas.
1049 * @return resource Used image resource
1058 * Display the image (send it to the browser).
1060 * This will output the image to the users browser. You can use the standard IMAGETYPE_*
1061 * constants to determine which image type will be generated. Optionally you can save your
1062 * image to a destination you set in the options.
1064 * @param bool $save Save or not the image on printout.
1065 * @param bool $free Free the image on exit.
1066 * @return bool True on success, otherwise PEAR::Error.
1068 * @see Image_Text::save()
1071 function display($save=false, $free=false)
1073 if (!headers_sent()) {
1074 header("Content-type: " .image_type_to_mime_type($this->options['image_type']));
1076 PEAR::raiseError('Header already sent.');
1078 switch ($this->options['image_type']) {
1080 $imgout = 'imagepng';
1082 case IMAGETYPE_JPEG:
1083 $imgout = 'imagejpeg';
1086 $imgout = 'imagebmp';
1089 return PEAR::raiseError('Unsupported image type.');
1093 $imgout($this->_img);
1094 $res = $this->save();
1095 if (PEAR::isError($res)) {
1099 $imgout($this->_img);
1103 $res = imagedestroy($this->image);
1105 PEAR::raiseError('Destroying image failed.');
1112 * Save image canvas.
1114 * Saves the image to a given destination. You can leave out the destination file path,
1115 * if you have the option for that set correctly. Saving is possible with the display()
1118 * @param string $destFile The destination to save to (optional, uses options value else).
1119 * @return bool True on success, otherwise PEAR::Error.
1120 * @see Image_Text::display()
1123 function save($dest_file=false)
1126 $dest_file = $this->options['dest_file'];
1129 return PEAR::raiseError("Invalid desitination file.");
1132 switch ($this->options['image_type']) {
1134 $imgout = 'imagepng';
1136 case IMAGETYPE_JPEG:
1137 $imgout = 'imagejpeg';
1140 $imgout = 'imagebmp';
1143 return PEAR::raiseError('Unsupported image type.');
1147 $res = $imgout($this->_img, $dest_file);
1149 PEAR::raiseError('Saving file failed.');
1155 * Get completely translated offset for text rendering.
1157 * Get completely translated offset for text rendering. Important
1158 * for usage of center coords and angles
1161 * @return array Array of x/y coordinates.
1164 function _getOffset()
1167 $width = $this->options['width'];
1168 $height = $this->options['height'];
1169 $angle = $this->options['angle'];
1170 $x = $this->options['x'];
1171 $y = $this->options['y'];
1172 // Using center coordinates
1173 if (!empty($this->options['cx']) && !empty($this->options['cy'])) {
1174 $cx = $this->options['cx'];
1175 $cy = $this->options['cy'];
1176 // Calculation top left corner
1177 $x = $cx - ($width / 2);
1178 $y = $cy - ($height / 2);
1179 // Calculating movement to keep the center point on himslf after rotation
1181 $ang = deg2rad($angle);
1182 // Vector from the top left cornern ponting to the middle point
1183 $vA = array( ($cx - $x), ($cy - $y) );
1184 // Matrix to rotate vector
1185 // sinus and cosinus
1186 $sin = round(sin($ang), 14);
1187 $cos = round(cos($ang), 14);
1193 // Multiply vector with matrix to get the rotated vector
1194 // This results in the location of the center point after rotation
1196 ($mRot[0] * $vA[0] + $mRot[2] * $vA[0]),
1197 ($mRot[1] * $vA[1] + $mRot[3] * $vA[1])
1199 // To get the movement vector, we subtract the original middle
1204 // Finally we move the top left corner coords there
1209 return array ('x' => (int)round($x, 0), 'y' => (int)round($y, 0));
1213 * Convert a color to an array.
1215 * The following colors syntax must be used:
1216 * "#08ffff00" hexadecimal format with alpha channel (08)
1217 * array with 'r','g','b','a'(optionnal) keys
1218 * A GD color special color (tiled,...)
1219 * Only one color is allowed
1220 * If $id is given, the color index $id is used
1222 * @param mixed $colors Array of colors.
1223 * @param mixed $id Array of colors.
1226 function _convertString2RGB($scolor)
1228 if (preg_match(IMAGE_TEXT_REGEX_HTMLCOLOR, $scolor, $matches)) {
1230 'r' => hexdec($matches[2]),
1231 'g' => hexdec($matches[3]),
1232 'b' => hexdec($matches[4]),
1233 'a' => hexdec(!empty($matches[1])?$matches[1]:0),
1240 * Extract the tokens from the text.
1244 function _processText()
1246 if (!isset($this->_text)) {
1249 $this->_tokens = array();
1251 // Normalize linebreak to "\n"
1252 $this->_text = preg_replace("[\r\n]", "\n", $this->_text);
1254 // Get each paragraph
1255 $paras = explode("\n",$this->_text);
1257 // loop though the paragraphs
1258 // and get each word (token)
1259 foreach($paras as $para) {
1260 $words = explode(' ',$para);
1261 foreach($words as $word) {
1262 $this->_tokens[] = $word;
1264 // add a "\n" to mark the end of a paragraph
1265 $this->_tokens[] = "\n";
1267 // we do not need an end paragraph as the last token
1268 array_pop($this->_tokens);