NYCPHP Meetup

NYPHP.org

[nycphp-talk] php gd problem

Jason Sia jsia18 at yahoo.com
Fri Aug 24 10:31:32 EDT 2007


Here's the code the format of the line is:

line_startX_startY_endX,endY_thickness;

<?php
function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
    if ($thick == 1) {
        return imageline($image, $x1, $y1, $x2, $y2, $color);
    }
    $t = $thick / 2 - 0.5;
    if ($x1 == $x2 || $y1 == $y2) {
        return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
    }
    $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
    $a = $t / sqrt(1 + pow($k, 2));
    $points = array(
        round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
        round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
        round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
        round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
    );
    imagefilledpolygon($image, $points, 4, $color);
    return imagepolygon($image, $points, 4, $color);
}

// grab the drawing data
$data=$_POST["data"];
$imagetype='jpg';
$data= str_replace('undefined','',$data);
if($imagetype=="jpg" || $imagetype=="png"){
$background=imagecreatetruecolor(500,300);
$image=imagecreate(500,300);
imageantialias($image,true);
imageantialias($background,true);
$backgroundColor=imagecolorallocate($image,255,255,255);
$colors=Array();
}


$shapes=explode("-",$data);

// loop through the shapes
for($s=0;$s<count($shapes)-1;$s++){
  // create the new swfshape
  if($imagetype=="swf"){$l=new SWFShape();}

  // split up the shape data
  $shapeData=explode("_",$shapes[$s]);

  // check/set color allocations and line thickness
  if($imagetype=="jpg" || $imagetype=="png"){imagesetthickness($image,$shapeData[5]);}
  $shapeData[6]=substr($shapeData[6],2);
  $r=hexdec(substr($shapeData[6],0,2));
  $g=hexdec(substr($shapeData[6],2,2));
  $b=hexdec(substr($shapeData[6],4,2));

  // check if color is already allocated
  if($imagetype=="jpg" || $imagetype=="png"){$shapecolor=imagecolorexact($image,$r,$g,$b);
  if($shapecolor==(-1)){
    $shapecolor=imagecolorallocate($image,$r,$g,$b);
  }}


  // draw the line shapes

  if($shapeData[0]=="line"){
    if($imagetype=="jpg" || $imagetype=="png"){
        imagelinethick($image, $shapeData[1], $shapeData[2], $shapeData[3], $shapeData[4], $shapecolor, $thick = $shapeData[5]);
//            imageline($image,$shapeData[1],$shapeData[2],$shapeData[3],$shapeData[4],$shapecolor);            
    }
  }

  // draw the rectangle shapes
  if($shapeData[0]=="rectangle"){
    if($imagetype=="jpg" || $imagetype=="png"){if($shapeData[7]=="true"){
      imagefilledrectangle($image,$shapeData[1],$shapeData[2],$shapeData[3],$shapeData[4],$shapecolor);
    }else{
      imagerectangle($image,$shapeData[1],$shapeData[2],$shapeData[3],$shapeData[4],$shapecolor);
    }}
  }

  // draw the curve shapes
  if($shapeData[0]=="curve"){
  $difX=$shapeData[1]-$shapeData[3];
  $difY=$shapeData[2]-$shapeData[4];
  if($difX<0 && $difY<0){$startD=270;$endD=360;}
  if($difX>0 && $difY<0){$startD=180;$endD=270;}
  if($difX>0 && $difY>0){$startD=90;$endD=180;}
  if($difX<0 && $difY>0){$startD=0;$endD=90;}
  if($imagetype=="jpg" || $imagetype=="png"){imagesetthickness($image,$shapeData[5]);
  imagearc($image,$shapeData[1],$shapeData[4],abs($difX)*2,abs($difY)*2,$startD,$endD,$shapecolor);
  }
}

  // draw the circle shapes
  if($shapeData[0]=="circle"){
  $w=abs($shapeData[1]-$shapeData[3]);
  if($w==0){$w=0.1;}
  $h=abs($shapeData[2]-$shapeData[4]);
  if($h==0){$h=0.1;}
  if($imagetype=="jpg" || $imagetype=="png"){imagesetthickness($image,$shapeData[5]);
  if($shapeData[7]=="true"){
    imagefilledarc($image,$shapeData[1],$shapeData[2],$w*2,$h*2,0,359,$shapecolor,IMAGE_ARC_PIE);
    imagefilledarc($image,$shapeData[1],$shapeData[2],$w*2,$h*2,359,360,$shapecolor,IMAGE_ARC_PIE);
  }else{
    imagearc($image,$shapeData[1],$shapeData[2],$w*2,$h*2,0,359,$shapecolor);
    imagearc($image,$shapeData[1],$shapeData[2],$w*2,$h*2,359,360,$shapecolor);
  }}
  $j=$w*0.70711;
  $n=$h*0.70711;
  $i=$j-($h-$n)*$w/$h;
  $M=$n-($w-$j)*$h/$w;
  
  if($imagetype=="swf"){
  $l->movePenTo($shapeData[1]+$w,$shapeData[2]);
  $l->drawCurveTo($shapeData[1]+$w,$shapeData[2]-$M,$shapeData[1]+$j,$shapeData[2]-$n);
  $l->drawCurveTo($shapeData[1]+$i,$shapeData[2]-$h,$shapeData[1],$shapeData[2]-$h);
  $l->drawCurveTo($shapeData[1]-$i,$shapeData[2]-$h,$shapeData[1]-$j,$shapeData[2]-$n);
  $l->drawCurveTo($shapeData[1]-$w,$shapeData[2]-$M,$shapeData[1]-$w,$shapeData[2]);
  
  $l->drawCurveTo($shapeData[1]-$w,$shapeData[2]+$M,$shapeData[1]-$j,$shapeData[2]+$n);
  $l->drawCurveTo($shapeData[1]-$i,$shapeData[2]+$h,$shapeData[1],$shapeData[2]+$h);
  $l->drawCurveTo($shapeData[1]+$i,$shapeData[2]+$h,$shapeData[1]+$j,$shapeData[2]+$n);
  $l->drawCurveTo($shapeData[1]+$w,$shapeData[2]+$M,$shapeData[1]+$w,$shapeData[2]);
  }}

}  // end drawing data loop

