<?php
$zip = new ZipArchive();
if ($zip->open(__DIR__ . "/../R6 BOM.xlsx") === TRUE) {
    // 1. Get drawing relationships
    $drawingRelsRaw = $zip->getFromName('xl/drawings/_rels/drawing1.xml.rels');
    $rels = [];
    if ($drawingRelsRaw) {
        $xml = simplexml_load_string($drawingRelsRaw);
        foreach ($xml->Relationship as $rel) {
            $rels[(string)$rel['Id']] = (string)$rel['Target'];
        }
    }
    
    // 2. Get drawing anchors to map row -> image path
    $drawingRaw = $zip->getFromName('xl/drawings/drawing1.xml');
    $rowImages = [];
    if ($drawingRaw) {
        $xml = simplexml_load_string($drawingRaw);
        $xml->registerXPathNamespace('xdr', 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing');
        $xml->registerXPathNamespace('a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
        $xml->registerXPathNamespace('r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
        
        $anchors = $xml->xpath('//xdr:twoCellAnchor | //xdr:oneCellAnchor');
        foreach ($anchors as $anchor) {
            $from = $anchor->xpath('.//xdr:from/xdr:row');
            if (!$from) continue;
            $row = (int)$from[0];
            
            $blip = $anchor->xpath('.//a:blip');
            if (!$blip) continue;
            $embedAttr = $blip[0]->attributes('r', true);
            $rId = (string)$embedAttr['embed'];
            
            if (isset($rels[$rId])) {
                $target = $rels[$rId]; // e.g. ../media/image1.jpg
                $target = str_replace('../', 'xl/', $target);
                $rowImages[$row] = $target; // 0-indexed row
            }
        }
    }
    
    // Check first 5 row images
    foreach (array_slice($rowImages, 0, 5, true) as $r => $img) {
        echo "Row " . ($r + 1) . " has image $img\n";
    }
    
    $zip->close();
}

