Calculation area whith Cartesian plane - php

PHP with GD I built a quadrangle on the Cartesian plane.
I measured the 4 sides, result = 100 each (the corners are at 90 degrees).
Area calculation formula of Gauss.
https://en.wikipedia.org/wiki/Shoelace_formula
Area doverebbe be 10,000, but my result is 15,000?
Where is the mistake?
Thanks for your help.
$Image = imagecreate(400,400);
imagecolorallocate($Image,0,0,0);
$White = imagecolorallocate($Image,255,255,255);
$p[0] = 100; $p[1] = 200;$p[2] = 200;
$p[3] = 200;$p[4] = 200; $p[5] = 300;$p[6] = 100; $p[7] = 300;
$XY = array(
$p[0], $p[1], // section of the line $p[2], $p[3]
$p[2], $p[3], // section of the line $p[4], $p[5]
$p[4], $p[5], // section of the line $p[6], $p[7]
$p[6], $p[7] // section of the line $p[0], $p[1]
);
imagepolygon($Image, $XY, 4, $White);
// segment
$SegmentLengthA = sqrt(bcpow($p[0]-$p[2],2) + bcpow($p[1]-$p[3],2));
imagestring ($Image , 5 , 10 , 5 , "LunghSegmA = $SegmentLengthA" , $White );
$SegmentLengthB = sqrt(bcpow($p[2]-$p[4],2) + bcpow($p[3]-$p[5],2));
imagestring ($Image , 5 , 10 , 20 , "LunghSegmB = $SegmentLengthB" , $White );
$SegmentLengthC = sqrt(bcpow($p[4]-$p[6],2) + bcpow($p[5]-$p[7],2));
imagestring ($Image , 5 , 10 , 35 , "LunghSegmC = $SegmentLengthC" , $White );
$SegmentLengthD = sqrt(bcpow($p[6]-$p[0],2) + bcpow($p[7]-$p[1],2));
imagestring ($Image , 5 , 10 , 50 , "LunghSegmC = $SegmentLengthC" , $White );
////// perimeter
$Perimeter = $SegmentLengthA + $SegmentLengthB + $SegmentLengthC + $SegmentLengthD;
imagestring ($Image , 5 , 10 , 65 , "Perimeter = $Perimeter" , $White );
// area
$L1 = ($p[0] * $p[3]) + ($p[2] * $p[5]) + ($p[4] * $p[7]);
$L2 = ($p[1] * $p[2]) + ($p[3] * $p[4]) + ($p[5] * $p[6]);
$Area = abs($L1 - $L2) / 2;
imagestring ($Image , 5 , 10 , 80 , "Area = $Area" , $White );
imagepng($Image);

You need to end shoelace formula with initial cordinates.
$L1 = ($p[0] * $p[3]) + ($p[2] * $p[5]) + ($p[4] * $p[7]) + ($p[6] * $p[1]);
$L2 = ($p[1] * $p[2]) + ($p[3] * $p[4]) + ($p[5] * $p[6]) + ($p[7] * $p[0]);

Related

How to generate fix size of barcode whether it's 3 char or 8 char?

