Colors as hex values saved in a text file - SYNTAX

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
gofly

Colors as hex values saved in a text file - SYNTAX

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

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

Post 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.
Last edited by fmw42 on 2010-03-19T16:01:24-07:00, edited 1 time in total.
gofly

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

Post 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
Post Reply