Sorry, that's still not it.
I wrote a script that hopefully better illustrates what I'm trying to do.
First, here's the full array that I'm starting with (sorry this is long, but I thought a full example might be useful):
[text]
This is the result of: exec('identify -verbose some_image.png' , $img_data);
Array
(
[0] => Image: some_image.png
[1] => Format: PNG (Portable Network Graphics)
[2] => Class: DirectClass
[3] => Geometry: 800x995+0+0
[4] => Type: TrueColorMatte
[5] => Endianess: Undefined
[6] => Colorspace: RGB
[7] => Depth: 8-bit
[8] => Channel depth:
[9] => Red: 8-bit
[10] => Green: 8-bit
[11] => Blue: 8-bit
[12] => Alpha: 1-bit
[13] => Channel statistics:
[14] => Red:
[15] => Min: 0 (0)
[16] => Max: 255 (1)
[17] => Mean: 214.231 (0.84012)
[18] => Standard deviation: 67.8397 (0.266038)
[19] => Green:
[20] => Min: 0 (0)
[21] => Max: 255 (1)
[22] => Mean: 203.409 (0.797681)
[23] => Standard deviation: 69.5713 (0.272829)
[24] => Blue:
[25] => Min: 0 (0)
[26] => Max: 255 (1)
[27] => Mean: 200.052 (0.784518)
[28] => Standard deviation: 77.8171 (0.305165)
[29] => Opacity:
[30] => Min: 0 (0)
[31] => Max: 0 (0)
[32] => Mean: 0 (0)
[33] => Standard deviation: 0 (0)
[34] => Rendering intent: Saturation
[35] => Gamma: 0.45455
[36] => Chromaticity:
[37] => red primary: (0.64,0.33)
[38] => green primary: (0.3,0.6)
[39] => blue primary: (0.15,0.06)
[40] => white point: (0.3127,0.329)
[41] => Resolution: 28.35x28.35
[42] => Units: PixelsPerCentimeter
[43] => Filesize: 220.906kb
[44] => Interlace: None
[45] => Background color: white
[46] => Border color: rgb(223,223,223)
[47] => Matte color: grey74
[48] => Transparent color: none
[49] => Page geometry: 800x995+0+0
[50] => Dispose: Undefined
[51] => Iterations: 0
[52] => Compression: Zip
[53] => Orientation: Undefined
[54] => Signature: 1518c127005b589fa58edddc32d21dff2d5711bade8258a93345f3c71163525b
[55] => Tainted: False
[56] => Version: ImageMagick 6.3.7 06/04/09 Q16
http://www.imagemagick.org
)
[/text]
And here's the code that kind of does what I want:
Code: Select all
<?php
exec('identify -verbose some_image.png',$img_data);
$chain = 'root';
// $chain will be added on to so that an entry might look like: root.Channel statistics.Green.Standard deviation=69...
// But instead of dots I want nested arrays: $root[Channel statistics][Green][Standard deviation] = 69
foreach($img_data as $line_number=>$line) {
$line_parts = explode(':',$line);
// determine number of spaces that line is indented
$indent = 0;
while($line_parts[0][$indent] == ' ')
$indent ++;
// $indent is now the quantity of leading spaces
$indent_diff = ($indent - $last_indent) / 2; // $indent_diff is difference from last indent (2 spaces per indentation)
/*
Example:
LINE ONE //$indent_diff = 0
LINE TWO //$indent_diff = +1
LINE THREE //$indent_diff = +1
LINE FOUR //$indent_diff = -2
*/
// trim key and value
foreach($line_parts as $part=>$value) $line_parts[$part] = trim($value);
if($indent_diff < 0) { // remove nodes from chain
for($remove_i = $indent_diff; $remove_i < 0; $remove_i ++) {
$chain = substr($chain,0,strrpos($chain,'.'));
}
}
if($line_parts[1] == '') { // line does not have a value, so add a node
$chain .= '.' . $line_parts[0] ;
}
else { // line has a value, make an entry in $final, where $line_parts[0] is the last node
$final[] = $chain . '.' . $line_parts[0] . '=' . $line_parts[1];
}
$last_indent = $indent; // set for next iteration
}
print_r($final);
?>
Which outputs:
[text]
Array
(
[0] => root.Image=some_image.png
[1] => root.Format=PNG (Portable Network Graphics)
[2] => root.Class=DirectClass
[3] => root.Geometry=800x995+0+0
[4] => root.Type=TrueColorMatte
[5] => root.Endianess=Undefined
[6] => root.Colorspace=RGB
[7] => root.Depth=8-bit
[8] => root.Channel depth.Red=8-bit
[9] => root.Channel depth.Green=8-bit
[10] => root.Channel depth.Blue=8-bit
[11] => root.Channel depth.Alpha=1-bit
[12] => root.Channel statistics.Red.Min=0 (0)
[13] => root.Channel statistics.Red.Max=255 (1)
[14] => root.Channel statistics.Red.Mean=214.231 (0.84012)
[15] => root.Channel statistics.Red.Standard deviation=67.8397 (0.266038)
[16] => root.Channel statistics.Green.Min=0 (0)
[17] => root.Channel statistics.Green.Max=255 (1)
[18] => root.Channel statistics.Green.Mean=203.409 (0.797681)
[19] => root.Channel statistics.Green.Standard deviation=69.5713 (0.272829)
[20] => root.Channel statistics.Blue.Min=0 (0)
[21] => root.Channel statistics.Blue.Max=255 (1)
[22] => root.Channel statistics.Blue.Mean=200.052 (0.784518)
[23] => root.Channel statistics.Blue.Standard deviation=77.8171 (0.305165)
[24] => root.Channel statistics.Opacity.Min=0 (0)
[25] => root.Channel statistics.Opacity.Max=0 (0)
[26] => root.Channel statistics.Opacity.Mean=0 (0)
[27] => root.Channel statistics.Opacity.Standard deviation=0 (0)
[28] => root.Rendering intent=Saturation
[29] => root.Gamma=0.45455
[30] => root.Chromaticity.red primary=(0.64,0.33)
[31] => root.Chromaticity.green primary=(0.3,0.6)
[32] => root.Chromaticity.blue primary=(0.15,0.06)
[33] => root.Chromaticity.white point=(0.3127,0.329)
[34] => root.Resolution=28.35x28.35
[35] => root.Units=PixelsPerCentimeter
[36] => root.Filesize=220.906kb
[37] => root.Interlace=None
[38] => root.Background color=white
[39] => root.Border color=rgb(223,223,223)
[40] => root.Matte color=grey74
[41] => root.Transparent color=none
[42] => root.Page geometry=800x995+0+0
[43] => root.Dispose=Undefined
[44] => root.Iterations=0
[45] => root.Compression=Zip
[46] => root.Orientation=Undefined
[47] => root.Signature=1518c127005b589fa58edddc32d21dff2d5711bade8258a93345f3c71163525b
[48] => root.Tainted=False
[49] => root.Version=ImageMagick 6.3.7 06/04/09 Q16 http
)
[/text]
The "dots" are merely to demonstrate what I'm trying to get with a PHP array.
So instead of element 12, for example, I want: $root[Channel statistics][Red][Min] = '0 (0)'
My primary problem is that while I can add and remove nodes onto a string, I don't know how to do so with an array (while keeping previous entries).
Thank you again.