dongwoqin7034 2016-09-08 13:21
浏览 42
已采纳

Php从数组创建适当的String

I am trying to create this string:

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"

from this array data (function):

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'],
        ]];
    }
}

So far I did this:

    $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++;
        }
    }

The above created this string (command), which is not correct:

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"

What Am I doing wrong.

  • 写回答

2条回答 默认 最新

  • doubi8512 2016-09-08 13:40
    关注

    Try this:

    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大