Page 1 of 1

how php loop in exec ?

Posted: 2014-12-03T13:40:12-07:00
by mostafanastary
Hi,

i have this code for example:

exec("for (($i=1; i<=$5; $i++)); do convert -size 1024x -font tahoma.ttf -pointsize 42 -channel RGBA -background transparent -fill dodgerblue1 -stroke slategray4 caption:'Hi, this is a text' intro$i.png; done");

but didn't work,

is it possible do while or for loop in exec?
how can ++ the counter?

thank you

Re: how php loop in exec ?

Posted: 2014-12-03T13:51:02-07:00
by fmw42
I suspect you need to write the loop part in PHP and then call just the convert part in exec(). see http://php.net/manual/en/control-structures.for.php

Re: how php loop in exec ?

Posted: 2014-12-03T13:56:48-07:00
by mostafanastary
dear fred, i need to create some png file such as : 1.png, 2.png,3.png and etc. for example or draw some text or ...
i think i need create loop inside exec() function, like:

cat text_data.txt |
while read width gravity color pointsize x y text
do
convert -size ${width}x -gravity $gravity -fill $color -background wheat \
-pointsize $pointsize -page +${x}+${y} label:"${text}" miff:-
done |
convert -size 200x100 xc: - -flatten text_layered.jpg


but, with this syntax, php do not anything but create 200*100 text_layered.jpg,
what is the true coding with php and exec?

Re: how php loop in exec ?

Posted: 2014-12-03T14:24:36-07:00
by fmw42
I am not a PHP expert, but I think you need to use PHP commands to do the looping with a for loop or other PHP loop methods. Then inside the loop, you can use exec() to process the image. I do not know if PHP exec() even accepts pipes.

Alternately, create a unix bash shell script to do all your commands above and just use PHP exec() to call the shell script.

Do your commands above work properly in a terminal without PHP? I would make sure they work before porting to PHP.

Re: how php loop in exec ?

Posted: 2014-12-03T14:28:47-07:00
by mostafanastary
i never use terminal and shell script, i need use that with php, in imagemagick command, pipes dosen't work.

Re: how php loop in exec ?

Posted: 2014-12-03T14:45:55-07:00
by Bonzo
exec () is calling an external program and will accept variables but I do not think you can have the loop inside the exec(). You would need to go to a forum which deals with php like the sitepoint or stackoverflow forums to find out more.

You would need to do something like:

Code: Select all

for ($i=1; $i<=5; $i++){ 
exec("convert -size 1024x -font tahoma.ttf -pointsize 42 -channel RGBA -background transparent -fill dodgerblue1 -stroke slategray4 caption:'Hi, this is a text' intro$i.png");
}
But again this is a php question and not an Imagemagick one.

Re: how php loop in exec ?

Posted: 2014-12-03T15:33:17-07:00
by mostafanastary
thank you bonzo, work for me,
i open this topic to find a way to use loop in exec() witch use in php for imagemagick mostly.

Re: how php loop in exec ?

Posted: 2014-12-03T16:21:47-07:00
by fmw42
As I said above, take your commands and put them into a bash shell script. It is just a text file with the first line containing

#!/bin/bash

Then call the script in exec(). See Bonzo's web site at
http://www.rubblewebs.co.uk/index.php
http://www.rubblewebs.co.uk/imagemagick ... l_bash.php

This question is not really an Imagemagick question, but one for Unix shell scripting forums.

You really should learn to use the terminal to test your IM command lines before putting them into PHP. If it has an error, it is easier to detect in the terminal. Furthermore, if you run your commands in PHP exec(), you won't know if a problem is with PHP or Imagemagick or with your unix portions of the code.

To use the terminal, just copy and paste your code and hit return.

Re: how php loop in exec ?

Posted: 2014-12-04T02:42:57-07:00
by mostafanastary
thank you fred, i got it, and i will try to use terminal.

Re: how php loop in exec ?

Posted: 2014-12-05T04:19:17-07:00
by mostafanastary
Hi, i ask in stackoverflow about loop in exec and there is a solution for that:

http://stackoverflow.com/questions/2731 ... 5#27311115

Re: how php loop in exec ?

Posted: 2014-12-05T10:35:36-07:00
by fmw42
That is similar to what I told you earlier. Use PHP looping.

Re: how php loop in exec ?

Posted: 2014-12-05T10:58:53-07:00
by mostafanastary
Yes fred. thank you again.