I am using one of third party class file to generate barcode with php dynamically as per user inputs. I have problem with it's variable size of width.
When user enters 3 char then it creates small width of barcode and when user enters 10-14 chars, then it generates big barcode in width. I want barcode with same height - width in all cases.
See my app details:
PHP 5.6
CI3 framework
barcode type : code128
Please see my code below:
public function barcode( $filepath="", $text="USPC", $size="43", $orientation="horizontal", $code_type="code128", $print=false, $SizeFactor=1 ) {
$code_string = "";
$chksum = 104;
// Must not change order of array elements as the checksum depends on the array's key to validate final code
$code_array = array(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","#"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","\`"=>"111422","a"=>"121124","b"=>"121421","c"=>"141122","d"=>"141221","e"=>"112214","f"=>"112412","g"=>"122114","h"=>"122411","i"=>"142112","j"=>"142211","k"=>"241211","l"=>"221114","m"=>"413111","n"=>"241112","o"=>"134111","p"=>"111242","q"=>"121142","r"=>"121241","s"=>"114212","t"=>"124112","u"=>"124211","v"=>"411212","w"=>"421112","x"=>"421211","y"=>"212141","z"=>"214121","{"=>"412121","|"=>"111143","}"=>"111341","~"=>"131141","DEL"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","FNC 4"=>"114131","CODE A"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
$code_keys = array_keys($code_array);
$code_values = array_flip($code_keys);
for ( $X = 1; $X <= strlen($text); $X++ ) {
$activeKey = substr( $text, ($X-1), 1);
$code_string .= $code_array[$activeKey];
$chksum=($chksum + ($code_values[$activeKey] * $X));
}
$code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];
$code_string = "211214" . $code_string . "2331112";
$code_length = 20;
if ($print) {
$text_height = 30;
} else {
$text_height = 0;
}
for ( $i=1; $i <= strlen($code_string); $i++ ){
$code_length = $code_length + (integer)(substr($code_string,($i-1),1));
}
if ( strtolower($orientation) == "horizontal" ) {
$img_width = $code_length*$SizeFactor;
$img_height = 192;
$img_width=384;// set hardcoded height -width as client suggested
} else {
$img_width = 384;
$img_height = $code_length*$SizeFactor;
}
$image = imagecreate($img_width, $img_height + $text_height);
$black = imagecolorallocate ($image, 0, 0, 0);
$white = imagecolorallocate ($image, 255, 255, 255);
imagefill( $image, 0, 0, $white );
if ( $print ) {
imagestring($image, 5, 31, $img_height, $text, $black );
}
$location = 10;
for ( $position = 1 ; $position <= strlen($code_string); $position++ ) {
$cur_size = $location + ( substr($code_string, ($position-1), 1) );
if ( strtolower($orientation) == "horizontal" )
imagefilledrectangle( $image, $location*$SizeFactor, 0, $cur_size*$SizeFactor, $img_height, ($position % 2 == 0 ? $white : $black) );
else
imagefilledrectangle( $image, 0, $location*$SizeFactor, $img_width, $cur_size*$SizeFactor, ($position % 2 == 0 ? $white : $black) );
$location = $cur_size;
}
// Draw barcode to the screen or save in a file
if ( $filepath=="" ) {
header ('Content-type: image/png');
imagepng($image);
imagedestroy($image);
} else {
imagepng($image,$filepath);
imagedestroy($image);
}
}
Can this be achieved?
If Someone is still looking for this answer, this is how it can be done.
I am using the CodeItNow barcode generator available at Github. Details https://github.com/codeitnowin/barcode-generator here.
After Setup, use this code.
<?php
use CodeItNow\BarcodeBundle\Utils\QrCode;
use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;
$barcode = new BarcodeGenerator();
$barcode->setText("Any Data You Need To Be Here");
$barcode->setType(BarcodeGenerator::Code128);
$barcode->setScale(2);
$barcode->setThickness(25);
$barcode->setFontSize(10);
$barcode->setlabel("Any Label for Barcode");
$code = $barcode->generate();
echo '<img src="data:image/png;base64,'.$code.'" width="400" height="60"/>';
?>
The width and height of the barcode can be defined in the image tag.

PHP GD convert coordinates from Polar to Cartesian

