/** * @name arrayComplexSearch * @desc Provides a utility method for searching for a key + value pair within a multidimensional array * @param array $array * @param string $key * @param string $value * @return Ambigous <multitype:unknown , multitype:> */ function arrayComplexSearch($array, $key, $value) { $results = array(); if (is_array($array)) { if (isset($array[$key]) && $array[$key] == $value) { $results[] = $array; } foreach ($array as $subarray) { $results = array_merge($results, arrayComplexSearch($subarray, $key, $value)); } } return $results; }
/** * @name loadDrawing * @desc Generates an image array (salvaged from previous version) */ public function loadDrawing() { # Get the blipFill for the imageRefId $mcAltContentXPath = $this->xPath->query("*/a:graphic/a:graphicData/pic:pic/pic:blipFill", $this->dom); $blipNode = $rectNode = null; foreach ($mcAltContentXPath as $blipFill) { # The blip however is always required to get the imageRefId if ($blipFill->nodeName == null) { continue; } $blipNode = $blipFill; } # Get the prev. element to load the alterateContent block $prevElement = $this->dom->parentNode->previousSibling; if (!isset($prevElement->nodeName)) { continue; } # Load the alt Content for the dimensions $mcDimensionXPath = $this->xPath->query("mc:AlternateContent/mc:Fallback/w:pict/v:rect", $prevElement); foreach ($mcDimensionXPath as $dimensionWrapper) { # If 'rect' is not found, we just use image width/height = auto so it is not required if ($dimensionWrapper->nodeName != null) { $rectNode = $dimensionWrapper; } } # Get the imageToUseId by searching the blip node for an id if ($blipNode != null) { $blipQuery = $this->xPath->query("a:blip", $blipNode); foreach ($blipQuery as $blipRes) { foreach ($blipRes->attributes as $blipEmbedNode) { if ($blipEmbedNode->nodeName == 'r:embed') { $imageToUseId = $blipEmbedNode->nodeValue; break 2; } } } # Use the id as a key within the _images array $imageData = arrayComplexSearch($this->docx->images, 'id', $imageToUseId); if (!is_array($imageData)) { return null; } # If the image doesnt have a width defined, then the image parser skipped on this specific image, so skip it if (!isset($imageData[0]['w'])) { return null; } # Defaults are initally set as 'auto' $w = $imageData[0]['w']; $h = $imageData[0]['h']; # Load the rect node if available to load the image dimensions if ($rectNode != null) { $rectStyles = $rectNode->attributes; foreach ($rectStyles as $rectStyleNode) { if ($rectStyleNode->nodeName == 'style') { $imageStyleArray = explode(";", $rectStyleNode->nodeValue); foreach ($imageStyleArray as $imageStyle) { $styleInfo = explode(":", $imageStyle); if (strtolower($styleInfo[0]) == 'width') { $w = $styleInfo[1]; } if (strtolower($styleInfo[0]) == 'height') { $h = $styleInfo[1]; } } break; } } } # Collate the image into the parsed array return array('type' => 'image', 'name' => $imageData[0]['title'], 'h' => $h, 'w' => $w, 'data' => $imageData[0]['data']); } }