2

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
  • Is getTextItems() always going to return a static list of value like that? Will there ever be a position2, position3, etc...? Commented Sep 8, 2016 at 13:24
  • @MonkeyZeus there will not be a position2 nor position3 .... it always return values it has some elseif branches which I did not show Commented Sep 8, 2016 at 13:28

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

I think this worked but you have to add "line after the -draw
@JohnDoe2 I see, I fixed it up
1

learn how foreach works $recItemDataValue equals $recItemDataValue $itemCounter equals $recItemDataKey, rewrite it will result a more readable code

<?php

error_reporting(E_ALL);
ini_set('display_errors',1);
/**

*/
function getTextItems()
{

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

}

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

$desired = '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"';
echo $desired,"\n";
echo $rec,"\n";

same 6 lines but order changed

4 Comments

thanks, but I do not get the desired string do you get the correct one?
it creates me this : convert -size 720x480 xc:black -strokewidth 5 -stroke lime -draw "line 165,400 265,400" -stroke blue -draw "line 165,405 265,405" -stroke lime -draw "line 295,400 395,400" -stroke blue -draw "line 295,405 395,405" -stroke lime -draw "line 425,400 525,400" -stroke blue -draw "line 425,405 525,405"
I picked MonkeyZeus's answer because I think the code is a little better nevertheless I gave you a "thumbs up", thanks for your help
@JohnDoe2 De gustibus non est disputandum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.