• Home
  • Programming
    • API
      • Google
    • Javascript
    • Php
    • Server
  • CMS
    • Magento
    • Yahoo! Store

Loop Spreadsheet and Update image height/width

Friday, 14 May 2021 19:13
Tim Ramsey
0 Comments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(storage_path('file.xlsx'));
        $worksheet = $spreadsheet->getActiveSheet();
        $highestRow = $worksheet->getHighestRow();
        for($row = 1; $row <= $highestRow; ++$row){;
            $value = $worksheet->getCellByColumnAndRow(23, $row)->getValue();
            $doc = new \DOMDocument();
            @$doc->loadHTML('<node>' . $value .'</node>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
            $images = $doc->getElementsByTagName('img');
            foreach ($images as $image) {
                if(!$image->hasAttribute('height') || $image->hasAttribute('width')){
                    $src = $image->getAttribute('src');
                    list($width, $height, $type, $attr) = @getimagesize($src);
                    if((int) $height > 0) {
                        $image->setAttribute('height', $height);
                    }
                    if((int) $width > 0) {
                        $image->setAttribute('width', $width);
                    }
                }
            }
            $document = utf8_decode($doc->saveHTML($doc->documentElement));
            $worksheet->setCellValueByColumnAndRow(23, $row, str_replace(array('<node>','</node>') , '' , $document));
        }
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
        $writer->save(storage_path('file-new.xlsx'));
This entry was posted on Friday, May 14th, 2021 at 7:13 pm and is filed under Php, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply

Cancel Reply

You must be logged in to post a comment.

Categories

  • CMS
    • Magento
    • Shopify
    • Yahoo! Store
  • Programming
    • API
      • Google
    • Javascript
    • Php
    • Server
    • SVN
  • VOIP

Recent Posts

  • How to write to Google Sheet with API in PHP
  • How to Migrate FreePBX server to a new server
  • Unifi Controller Java CPU 100%
  • Update Unifi Controller on Linux
  • Code Signing Desktop App