I want to show percentages near or over each parts of the chart.
Do I have to convert coordinates from Polar to Cartesian?
To convert polar coordinates (r, θ) to rectangular coordinates (x, y) I used the following conversion:
x = r * cos( θ )
y = r * sin( θ )
in this graph
$x = $radius * cos( $Arc ); $y = $radius * sin( $Arc );
But results doesn't match (the percentages values aren't any close to each single parts of the chart), where I go wrong?
How can I fix it?
<?php
$W = 500; $H = 500;
$Image = imagecreate($W,$H);
$colors = array();
imagecolorallocate($Image,150,150,150); // grey background
$colors[0] = imagecolorallocate($Image,255,0,0); // red
$colors[1] = imagecolorallocate($Image,100,255,10); // green
$colors[2] = imagecolorallocate($Image,0,0,255); // bleu
$colors[3] = imagecolorallocate($Image,255,255,0); // yellow
$Country = array('England', 'Northern Ireland', 'Scotland', 'Wales');
$Population = array(53012456,1810863,5295403,3063456);
$TotPopulation = array_sum($Population);
$radius = 300; $StarAngle = 0;
foreach($Population as $index => $V )
{
$Arc = (360 * $V) / $TotPopulation;
$Percentage = (100 * $V) / $TotPopulation;
imagefilledarc(
$Image,
$W / 2, $H / 2, // center x,y
$radius,
$radius,
$StarAngle,
($StarAngle + $Arc), // end arc
$colors[$index],
IMG_ARC_PIE
);
$x = $radius * cos($Arc);
$y = $radius * sin($Arc);
imagestring($Image, 15, 5, 30 + $index*15 , 'x= '.number_format($x, 2, '.', '').' y='.number_format($y, 2, '.', '').' Country='.$Country[$index].' %='. number_format($Percentage, 2, '.', '').' ' , $colors[$index]);
$StarAngle += $Arc;
imagestring($Image, 15, 5, 430 + $index*15 , 'Country='.$Country[$index].' Population= $V %='.number_format($Percentage, 2, '.', '').' ' , $colors[$index]);
}
imagestring($Image, 5, 35, 10 , 'The population of the United Kingdom year 2011' , $colors[3]);
imagepng($Image);
You can try the library geo-math-php
composer require rkondratuk/geo-math-php:^1
Convert coordinates polar to cartesian:
<?php
use PhpGeoMath\Model\Polar3dPoint;
$polarPoint1 = new Polar3dPoint(
40.758742779050706, -73.97855507715238, Polar3dPoint::EARTH_RADIUS_IN_METERS
);
$cartesianpoint1 = $polarPoint1->buildCartesian3DPoint();
// Result:
$cartesianpoint1->x;
$cartesianpoint1->y;
$cartesianpoint1->z;
Convert coordinates cartesian to polar:
<?php
use PhpGeoMath\Model\Cartesian3dPoint;
$x = 1001;
$y = 205;
$z = 512;
$cartesianPoint2 = new Cartesian3dPoint($x, $y, $z);
$polarPoint2 = $cartesianPoint2->buildPolar3dPoint();
// Result:
$polarPoint2->lat;
$polarPoint2->lng;
$polarPoint2->radius;

How to draw flat topped hex (title) image in PHP?

I am trying to draw a hex using PHP. I have been trying to follow the manual from http://www.redblobgames.com/grids/hexagons/ supporting myself with leland hex generation class (https://github.com/pushcx/leland/blob/master/class_hex_image.php).
Unluckily my "hex" looks like this:
Can you please advice what am I doing wrong or tell me how can I create a proper hex?
It seems to me that function which grabs hex corners does not work correctly:
function hex_corners($center, $size)
{
$points = array();
for($i=0; $i <= 5; $i++)
{
$deg = 60 * $i; // Oblicz kąt, w którym znajduje sie róg hexu
$rad = pi() / 180 * $deg; // Przelicz kąt na radiany
$points[$i] = array_push($points, $center['x'] + $this->size / 2 * cos($rad), $center['y'] + $this->size / 2 * sin($rad));
}
return($points);
}
but I have tried to mimic the one described in http://www.redblobgames.com/grids/hexagons/ manual:
function hex_corner(center, size, i):
var angle_deg = 60 * i
var angle_rad = PI / 180 * angle_deg
return Point(center.x + size * cos(angle_rad),
center.y + size * sin(angle_rad))
I am using the following draw function:
public function hex_draw($x, $y)
{
$this->hex = imagecreatetruecolor ($this->size , $this->size);
$center['x'] = $this->size / 2;
$center['y'] = $this->size / 2;
$blue = imagecolorallocate($this->hex, 0, 0, 255);
// Get points
$points = $this->hex_corners($center, $this->size);
//die($print_r($points);
imagefilledpolygon($this->hex, $points, count($points)/2, $blue);
// flush image
header('Content-type: image/png');
imagepng($this->hex);
imagedestroy($this->hex);
}
My error was trivial.
In line
$points[$i] = array_push($points, $center['x'] + $this->size / 2 * cos($rad), $center['y'] + $this->size / 2 * sin($rad));
there was unneeded [$i] which caused problems.
Hex may be properly generated using the following code:
<?php
class hexmapimage
{
private $size = 100;
private $hex;
public function hex_draw($x, $y)
{
$this->hex = imagecreatetruecolor ($this->size , $this->size);
$center['x'] = $this->size / 2;
$center['y'] = $this->size / 2;
$black = imagecolorallocate($this->hex, 0, 0, 0);
$blue = imagecolorallocate($this->hex, 0, 0, 255);
$transparent = imagecolortransparent ($this->hex, $black);
// Get points
$points = $this->hex_corners($center, $this->size);
imagefilledrectangle($this->hex, 0, 0, $this->size, $this->size, $transparent);
imagefilledpolygon($this->hex, $points, count($points)/2, $blue);
// flush image
header('Content-type: image/png');
imagepng($this->hex);
imagedestroy($this->hex);
}
/*
* $center = array(x coordinate of center of hex, y coordinate of center of hex)
* $size = int (size of hex)
*/
function hex_corners($center, $size)
{
$points = array();
for($i=0; $i <= 5; $i++)
{
$deg = 60 * $i; // Oblicz kąt, w którym znajduje sie róg hexu
$rad = deg2rad($deg); // Przelicz kąt na radiany
array_push($points, $center['x'] + $this->size / 2 * cos($rad), $center['y'] + $this->size / 2 * sin($rad));
}
return($points);
}
}
$hex = new hexmapimage();
$hex->hex_draw(0, 0);

