如何动态创建形状和半径的图像?

时间:2016-04-21 16:36:21

标签: php gd

我正在使用php image gd库创建产品设计。用户可以选择形状并可以设置左上角,右上角,左下角和右下角的半径值,并根据用户选择图像创建该形状和半径。我可以设置图像左上角的半径和形状但是没有按照用户的选择设置右上角,左下角和右下角半径。

这是我的代码:

<form method="post" name="myform">

<input type="text" value="20" name="w" />

<input type="text" value="20" name="l" />

<div>
<label>Left Top </label>
<select name="shape1">
    <option value="round">round</option>
    <option value="square">square</option>

</select>
<label>Radius</label>
<select name="valbr1">
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="10">10</option>
</select>

</div>


<div>
<label>Right Top </label>
<select name="shape2">
    <option value="round">round</option>
    <option value="square">square</option>

</select>
<label>Radius</label>
<select name="valbr2">
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="10">10</option>
</select>

</div>


<div>
<label>Left Bottom </label>
<select name="shape3">
    <option value="round">round</option>
    <option value="square">square</option>

</select>
<label>Radius</label>
<select name="valbr3">
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="10">10</option>
</select>

</div>

<div>
<label>Right Bottom </label>
<select name="shape4">
    <option value="round">round</option>
    <option value="square">square</option>

</select>
<label>Radius</label>
<select name="valbr4">
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="10">10</option>
</select>

</div>
<input type="submit" name="submit">

</form>

if(isset($_POST['submit']))
{
$w = $_POST['w'];
$l = $_POST['l'];

$corner_shape[1] = $_POST['shape1'];
$corner_shape[2] = $_POST['shape2'];
$corner_shape[3] = $_POST['shape3'];
$corner_shape[4] = $_POST['shape4'];
$corner_val[1] = $_POST['valbr1'];
$corner_val[2] = $_POST['valbr2'];
$corner_val[3] = $_POST['valbr3'];
$corner_val[4] = $_POST['valbr4'];

$canvas_inner_width= $w*4;
$canvas_inner_height= $l*4;
$canvas_width=$canvas_inner_width+75;
$canvas_height=$canvas_inner_height+75;
$canvas = imagecreatetruecolor($canvas_width, $canvas_height);

function centertext($width ,$height,$w)
{
    $width = $width;
    $height = $height;

     $im = ImageCreate($width, $height);
     $bg = ImageColorAllocate($im, 0, 0, 0);
     $border = ImageColorAllocate($im, 0, 0, 0);
     ImageRectangle($im, 0, 0, $width - 1, $height - 1, $border);
     $text = $w;

        $textcolor = ImageColorAllocate($im, 255, 255, 255);
     // Font Size
        $font = 3;
         $font_width = ImageFontWidth($font);
        $font_height = ImageFontHeight($font);

        $text_width = $font_width * strlen($text);
        // Position to align in center
        $position_center = ceil(($width - $text_width) / 2);
         /*
        -----------
        Text Height
        -----------
        */
     $text_height = $font_height;
     // Position to align in abs middle
    $position_middle = ceil(($height - $text_height) / 2);
    ImageString($im, $font,  $position_center, $position_middle, $text, $textcolor);
    $white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
    // Draw a  line
    if($width < $height)
    {

    imageline($im, 0, 0, 0, $height, $white);   
    }
    else
    {
    imageline($im, 0, 0, $width, 0, $white);
    }
    //return array ($position_center , $position_middle); 
     return $im ;
}
$width = $canvas_inner_width;
$height = 15;
$im = centertext($width , $height, $w);
$sx = imagesx($im);
$sy = imagesy($im);
$marge_right = $canvas_width/2 - $sx/2;
$marge_bottom = 20;
//Merge the im onto our photo with an opacity of 50%
imagecopymerge($canvas, $im, imagesx($canvas) - $sx - $marge_right, imagesy($canvas) - $sy - $marge_bottom, 0, 0, imagesx($im), imagesy($im), 100);
$width = 15;
$height = $canvas_inner_height;
$im_vertical = centertext($width , $height ,$l);

$marge_right = 10;
$marge_bottom = $canvas_width/2 - $sx/2;

$sx = imagesx($im_vertical);
$sy = imagesy($im_vertical);

imagecopymerge($canvas, $im_vertical, imagesx($canvas) - $sx - $marge_right, imagesy($canvas) - $sy - $marge_bottom, 0, 0, imagesx($im_vertical), imagesy($im_vertical), 100);

/* create new image which show product design*/
$canvas_inner = imagecreatetruecolor($canvas_inner_width, $canvas_inner_height);
imagesetthickness($canvas_inner, 5);

$white = imagecolorallocate($canvas_inner,  0, 83, 150);
imagerectangle($canvas_inner, 0, 0, $canvas_inner_width, $canvas_inner_height, $white);
function draw_roundrectangle($img, $x1, $y1, $x2, $y2, $radius, $color,$corner_shape,$corner_val) {
//imagesetthickness($canvas_inner, 2);
if($corner_val[1] !='')
{
$radius=$corner_val[1];
$radius=$radius*4;
switch ($corner_shape[1]) 
{
case "round":

imagesetthickness($img, 3);

  $png_imagec1 = imagecreate($radius, $radius);
                 imagecolorallocate($png_imagec1, 0, 0, 0);
                 imagesetthickness($png_imagec1, 3);
                 $gray_notchc1 = imagecolorallocate($png_imagec1, 0, 83, 150);
                 $bluec1 = imagecolorallocate($png_imagec1, 102, 102, 102);
                 $xc = 0;
                 $yc = 0;
                 $wc = imagesx($png_imagec1) - 1;
                 $zc = imagesy($png_imagec1) - 1;
                 imageline($png_imagec1, $xc, $yc, $xc, $yc+$zc, $bluec1);
                 imageline($png_imagec1, $xc, $yc, $xc+$wc, $yc, $bluec1);
                 imagearc($png_imagec1,$x1+$radius, $y1+$radius, $radius*2, $radius*2, 180 , 270, $gray_notchc1);
                 imagecopymerge($img, $png_imagec1, 0, 0, 0, 0, imagesx($png_imagec1), imagesy($png_imagec1), 100);

break;

default:

}


}


}

$color=imagecolorallocate($canvas_inner, 0, 83, 150);
draw_roundrectangle($canvas_inner,0,0,$canvas_inner_width,$canvas_inner_height,4,$color ,$corner_shape,$corner_val);


$canvas_inner_sx = imagesx($canvas_inner);
$canvas_inner_sy = imagesy($canvas_inner); 
$canvas_sx = imagesx($canvas);
$canvas_sy = imagesy($canvas);
$x_co = ($canvas_sx/2)-($canvas_inner_sx/2);
$y_co = ($canvas_sy/2)-($canvas_inner_sy/2);

imagecopymerge($canvas, $canvas_inner, ceil($x_co), ceil($y_co), 0, 0, imagesx($canvas_inner), imagesy($canvas_inner), 100); 
header('Content-Type: image/jpeg');

imagejpeg($canvas);
imagedestroy($canvas);
}

0 个答案:

没有答案