PHP从数组创建正确的字符串

utdev

我正在尝试创建此字符串:

convert -size 720x480 xc:black -strokewidth 5 -stroke lime -draw "line 165,400 265,400" -draw "line 295,400 395,400" -draw "line 425,400 525,400" -stroke blue -draw "line 165,405 265,405" -draw "line 295,405 395,405" -draw "line 425,405 525,405"

从此数组数据(函数):

protected function getTextItems()
{
    if($this->hasCh())
    {
        return [[
            'title' => 'test1',
            'position0' => ['x0' => '165', 'x1' => '400', 'y0' => '265', 'y1' => '400'],
            'position1' => ['x0' => '165', 'x1' => '405', 'y0' => '265', 'y1' => '405'],
        ],
        [
            'title' => 'test2',
            'position0' => ['x0' => '295', 'x1' => '400', 'y0' => '395', 'y1' => '400'],
            'position1' => ['x0' => '295', 'x1' => '405', 'y0' => '395', 'y1' => '405'],
        ],
        [
            'title' => 'test3',
            'position0' => ['x0' => '425', 'x1' => '400', 'y0' => '525', 'y1' => '400'],
            'position1' => ['x0' => '425', 'x1' => '405', 'y0' => '525', 'y1' => '405'],
        ]];
    }
}

到目前为止,我做到了:

    $itemCounter = 0;
    $positionCounter = 0;
    $recItemData = $this->getTextItems();
    $rec = 'convert -size 720x480 xc:black -strokewidth 5 -stroke lime';
    foreach ($recItemData as $recItemDataKey => $recItemDataValue)
    {
        $rec .= ' -draw "line ' . $recItemData[$itemCounter]['position0']['x0'] . ',' . $recItemData[$itemCounter]['position0']['x1'];
        $rec .= ' ' . $recItemData[$itemCounter]['position0']['y0'] . ',' . $recItemData[$itemCounter]['position0']['y1'] . '"';
        if($itemCounter % count($recItemData)-1 == 0)
        {
            $rec .= ' -stroke blue ';
        }
        $rec .= ' -draw "line ' . $recItemData[$itemCounter]['position1']['x0'] . ',' . $recItemData[$itemCounter]['position1']['x1'];
        $rec .= ' ' . $recItemData[$itemCounter]['position1']['y0'] . ',' . $recItemData[$itemCounter]['position1']['y1'] . '"';
        if($itemCounter < count($recItemData))
        {
            $itemCounter++;
        }
    }

上面创建了这个字符串(命令),这是不正确的:

convert -size 720x480 xc:black -strokewidth 5 -stroke lime -draw "line 165,400 265,400" -draw "line 165,400 265,400" -draw "line 295,400 395,400" -stroke blue -draw "line 295,400 395,400" -draw "line 425,400 525,400" -draw "line 425,400 525,400"

我究竟做错了什么。

猴子宙斯

试试这个:

$command = 'convert -size 720x480 xc:black -strokewidth 5';

$position0 = '-stroke lime';
$position1 = '-stroke blue';

foreach($this->getTextItems() as $v)
{
    $position0.= ' -draw "line '.$v['position0']['x0'].','.$v['position0']['x1'].' '.$v['position0']['y0'].','.$v['position0']['y1'].'"';

    $position1.= ' -draw "line '.$v['position1']['x0'].','.$v['position1']['x1'].' '.$v['position1']['y0'].','.$v['position1']['y1'].'"';
}

$command.= ' '.$position0.' '.$position1;

echo $command;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章