Applying FXAA in PHP?

When I'm using PHP's GD image library to draw shapes it always shows hard edges. I've tried to use GD's imageantialias() function but that is for straight lines only.
In order to solve the problem, I've searched some anti-aliasing algorithms and found FXAA works pretty well so I'm going to give it a try. I tried to port the FXAA anti-aliasing filter from the GLSL shader here.
Then, when I finished porting the FXAA shader to PHP, it doesn't give me the correct result. I test the FXAA filter using the imagecolorallocatealpha() example on PHP.net:
<?php
require('fxaa.php');
$size = 300;
$image=imagecreatetruecolor($size, $size);
// something to get a white background with black border
$back = imagecolorallocate($image, 255, 255, 255);
$border = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back);
imagerectangle($image, 0, 0, $size - 1, $size - 1, $border);
$yellow_x = 100;
$yellow_y = 75;
$red_x = 120;
$red_y = 165;
$blue_x = 187;
$blue_y = 125;
$radius = 150;
// allocate colors with alpha values
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75);
$red = imagecolorallocatealpha($image, 255, 0, 0, 75);
$blue = imagecolorallocatealpha($image, 0, 0, 255, 75);
// drawing 3 overlapped circle
imagefilledellipse($image, $yellow_x, $yellow_y, $radius, $radius, $yellow);
imagefilledellipse($image, $red_x, $red_y, $radius, $radius, $red);
imagefilledellipse($image, $blue_x, $blue_y, $radius, $radius, $blue);
FXAA::process($image);
// don't forget to output a correct header!
header('Content-Type: image/png');
// and finally, output the result
imagepng($image);
imagedestroy($image);
?>
Here's the original image:
And this is the processed image:
Here's another test image. (The left is FXAA-processed and the right is not.)
The example images' colour are messed up with the background and the edges is smoothened too much. This is not the expected result as I think. I don't understand what's wrong with my code so I seek for your help.
Also, here's the FXAA class I wrote & the original GLSL shader:
<?php
class FXAA {
const FXAA_REDUCE_MIN = 0.0078125;
const FXAA_REDUCE_MUL = 0.125;
const FXAA_SPAN_MAX = 8;
static $w = 0;
static $h = 0;
private static function add($a, $b){
return array($a[0] + $b[0], $a[1] + $b[1], $a[2] + $b[2]);
}
private static function dot($a, $b){
return $a[0] * $b[0] + $a[1] * $b[1] + $a[2] * $b[2];
}
private static function texture2D($img, $pos){
if(($pos[0] >= self::$w || $pos[0] < 0) || ($pos[1] >= self::$h || $pos[1] < 0)){
return array(0, 0, 0, 0);
}
$color = imagecolorat($img, $pos[0], $pos[1]);
$a = ($color >> 24) & 0xFF;
$r = ($color >> 16) & 0xFF;
$g = ($color >> 8) & 0xFF;
$b = $color & 0xFF;
return array($r, $g, $b, $a);
}
public static function process($img){
self::$w = imagesx($img);
self::$h = imagesy($img);
for($x = 0; $x < imagesx($img); $x++){
for($y = 0; $y < imagesy($img); $y++){
$rgbNW = self::texture2D($img,array($x - 1, $y - 1));
$rgbNE = self::texture2D($img,array($x + 1, $y - 1));
$rgbSW = self::texture2D($img,array($x - 1, $y + 1));
$rgbSE = self::texture2D($img,array($x + 1, $y + 1));
$rgbaM = $rgbM = self::texture2D($img,array($x, $y));
$opacity = array_pop($rgbM);
$luma = array(76, 149, 29);
$lumaNW = self::dot($rgbNW, $luma);
$lumaNE = self::dot($rgbNE, $luma);
$lumaSW = self::dot($rgbSW, $luma);
$lumaSE = self::dot($rgbSE, $luma);
$lumaM = self::dot($rgbM, $luma);
$lumaMin = min($lumaM, min(min($lumaNW, $lumaNE ), min($lumaSW, $lumaSE)));
$lumaMax = max($lumaM, max(max($lumaNW, $lumaNE), max($lumaSW, $lumaSE)));
$dir = array(
-(($lumaNW + $lumaNE) - ($lumaSW + $lumaSE)),
(($lumaNW + $lumaSW) - ($lumaNE + $lumaSE))
);
$dirReduce = max(($lumaNW + $lumaNE + $lumaSW + $lumaSE ) * ( 0.25 * self::FXAA_REDUCE_MUL ), self::FXAA_REDUCE_MIN);
$rcpDirMin = 1 / (min(abs($dir[0]), abs($dir[1])) + $dirReduce);
$dir[0] = min(self::FXAA_SPAN_MAX, max(-self::FXAA_SPAN_MAX, $dir[0] * $rcpDirMin));
$dir[1] = min(self::FXAA_SPAN_MAX, max(-self::FXAA_SPAN_MAX, $dir[1] * $rcpDirMin));
$rgbA = self::add(
self::texture2D($img, array($x + $dir[0] * (1 / 3 - 0.5), $y + $dir[1] * (1 / 3 - 0.5))),
self::texture2D($img, array($x + $dir[0] * (2 / 3 - 0.5), $y + $dir[1] * (1 / 3 - 0.5)))
);
$rgbA[0] *= 0.5;
$rgbA[1] *= 0.5;
$rgbA[2] *= 0.5;
$rgbB = self::add(
self::texture2D($img, array($x + $dir[0] * -0.5, $y + $dir[1] * -0.5)),
self::texture2D($img, array($x + $dir[0] * 0.5, $y + $dir[1] * 0.5))
);
$rgbB[0] = $rgbB[0] * 0.25 + $rgbA[0] * 0.5;
$rgbB[1] = $rgbB[1] * 0.25 + $rgbA[1] * 0.5;
$rgbB[2] = $rgbB[2] * 0.25 + $rgbA[2] * 0.5;
$lumaB = self::dot($rgbB, $luma);
if(($lumaB < $lumaMin) || ($lumaB > $lumaMax)){
imagesetpixel($img, $x, $y, imagecolorallocatealpha($img, $rgbA[0], $rgbA[1], $rgbA[2], $opacity));
}
else {
imagesetpixel($img, $x, $y, imagecolorallocatealpha($img, $rgbB[0], $rgbB[1], $rgbB[2], $opacity));
}
}
}
}
}
Original GLSL Shader:
uniform sampler2D tDiffuse;
uniform vec2 resolution;
varying vec2 vUv;
#define FXAA_REDUCE_MIN (1.0/128.0)
#define FXAA_REDUCE_MUL (1.0/8.0)
#define FXAA_SPAN_MAX 8.0
void main() {
vec3 rgbNW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, -1.0 ) ) * resolution ).xyz;
vec3 rgbNE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, -1.0 ) ) * resolution ).xyz;
vec3 rgbSW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, 1.0 ) ) * resolution ).xyz;
vec3 rgbSE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, 1.0 ) ) * resolution ).xyz;
vec4 rgbaM = texture2D( tDiffuse, gl_FragCoord.xy * resolution );
vec3 rgbM = rgbaM.xyz;
float opacity = rgbaM.w;
vec3 luma = vec3( 0.299, 0.587, 0.114 );
float lumaNW = dot( rgbNW, luma );
float lumaNE = dot( rgbNE, luma );
float lumaSW = dot( rgbSW, luma );
float lumaSE = dot( rgbSE, luma );
float lumaM = dot( rgbM, luma );
float lumaMin = min( lumaM, min( min( lumaNW, lumaNE ), min( lumaSW, lumaSE ) ) );
float lumaMax = max( lumaM, max( max( lumaNW, lumaNE) , max( lumaSW, lumaSE ) ) );
vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max( ( lumaNW + lumaNE + lumaSW + lumaSE ) * ( 0.25 * FXAA_REDUCE_MUL ), FXAA_REDUCE_MIN );
float rcpDirMin = 1.0 / ( min( abs( dir.x ), abs( dir.y ) ) + dirReduce );
dir = min( vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),max( vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),dir * rcpDirMin)) * resolution;
vec3 rgbA = 0.5 * (
texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 1.0 / 3.0 - 0.5 ) ).xyz +
texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 2.0 / 3.0 - 0.5 ) ).xyz );
vec3 rgbB = rgbA * 0.5 + 0.25 * (
texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * -0.5 ).xyz +
texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * 0.5 ).xyz );
float lumaB = dot( rgbB, luma );
if ( ( lumaB < lumaMin ) || ( lumaB > lumaMax ) ) {
gl_FragColor = vec4( rgbA, opacity );
} else {
gl_FragColor = vec4( rgbB, opacity );
}
}
The problem with your code is that it operates on a single image, and thus the anti-aliasing operation reads its own output.
Drawing the output of the anti-aliasing operation into a a new image will work.

