I am using PHPExcel to write a set of images to an Excel file using PHP. Inserting images does not seem to be a problem, however, I am having difficulty setting the appropriate line height according to the image height.
I managed to use pixelsToCellDimension to calculate the correct column width, but this does not seem to be suitable for calculating the row height. After checking the rest of the functions of the PHPExcel_Shared_Drawing and Googling class, I was stunned how to do this.
My code is:
// Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("creator") ->setLastModifiedBy("modifiedby") ->setTitle("Title") ->setSubject("Subject") ->setDescription("Description") ->setKeywords("keyword1 keyword2") ->setCategory("category"); // Get default font $defaultFont = $objPHPExcel->getDefaultStyle()->getFont();
[some code to create my images and add their paths to the array]
foreach( $filePaths as $i => $filePath ) { $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setWorkSheet( $objPHPExcel->getActiveSheet() ); $objDrawing->setName("name"); $objDrawing->setDescription("Description"); $objDrawing->setPath( $filePath ); $size = getimagesize($filePath); $columnWidth = PHPExcel_Shared_Drawing::pixelsToCellDimension($size[0], $defaultFont); $rowHeight = PHPExcel_Shared_Drawing::pixelsToCellDimension($size[1], $defaultFont); $objDrawing->setCoordinates('A' . ($i+1) ); $objDrawing->setOffsetX(0); $objDrawing->setOffsetY(0);
source share