Image

Imagejarodrussell wrote in Imagephp

GD2 artefact

This code is supposed to draw arcs, but I keep getting that line at the bottom. Can anyone tell me what that is and how to get rid of it?

Here's the buggy code:


<?php

// ----- Global Variables for Settings -----

// Globals get defined here for scope
$bwidth = 0;
$bheight = 0;
$corner = ''; // tl, tr, bl, br
$bcolor = array(); // Background color
$acolor = array(); // Arc color
$fcolor = array(); // Foreground color
$border = 1;

// This is only use internally to the program.
// This is set by the presence or lack of forecolor.
$filled = 1;

// Sets the type of image you display:
// 1 - JPEG
// 2 - PNG
// 3 - GIF
$imagetype = 3;


// --- START GET BLOCK ---
// The following gets and sets variables according to $_GET.

// Gets the image width
if (@$_GET['width'] != "")
{
    $bwidth = $_GET['width'];
}
else
{
    error_out("You need to provide a 'width'.");
}

// Gets the image height
if (@$_GET['height'] != "")
{
    $bheight = $_GET['height'];
}
else
{
    error_out("You need to provide a 'height'.");
}

if (@$_GET['corner'] != "")
{
    $corner = $_GET['corner'];
}
else
{
    error_out("You need to provide a 'corner', to determine the arc angle.");
}


// Gets the background color
if (@$_GET['backcolor'] != "")
{
    $bcolor = split(',', $_GET['backcolor']);
}
else
{
    error_out("You need a background color: 255,255,255");
}

// Gets the arc color
if (@$_GET['arccolor'] != "")
{
    $acolor = split(',', $_GET['arccolor']);
}
else
{
    error_out("You need an arc color: 255,255,255");
}

// Gets the foreground color if there is one.
// If a forecolor exists, then do not use a filled arc
if (@$_GET['forecolor'] != "")
{
    $fcolor = split(',', $_GET['forecolor']);
    
    // Do not use filled arc
    $filled = 0;
    
    // Gets the border width, if there is one.
    // border will only be used if a forecolor is given, hence it being here
    if (@$_GET['border'] != "")
    {
        $border = intval($_GET['border']);
    }
}

// --- END GET BLOCK ---


// ARC WIDTH, ARC HEIGHT - always twice $bwidth and $bheight
$arcwidth = $bwidth * 2;
$archeight = $bheight * 2;

// Creates the image
$crv = imagecreate($bwidth, $bheight) or error_out("Cannot create image");

// Sets the background color according to the $bcolor array
$background_color = imagecolorallocate($crv, $bcolor[0], $bcolor[1], $bcolor[2]);

// Sets the arc color based on the $acolor array
$arc_color = imagecolorallocate($crv, $tcolor[0], $tcolor[1], $tcolor[2]);

// If $filled == 1, create a two-color background and arc image
if ($filled == 1)
{
    // Gets the arc data
    $arcdata = create_arc($corner, $bwidth, $bheight);
    
    // The filled arc
    imagefilledarc($crv, $arcdata[0], $arcdata[1], $arcwidth, $archeight,
                   $arcdata[2], $arcdata[3], $arc_color, IMG_ARC_PIE);
}

// If $filled == 0, create a three-color background, arc, and forground image
else if ($filled == 0)
{
    // Sets the background color according to the $bcolor array
    $foreground_color = imagecolorallocate($crv, $fcolor[0], $fcolor[1], $fcolor[2]);
    
    // Gets the arc data
    $arcdata = create_arc($corner, $bwidth, $bheight);
    
    // Draws the "border" arc if has a width greater than 1
    if ($border > 1)
    {
        $iwborder = $arcwidth - $border;
        $ihborder = $archeight - $border;
        
        // The border
        imagefilledarc($crv, $arcdata[0], $arcdata[1], $arcwidth, $archeight,
                       $arcdata[2], $arcdata[3], $arc_color, IMG_ARC_PIE);
        
        // The filled arc
        imagefilledarc($crv, $arcdata[0], $arcdata[1], $iwborder, $ihborder,
                       $arcdata[2], $arcdata[3], $foreground_color, IMG_ARC_PIE);        
    }
    
    // The 'border' arc if the width is just 1
    else
    {
        // The filled arc
        imagefilledarc($crv, $arcdata[0], $arcdata[1], $arcwidth, $archeight,
                       $arcdata[2], $arcdata[3], $foreground_color, IMG_ARC_PIE);
        
        // The border    
        imagearc($crv, $arcdata[0], $arcdata[1], $arcwidth, $archeight,
                 $arcdata[2], $arcdata[3], $arc_color);
    }
}

// Displays the image based $imagetype
switch ($imagetype)
{
    // Displays the image as a JPEG
    case 1:
        header("Content-type: image/jpeg"); 
        imagejpeg($crv);
        break;
    
    // Displays the image as a PNG
    case 2:
        header("Content-type: image/png"); 
        imagepng($crv);
        break;
    
    // Displays the image as a GIF
    case 3:
        header("Content-type: image/gif"); 
        imagegif($crv);
        break;  
}

// Destroys the image
imagedestroy($crv);

// Ends the program
exit;


// ---------- Functions ----------

// Given $corner, $bwidth, and $bheight this returns an array with values
// to draw an arc
function create_arc($corner, $w, $h)
{
    $arc_ret = array();
    
    switch ($corner)
    {
        // top, left
        case "tl":
            $arc_ret = array($w, $h, 180, 270);
            break;
        
        // top, right
        case "tr":
            $arc_ret = array(0, $h, 270, 360);
            break;
        
        // bottom, left
        case "bl":
            $arc_ret = array($w, 0, 90, 180);
            break;
        
        // bottom, right
        case "br":
            $arc_ret = array(0, 0, 0, 90);
            break;
    }
    
    return($arc_ret);
}

// Error out
function error_out($message)
{
    if (@$message != "")
    {
        echo($message);
        exit;
    }
    else
    {
        error_out("Your error message needs a message.");
    }
}

?>