Martyn Hill
Martyn Hill

Reputation: 11

PHP Excel upload images in order

I couldn't find the answer for this anywhere on google. I'm uploading an excel sheet via PHP using the PHPExcel_Worksheet_MemoryDrawing class however it seems to be uploading the images in a random order.

Is there anyway to specify which order it uploads in such as row $n. Currently I'm uploading the file and pushing each image to an array using $i as the value but it seems to be selecting images at random. In the excel file I have also renamed images 001, 002 etc but it's still seems to be random once it uploads.

$i=0;
foreach ($objPHPExcel->getSheetByName("Sheet1")->getDrawingCollection() as $drawing) {
    if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
        ob_start();
        call_user_func(
            $drawing->getRenderingFunction(),
            $drawing->getImageResource()
        );
        $imageContents = ob_get_contents();

        ob_end_clean();
        $extension = 'jpg';
        $myFileName = $dir_to_create.'/'.date('Ymjis').rand().'.'.$extension;
                        array_push($td, $myFileName);

        file_put_contents($myFileName,$imageContents);
        $images_data[$i] = $myFileName;
        $i++;
    }
}

Upvotes: 0

Views: 179

Answers (1)

Martyn Hill
Martyn Hill

Reputation: 11

A workaround I'm using is to call the coordinates and use that as my array key then use asort on the array to sort it. Although this method works I would like to know if there's an alternative method to upload in order.

$row = $drawing->getCoordinates();
$images_data[$row] = $myFileName;
asort($images_data);

Upvotes: 1

Related Questions