tutorials.de Buch-Aktion 05/2012
RSS-Feed anzeigen

ComFreek

[PHP] Text innerhalb & entlang eines Kreises schreiben

Bewertung: 3 Stimmen mit einer durchschnittlichen Bewertung von 5,00.
von ComFreek am 01.02.12 um 15:02 (823 Hits)
Wegen diesem Thread habe ich eine kleine Funktion geschrieben, die mittels GD2 in PHP Text innerhalb und entlang eines Kreises ("draw circular text") ausgibt.

Beispiel-Ausgaben
blogs/comfreek/attachments/59470-php-text-innerhalb-entlang-eines-kreises-schreiben-example_image.jpgblogs/comfreek/attachments/59472-php-text-innerhalb-entlang-eines-kreises-schreiben-example_image2.jpg

Code

PHP-Code:
<?php

/**
  * Draws a circular text on a (virtual) circle in a GD2 image
  * @param resource $imgObj The GD2 object
  * @param array $textProps Associative array with four properties:
  * -text The text
  * -color An allocated color
  * -font The font filename
  * -fontSize The font size
  * @param integer $radius The radius of the (virtual) circle
  * @param array $positionProps Associative array with three entries:
  * -offsetX Offset from left
  * -offsetY Offset from top
  * -angleOffset The angle in degrees to rotate the circle (use this if the text isn't properly centered)
  * @param integer $charSpan=10 The amount of degrees between two characters
  * @return resource Returns the modified image as a GD2 resource
  *
  * @author ComFreek <programmer-comfreek@hotmail.com>
  * @copyright ComFreek, 2012
  * @license Creative Commons Attribution-ShareAlike 3.0 Unported <http://creativecommons.org/licenses/by-sa/3.0/>
  * @created 31.01.2012 (dd.mm.yyyy)
  *
  * Thanks to:
  * -PHP
  * -Radon: http://www.ithowto.ro/2009/03/howto-write-circular-text-with-php-and-gd/
  */
function &DrawCircularText(&$imgObj$textProps$radius$positionProps$charSpan=10)
{
  
$text strrev($textProps['text']);
  
$textLen strlen($text);

  
$start_angle $charSpan*($textLen/2);
  
  for ( 
$i=0$i<$textLen$i++ )
  {
    
$a = ($charSpan*$i)-$start_angle;
    
$a += $positionProps['angleOffset'];

    
$cos cos(deg2rad($a));
    
$sin sin(deg2rad($a));
    
$x 0;

    
$xt round($cos*($x) - $sin*($radius));
    
$yt round($sin*($x) + $cos*($radius));
    
imagettftext($imgObj$textProps['fontSize'], 360-$a$positionProps['offsetX']+$xt,
                 
$positionProps['offsetY']+$yt$textProps['color'], $textProps['font'], $text[$i]);
  }
  return 
$imgObj;
}


/** EXAMPLE **/ if ( false )  // This avoids including this file and executing this example code
{
  
// Load the image 'image.png'
  // (You can use all imagecreatefrom* (e.g. -jpeg, -gif) functions, see http://php.net/manual/ref.image.php)
  
$image imagecreatefrompng('image.png');
  
  
// Allocate a new color with the RGB values 255, 0 and 0 
  
$textColor imagecolorallocate($image25500);
  
  
$textProps = array('text' => 'Your text',      // The text
                     
'color' => $textColor,      // The color which we allocated
                     
'font' => 'yourFont.ttf',   // The font file
                     
'fontSize' => 20            // The font size
                    
);
                    
  
$radius =  50;  // Radius of the (virtual) circle
  
  
$positionProps = array('offsetX' => 50,    // Offset from left
                         
'offsteY' => 50,    // Offset from top
                         
'angleOffset' => 6  // If the text isn't properly centered, adjust it here in degrees
                        
);
                        
  
$charSpan 10;  // The amount of degrees between two characters
  
  // Draw the text!
  
$image DrawCircularText($image$textProps$radius$positionProps$charSpan);
  
  
// Send our image as a JPEG file to the browser...
  
header('Content-type: image/jpeg');
  
imagejpeg($imageNULL100);  // Use best quality (100)
  
  // ...or save it into a file
  
imagejpeg($image'image_2.jpg'100);
  
  
// Free the resources!
  
imagecolordeallocate($textColor);
  
imagedestroy($image);
}
Hier noch ein Beispiel zum Download:
DrawCircularText_Example.zip

"[PHP] Text innerhalb & entlang eines Kreises schreiben" bei Twitter speichern "[PHP] Text innerhalb & entlang eines Kreises schreiben" bei Facebook speichern

Stichworte: codeschnipsel, gd2, image-gd, Kreis, php, text
Kategorien
Webmaster , ‎ Internet , ‎ Programmierung , ‎ PHP , ‎ Codeschnipsel

Kommentare

  1. Avatar von Alice
    Vielen Dank @ComFreek

    Du weisst sicher wie lange ich schon versuche so eine Funktion zu erstellen!