[PHP] Text innerhalb & entlang eines Kreises schreiben

[PHP] Text innerhalb & entlang eines Kreises schreiben

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
php-text-kreis-1.png
php-text-kreis-2.png


Code

PHP:
<?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 <comfreek@outlook.com>
  * @copyright (C) Copyright 2012 ComFreek
  * @license MIT
  * @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;
}

Hier ein Beispiel:
PHP:
// 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($image, 255, 0, 0);

$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($image, NULL, 100);  // 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:
Anhang anzeigen 61283
Autor
ComFreek
Aufrufe
2.030
First release
Last update

Bewertungen

0,00 Stern(e) 0 Bewertungen

Share this resource

Zurück