Advertisement
Demonslay335

DImage::write()

Nov 6th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.37 KB | None | 0 0
  1. /**
  2.     *   Output string to image
  3.     *   @param  integer $x          - X coordinant to begin string output
  4.     *   @param  integer $y          - Y coordinant to begin string output
  5.     *   @param  string  $string     - String to be written to image
  6.     *   @param  integer $color      - Name pre-allocated color constant to be used
  7.     *   @param  integer $font       - Size of font to be used
  8.     *   @param  integer $flag       - Flag altering writing mode
  9.     *   @param  integer $angle      - Angle of text (used only with self::WRITE_TTF flag)
  10.     *   @param  string  $font_file  - File to use for TTF font (used only with self::WRITE_TTF flag)
  11.     *   @return boolean $success    - Success on writing the string
  12.     */
  13.     public function write($x, $y, $string, $color = 'black', $flag = self::WRITE_REGULAR, $angle = 0, $font_file = 'arial.ttf'){
  14.  
  15.         // Return flag
  16.         $success = true;
  17.  
  18.         // Check for bold flag
  19.         if($flag & self::WRITE_BOLD){
  20.  
  21.             // Array of coordinate adjustments to write to
  22.             $_x = array(1, 0, 1);
  23.             $_y = array(0, -1, -1);
  24.  
  25.             // Flag for offset fix
  26.             $this->write_fix_offset = 1;
  27.  
  28.             // Iterate to draw the text 3 times to produce bold text effect
  29.             for($n = 0; $n < 3; $n++){
  30.  
  31.                 // Write the text with coordinate adjustments
  32.                 $success = $this->write($x + $_x[$n], $y + $_y[$n], $string, $color, $flag & ~self::WRITE_BOLD, $angle, $font_file);
  33.  
  34.             }
  35.  
  36.             // Disable the offset fix
  37.             $this->write_fix_offset = 0;
  38.  
  39.             return $success;
  40.  
  41.         }
  42.  
  43.         // Cast the coordinates and cache
  44.         $x = $original_x = (int)$x;
  45.         $y = $original_y = (int)$y;
  46.  
  47.         // Gather the color
  48.         $color = $this->get_color($color);
  49.  
  50.         // Check for word wrapping
  51.         if($flag & self::WRITE_WRAP){
  52.  
  53.             // Check for writing vertically
  54.             if($flag & self::WRITE_UP){
  55.  
  56.                 // Word wrap the text
  57.                 $strings = explode("\n",
  58.                     wordwrap($string,
  59.                         ($y / ($this->font_width + $this->settings['kerning'])) - $this->settings['kerning'] + $this->write_fix_offset,
  60.                         "\n",
  61.                         $flag & self::WRITE_WRAP_CUT
  62.                     )
  63.                 );
  64.  
  65.             }
  66.  
  67.             else{
  68.  
  69.                 // Word wrap the text
  70.                 $strings = explode("\n",
  71.                     wordwrap($string,
  72.                         round((($this->width - $x) / ($this->font_width + $this->settings['kerning'])) - $this->settings['kerning'] + $this->write_fix_offset, 1),
  73.                         "\n",
  74.                         $flag & self::WRITE_WRAP_CUT
  75.                     )
  76.                 );
  77.  
  78.             }
  79.  
  80.         }
  81.  
  82.         else{
  83.  
  84.             // Cast the string to a singular array for iteration conformance
  85.             $strings = array($string);
  86.  
  87.         }
  88.  
  89.         // Check for center alignment
  90.         if($flag & self::WRITE_ALIGN_CENTER){
  91.  
  92.             // Check for vertical writing
  93.             if($flag & self::WRITE_UP){
  94.  
  95.                 // Calculate the new starting x-coordinate
  96.                 $x = round(
  97.                     // Split the image width in half to get the center focal point
  98.                     ($this->width / 2)
  99.                     // Subtract half of the font width
  100.                     - (
  101.                         // The amount of strings
  102.                         (count($strings) - 1)
  103.                         // Times the font height and adding line spacing, divided in half
  104.                         * ($this->font_height + $this->settings['line_spacing']) / 2
  105.                     )
  106.                     // Adjust by half of the font height
  107.                     - ($this->font_height / 2),
  108.                 1);
  109.  
  110.             }
  111.  
  112.         }
  113.  
  114.         // Check for vertical center alignment
  115.         if($flag & self::WRITE_CENTER_Y){
  116.  
  117.             // Check for vertical writing
  118.             if(!($flag & self::WRITE_UP)){
  119.  
  120.                 // Calculate the new starting y-coordinate
  121.                 $y = round(
  122.                     // Split the image height in half to get the center focal point
  123.                     ($this->height / 2)
  124.                     // Subtract half of the image height
  125.                     - (
  126.                         // The amount of strings
  127.                         (count($strings) - 1)
  128.                         // Times the font height and adding line spacing, divided by half
  129.                         * ($this->font_height + $this->settings['line_spacing']) / 2)
  130.                     // Adjust by half of the font height
  131.                     - ($this->font_height / 2),
  132.                 1);
  133.  
  134.             }
  135.  
  136.         }
  137.  
  138.         // Iterate the strings to write
  139.         foreach($strings as $s){
  140.  
  141.             // Check for writing with TTF font
  142.             if($flag & self::WRITE_TTF){
  143.  
  144.                 // Calculate the TTF bounding box
  145.                 $box = imagettfbbox($this->settings['font'], (int)$angle, $font_file, $s);
  146.  
  147.                 // Calculate Deviation
  148.                 $dx = ($box[2] - $box[0]) / 2 - ($box[2] - $box[4]) / 2; // Left-Right
  149.                 $dy = ($box[3] - $box[1]) / 2 + ($box[7] - $box[1]) / 2; // Top-Bottom
  150.                
  151.                 // Pivot Point
  152. //              $x -= $dx;
  153. //              $y -= $dy;
  154.  
  155.                 // Check for center aligning
  156.                 if($flag & self::WRITE_ALIGN_CENTER){
  157.  
  158.                     // Calculate the new x-coordinate
  159.                     $x = round(
  160.                         // Split the image width in half to get the center focal point
  161.                         ($this->width / 2)
  162.                         // Subtract half of the bounding box width
  163.                         - (($box[2] - $box[0]) / 2),
  164.                     1);
  165.  
  166.                 }
  167.  
  168.                 // Check for right alignment
  169.                 elseif($flag & self::WRITE_ALIGN_RIGHT){
  170.  
  171.                     // Calculate the new x-coordinate, width minus half of the bounding box width
  172.                     $x = $this->width - ($box[4] - $box[0]) - 2;
  173.  
  174.                 }
  175.  
  176.                 // Add the font height and line spacing
  177.                 $y += $this->font_height + $this->settings['line_spacing'];
  178.  
  179.                 // Draw the text
  180.                 $success = imagettftext($this->image, $this->settings['font'], (int)$angle, $x, $y, $color, $font_file, $s);
  181.  
  182.             }
  183.  
  184.             // Check for vertical writing
  185.             elseif($flag & self::WRITE_UP){
  186.  
  187.                 // Check for vertical centering
  188.                 if($flag & self::WRITE_CENTER_Y){
  189.  
  190.                     // Calculate the new y-coordinate
  191.                     $y = round(
  192.                         // Split the image width in half to get the center focal point
  193.                         ($this->height / 2)
  194.                         // Add the string length times the font width and kerning, divided in half
  195.                         + (strlen($s) * ($this->font_width + $this->settings['kerning']) / 2),
  196.                     1);
  197.  
  198.                 }
  199.  
  200.                 else{
  201.  
  202.                     // Set to the original y value
  203.                     $y = $original_y;
  204.  
  205.                 }
  206.  
  207.                 // Iterate each character for more consistancy and control
  208.                 foreach(str_split($s) as $c){
  209.  
  210.                     // Write the character to the image
  211.                     $success = imagecharup($this->image, $this->settings['font'], $x, $y, $c, $color);
  212.  
  213.                     // Subtract the y-coordinate for the next character
  214.                     $y -= $this->font_width + $this->settings['kerning'];
  215.  
  216.                 }
  217.  
  218.                 // Advance the x-coordinate by the font height and line spacing for the next string
  219.                 $x += $this->font_height + $this->settings['line_spacing'];
  220.  
  221.             }
  222.  
  223.             // Regular horizontal writing
  224.             else{
  225.  
  226.                 // Check for center alignment
  227.                 if($flag & self::WRITE_ALIGN_CENTER){
  228.  
  229.                     // Calculate the new x-coordinate
  230.                     $x = round(
  231.                         // Split the image width in half to get the center focal point
  232.                         ($this->width / 2)
  233.                         // Subtract the string length times the font width and kerning, divided in half
  234.                         - (strlen($s) * ($this->font_width + $this->settings['kerning']) / 2),
  235.                     1);
  236.  
  237.                 }
  238.  
  239.                 // Check for right-alignment
  240.                 elseif($flag & self::WRITE_ALIGN_RIGHT){
  241.  
  242.                     // Calculate the new x-coordinate
  243.                     $x = $this->width - (strlen($s) * ($this->font_width + $this->settings['kerning']) - $this->settings['kerning']);
  244.  
  245.                 }
  246.  
  247.                 else{
  248.  
  249.                     // Set to the original x value
  250.                     $x = $original_x;
  251.                 }
  252.  
  253.                 // Iterate each character for more consistancy and control
  254.                 foreach(str_split($s) as $c){
  255.  
  256.                     // Write the character to the image
  257.                     $success = imagechar($this->image, $this->settings['font'], $x, $y, $c, $color);
  258.  
  259.                     // Add the x-coordinate for the next character
  260.                     $x += $this->font_width + $this->settings['kerning'];
  261.                 }
  262.  
  263.                 // Advance the y-coordinate by the font height and line spacing for the next string
  264.                 $y += $this->font_height + $this->settings['line_spacing'];
  265.  
  266.             }
  267.  
  268.         }
  269.  
  270.         return $success;
  271.  
  272.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement