phpexcel - PHP Excel image change size

452

Developing PHP code that will create Excel file using PHP Excel.

I need to place an image into XLS file. Problem I have is that dimension of the image are not the one I defined in my code:

$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());

This image is much higher than 74 pixels. Tried to add height and width also, but always the same.

Can you help me how to add image in my XLS, with dimensions I defined in PHP code?

Thank you in advance!

909

Answer

Solution:

Have you tried using setWidthAndHeight and set ResizeProportional true?

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

Answer

Solution:

I've tried it in different way but excel was forcing the image scale automatically. Anyway I solved it by setting first to false the "resize proportional" and then setting a custom width for the image

/* 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 */
499

Answer

Solution:

Same problem here, for me, i had to set the var after setting setImageResource like this

        ... 
        $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);  
668

Answer

Solution:

In PhpSpreadSheet is similar like 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);
185

Answer

Solution:

Add this, probably this would work

$objDrawing->setOffsetX(110);

People are also looking for solutions to the problem: php - How can I update multiple mysql columns of a row using the result of a select query?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.