How to generate adaptive text that curves inside a circle in php?

I am trying to create an image with a circle and some text on it where the text curves around the inside of the circle. The circle is adaptive and I want the text inside it to be adaptive.
Until now I have the following code that generates a text that curves:
$row1="line1";
$degrees = (130/strlen($row1));
imageellipse ( $im , 250 , 250 , 390 , 390 , $black );
imageellipse ( $im , 250 , 250 , 398 , 398 , $black );
for ($i=0;$i<strlen($row1);$i++)
{
$a = ($degrees*$i)+126-strlen($row1)/3;
$cos = cos(deg2rad($a));
$sin = sin(deg2rad($a));
$x = 0;
$y = 180;
$xt = round($cos*($x) - $sin*($y));
$yt = round($sin*($x) + $cos*($y));
imagettftext($im,14,180-($a),250+$xt,270+$yt,$red,'fonts\times.TTF',$row1[$i]);
}
Can you please tell me how can I make it adaptive, so it can adapt with the generated ellipse?
Hope this helps:
$image = imagecreatetruecolor(400,400);
$white = imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$white);
$red = imagecolorallocate($image,255,0,0);
$degrees = (360/strlen($text));
for($i=0;$i<strlen($text);$i++)
{
$a = ($degrees*$i)+180;
$cos = cos(deg2rad($a));
$sin = sin(deg2rad($a));
$x = 0;
$y = 180;
$xt = round($cos*($x) - $sin*($y));
$yt = round($sin*($x) + $cos*($y));
imagettftext($image,20,180-($a),200+$xt,200+$yt,$red,"C:/WINNT/Fonts
arial.ttf",$text[$i]);
}

Categories