Page 1 of 1

Colors as hex values saved in a text file - SYNTAX

Posted: 2010-03-11T09:28:30-07:00
by gofly
Hi all,

I've been dabbling with IM and really impressed with the results.
Using it via a PHP exec command (shared hosting).

However, I am doing the following, getting an image to convert to 4 colors and display the data, works OK:

Code: Select all

$Tnl="filename.png";
$ClrTxt="filename.txt";

exec("/usr/bin/convert convert $TnL -scale 2x2\! $ClrTxt");
And gives the expected result:
# ImageMagick pixel enumeration: 2,2,255,RGB
0,0: (216,216,222) #D8D8DE
1,0: (239,226,208) #EFE2D0
0,1: (230,223,225) #E6DFE1
1,1: (239,225,206) #EFE1CE
But I am (desperately) trying to get it just to output the Hex values as such:
#D8D8DE,#EFE2D0,#E6DFE1,#EFE1CE
with or without the comma, either in a txt file that can easily be parsed or as a variable that PHP can pick up and use.

Been looking through MOST of the online docs, but just can't get it to work. Tried lots of things, but even the -format command doesn't seem to be usefull in this case.

Any clues on how to do this? :?

Thanks for any help. :D
Gofly

Re: Colors as hex values saved in a text file - SYNTAX

Posted: 2010-03-11T09:51:08-07:00
by fmw42
try

<?php
exec("/usr/bin/convert convert $TnL -scale 2x2\! txt:- | tail -n +2 | sed -n 's/^.*\([#].*\) .*$/\1/p' 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>

The command works in command line as for example:
convert rose: -scale 2x2\! txt:- | tail -n +2 | sed -n 's/^.*\([#].*\) .*$/\1/p'
#7A4241
#AD6259
#A6726D
#7B4F3B


But your server must allow you to use tail and sed. My server at Godaddy allows tail, but does not allow me to use sed.

Using the $out parameter in exec, allows you to put the text into an array (via the 2>&1). From there you can use it in PHP and if you cannot use sed, then you can use something similar in PHP to extract just the hex values.

Re: Colors as hex values saved in a text file - SYNTAX

Posted: 2010-03-12T07:53:22-07:00
by gofly
Thanks a million fmw42 :D

Cannot use sed but as you mentionned used $out & substr it with PHP & now works like a charm :D

Thanks again ;-)

GoFly