// copy the pallette drawing image to the truecolor jpeg image
if($imagetype=="jpg" || $imagetype=="png"){
imageantialias($image,true);
imageantialias($background,true);
$background = imagecreatetruecolor(90, 55);
imagecopyresampled($background,$image,0,0,0,0,90,55,500,300);
}

// save the jpeg to file
$uniqueFile=rand();
if($imagetype=="jpg"){imagejpeg($background,"draw/$uniqueFile.jpg");}

// save the png to file
if($imagetype=="png"){imagepng($background,"draw/$uniqueFile.png");}

// free up the image allocations
if($imagetype=="jpg" || $imagetype=="png"){
imagedestroy($background);
imagedestroy($image);
}
?>

----- Original Message ----
From: Dan Cech <dcech at phpwerx.net>
To: NYPHP Talk <talk at lists.nyphp.org>
Sent: Friday, August 24, 2007 9:34:41 PM
Subject: Re: [nycphp-talk] php gd problem

Jason Sia wrote:
> Hi, I created a drawing application flash using actionscript then I wanted to save the drawing in a jpg file.  I succefulkly saved the file however upon looking at the output image, the output is not smooth,  I used imagefillpolygon for lines thicker than 1px coz according to php.net imagesetthickness is only applicable to orthogonal lines.  I also tried to use imgaesetthickness but the output was worse.  I got convincing output when I convert the drawing to swf using PHP ming.  Attached is the output image could someone help me with this?

Jason,

It looks like you are probably generating a huge number of points for
the line, so you've got a lot of polygons and possibly the jagged edges
you are seeing are from misplaced corners.  You will probably need to
post some code so that we can see exactly what is happening.

My guess would be that we don't need to see the flash stuff, but just
the gd script and the data you're feeding it.

Dan
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com

Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php






Send instant messages to your online friends http://uk.messenger.yahoo.com 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070824/e5ec1d4e/attachment.html>


More information about the talk mailing list