php – Coulmn in which image is embedded changes size when opened in Windows for excel (PHPExcel)-ThrowExceptions

Exception or error:

I am using PHPExcel to create my excel sheet.

The excel opens just fine on Ubuntu machine but when the same excelsheet is opened in Google sheets or any Windows machine then the column in which I have embedded images changes size. Rest all columns retain their size.

Below is my code for embedding image in an Excel cell:-

$oCell = $sheet ->getCell('B'.$mainCounter);
        $oCell->setValue('');
        $imageName = downloadImage($product_detail['img'], $product_detail['sku']);

        if(!empty($imageName)){

            $objDrawing = new PHPExcel_Worksheet_Drawing();

            if(mime_content_type(__DIR__."/images/".$imageName.".webp")=="image/webp") {
                $im = imagecreatefromwebp(__DIR__."/images/".$imageName.".webp");

                imagejpeg($im, __DIR__."/images/".$imageName.".jpg", 100);
                if(file_exists(__DIR__."/images/".$imageName.".jpg")) {

                // imagejpeg($im, __DIR__."/images/".$imageName.".jpg", 100);
                    $objDrawing->setPath( __DIR__."/images/".$imageName.".jpg");
                }

                $objDrawing->setWidth(216);
                $objDrawing->setHeight(324);
                $objDrawing->setOffsetX(7);
                $objDrawing->setOffsetY(7);
                $objDrawing->setCoordinates($oCell->getCoordinate());
                $objDrawing->setWorksheet($phpExcel->getActiveSheet());
            }
            else if(mime_content_type(__DIR__."/images/".$imageName.".jpg")=="image/jpeg") {

                if(file_exists(__DIR__."/images/".$imageName.".jpg")) {

                    // imagejpeg($im, __DIR__."/images/".$imageName.".jpg", 100);
                    $objDrawing->setPath( __DIR__."/images/".$imageName.".jpg");
                }

                $objDrawing->setWidth(216);
                $objDrawing->setHeight(324);
                $objDrawing->setOffsetX(7);
                $objDrawing->setOffsetY(7);
                $objDrawing->setCoordinates($oCell->getCoordinate());
                $objDrawing->setWorksheet($phpExcel->getActiveSheet());
            }
        }
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *