PHP Excel图像更改大小

时间:2012-07-09 23:33:35

标签: php phpexcel xls

开发将使用PHP Excel创建Excel文件的PHP代码。

我需要将图像放入XLS文件中。我遇到的问题是图像的尺寸不是我在代码中定义的尺寸:

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Water_Level');
$objDrawing->setDescription('Water_Level');
$objDrawing->setPath('img/logo-rab.jpg');
$objDrawing->setHeight(74);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

此图像远高于74像素。试图增加高度和宽度,但总是相同。

你能帮我看看如何在我的XLS中添加图像,我在PHP代码中定义了维度吗?

提前谢谢!

5 个答案:

答案 0 :(得分:6)

您是否尝试过使用setWidthAndHeight并将ResizeProportional设为true?

$objDrawing->setWidthAndHeight(148,74);
$objDrawing->setResizeProportional(true);

答案 1 :(得分:3)

我以不同的方式尝试过它,但excel强制自动强制缩放图像。无论如何,我通过先设置为false来解决它" resize proportional"然后为图像设置自定义宽度

/* ADD LOGO */
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('../images/logoexcel.png');
$objDrawing->setCoordinates('A1');
// set resize to false first
$objDrawing->setResizeProportional(false);
// set width later
$objDrawing->setWidth(45);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(35);
/* END LOGO */

答案 2 :(得分:1)

这里的问题相同,对我来说,我必须在设置setImageResource之后设置var

        ... 
        $this->objPHPExcel->getActiveSheet()->SetCellValue($index, $entry);
        $this->gdImage = imagecreatefromjpeg('/path/img.jpg');
        $this->objDrawing->setCoordinates('C1');
        ...
        }


        function save_excel(){
        $this->objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
        $this->objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);    
        $this->objDrawing->setImageResource($this->gdImage);
        $this->objDrawing->setHeight(100);  

答案 3 :(得分:0)

在PhpSpreadSheet中类似于PHPExcel:

$drawing = new Drawing();

$drawing->setName( $name );
$drawing->setDescription( $name );
$drawing->setPath( $path );
$drawing->setWorksheet( $sheet );
$drawing->setCoordinates( $cell );

$drawing->setHeight(100);
$drawing->setResizeProportional(true);

答案 4 :(得分:-2)

添加此项,可能会有效

$ objDrawing-> setOffsetX(110);