9

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!

pnuts
  • 58,317
  • 11
  • 87
  • 139
user198003
  • 11,029
  • 28
  • 94
  • 152
  • Which writer are you using? The code that creates the drawing isn't any different, but it does make a difference knowing what code is actually writing that image information to file. – Mark Baker Jul 10 '12 at 08:13
  • Could it be related to this issue from the phpexcel site? http://phpexcel.codeplex.com/workitem/14815 – Mark Baker Jul 10 '12 at 08:15
  • it looks like there is some relation with width of cell, because image changes size if i move it (ie.) from left part of XLS to right – user198003 Jul 10 '12 at 09:10
  • 2
    This is caused by using `->setAutoSize(true);` for the rows. As far as I know there is no work-a-round for this. Leaving this here as I am looking for a solution and this solves the image issue but then your data is not set properly... My fix was to apply the logo after the resize is done – Jeremy Jun 03 '14 at 23:12

5 Answers5

7

Have you tried using setWidthAndHeight and set ResizeProportional true?

$objDrawing->setWidthAndHeight(148,74);
$objDrawing->setResizeProportional(true);
Fedy Venom
  • 399
  • 4
  • 11
  • doesn't work. i set image to square size, after changing cell size, and the image will be resized to wrong size – TomSawyer May 27 '20 at 17:02
5

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 */
Brugolo
  • 4,685
  • 3
  • 22
  • 14
1

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);  
Tim
  • 147
  • 1
  • 10
0

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);
-2

Add this, probably this would work

$objDrawing->setOffsetX(110);

mfk
  • 1