Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Output string to image
- * @param integer $x - X coordinant to begin string output
- * @param integer $y - Y coordinant to begin string output
- * @param string $string - String to be written to image
- * @param integer $color - Name pre-allocated color constant to be used
- * @param integer $font - Size of font to be used
- * @param integer $flag - Flag altering writing mode
- * @param integer $angle - Angle of text (used only with self::WRITE_TTF flag)
- * @param string $font_file - File to use for TTF font (used only with self::WRITE_TTF flag)
- * @return boolean $success - Success on writing the string
- */
- public function write($x, $y, $string, $color = 'black', $flag = self::WRITE_REGULAR, $angle = 0, $font_file = 'arial.ttf'){
- // Return flag
- $success = true;
- // Check for bold flag
- if($flag & self::WRITE_BOLD){
- // Array of coordinate adjustments to write to
- $_x = array(1, 0, 1);
- $_y = array(0, -1, -1);
- // Flag for offset fix
- $this->write_fix_offset = 1;
- // Iterate to draw the text 3 times to produce bold text effect
- for($n = 0; $n < 3; $n++){
- // Write the text with coordinate adjustments
- $success = $this->write($x + $_x[$n], $y + $_y[$n], $string, $color, $flag & ~self::WRITE_BOLD, $angle, $font_file);
- }
- // Disable the offset fix
- $this->write_fix_offset = 0;
- return $success;
- }
- // Cast the coordinates and cache
- $x = $original_x = (int)$x;
- $y = $original_y = (int)$y;
- // Gather the color
- $color = $this->get_color($color);
- // Check for word wrapping
- if($flag & self::WRITE_WRAP){
- // Check for writing vertically
- if($flag & self::WRITE_UP){
- // Word wrap the text
- $strings = explode("\n",
- wordwrap($string,
- ($y / ($this->font_width + $this->settings['kerning'])) - $this->settings['kerning'] + $this->write_fix_offset,
- "\n",
- $flag & self::WRITE_WRAP_CUT
- )
- );
- }
- else{
- // Word wrap the text
- $strings = explode("\n",
- wordwrap($string,
- round((($this->width - $x) / ($this->font_width + $this->settings['kerning'])) - $this->settings['kerning'] + $this->write_fix_offset, 1),
- "\n",
- $flag & self::WRITE_WRAP_CUT
- )
- );
- }
- }
- else{
- // Cast the string to a singular array for iteration conformance
- $strings = array($string);
- }
- // Check for center alignment
- if($flag & self::WRITE_ALIGN_CENTER){
- // Check for vertical writing
- if($flag & self::WRITE_UP){
- // Calculate the new starting x-coordinate
- $x = round(
- // Split the image width in half to get the center focal point
- ($this->width / 2)
- // Subtract half of the font width
- - (
- // The amount of strings
- (count($strings) - 1)
- // Times the font height and adding line spacing, divided in half
- * ($this->font_height + $this->settings['line_spacing']) / 2
- )
- // Adjust by half of the font height
- - ($this->font_height / 2),
- 1);
- }
- }
- // Check for vertical center alignment
- if($flag & self::WRITE_CENTER_Y){
- // Check for vertical writing
- if(!($flag & self::WRITE_UP)){
- // Calculate the new starting y-coordinate
- $y = round(
- // Split the image height in half to get the center focal point
- ($this->height / 2)
- // Subtract half of the image height
- - (
- // The amount of strings
- (count($strings) - 1)
- // Times the font height and adding line spacing, divided by half
- * ($this->font_height + $this->settings['line_spacing']) / 2)
- // Adjust by half of the font height
- - ($this->font_height / 2),
- 1);
- }
- }
- // Iterate the strings to write
- foreach($strings as $s){
- // Check for writing with TTF font
- if($flag & self::WRITE_TTF){
- // Calculate the TTF bounding box
- $box = imagettfbbox($this->settings['font'], (int)$angle, $font_file, $s);
- // Calculate Deviation
- $dx = ($box[2] - $box[0]) / 2 - ($box[2] - $box[4]) / 2; // Left-Right
- $dy = ($box[3] - $box[1]) / 2 + ($box[7] - $box[1]) / 2; // Top-Bottom
- // Pivot Point
- // $x -= $dx;
- // $y -= $dy;
- // Check for center aligning
- if($flag & self::WRITE_ALIGN_CENTER){
- // Calculate the new x-coordinate
- $x = round(
- // Split the image width in half to get the center focal point
- ($this->width / 2)
- // Subtract half of the bounding box width
- - (($box[2] - $box[0]) / 2),
- 1);
- }
- // Check for right alignment
- elseif($flag & self::WRITE_ALIGN_RIGHT){
- // Calculate the new x-coordinate, width minus half of the bounding box width
- $x = $this->width - ($box[4] - $box[0]) - 2;
- }
- // Add the font height and line spacing
- $y += $this->font_height + $this->settings['line_spacing'];
- // Draw the text
- $success = imagettftext($this->image, $this->settings['font'], (int)$angle, $x, $y, $color, $font_file, $s);
- }
- // Check for vertical writing
- elseif($flag & self::WRITE_UP){
- // Check for vertical centering
- if($flag & self::WRITE_CENTER_Y){
- // Calculate the new y-coordinate
- $y = round(
- // Split the image width in half to get the center focal point
- ($this->height / 2)
- // Add the string length times the font width and kerning, divided in half
- + (strlen($s) * ($this->font_width + $this->settings['kerning']) / 2),
- 1);
- }
- else{
- // Set to the original y value
- $y = $original_y;
- }
- // Iterate each character for more consistancy and control
- foreach(str_split($s) as $c){
- // Write the character to the image
- $success = imagecharup($this->image, $this->settings['font'], $x, $y, $c, $color);
- // Subtract the y-coordinate for the next character
- $y -= $this->font_width + $this->settings['kerning'];
- }
- // Advance the x-coordinate by the font height and line spacing for the next string
- $x += $this->font_height + $this->settings['line_spacing'];
- }
- // Regular horizontal writing
- else{
- // Check for center alignment
- if($flag & self::WRITE_ALIGN_CENTER){
- // Calculate the new x-coordinate
- $x = round(
- // Split the image width in half to get the center focal point
- ($this->width / 2)
- // Subtract the string length times the font width and kerning, divided in half
- - (strlen($s) * ($this->font_width + $this->settings['kerning']) / 2),
- 1);
- }
- // Check for right-alignment
- elseif($flag & self::WRITE_ALIGN_RIGHT){
- // Calculate the new x-coordinate
- $x = $this->width - (strlen($s) * ($this->font_width + $this->settings['kerning']) - $this->settings['kerning']);
- }
- else{
- // Set to the original x value
- $x = $original_x;
- }
- // Iterate each character for more consistancy and control
- foreach(str_split($s) as $c){
- // Write the character to the image
- $success = imagechar($this->image, $this->settings['font'], $x, $y, $c, $color);
- // Add the x-coordinate for the next character
- $x += $this->font_width + $this->settings['kerning'];
- }
- // Advance the y-coordinate by the font height and line spacing for the next string
- $y += $this->font_height + $this->settings['line_spacing'];
- }
- }
- return $success;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement