how php loop in exec ?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

how php loop in exec ?

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how php loop in exec ?

Post 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
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: how php loop in exec ?

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how php loop in exec ?

Post 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.
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: how php loop in exec ?

Post by mostafanastary »

i never use terminal and shell script, i need use that with php, in imagemagick command, pipes dosen't work.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: how php loop in exec ?

Post 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.
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: how php loop in exec ?

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how php loop in exec ?

Post 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.
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: how php loop in exec ?

Post by mostafanastary »

thank you fred, i got it, and i will try to use terminal.
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: how php loop in exec ?

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how php loop in exec ?

Post by fmw42 »

That is similar to what I told you earlier. Use PHP looping.
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: how php loop in exec ?

Post by mostafanastary »

Yes fred. thank you again.
Post Reply