PhpSpreadsheet
                                
                                 PhpSpreadsheet copied to clipboard
                                
                                    PhpSpreadsheet copied to clipboard
                            
                            
                            
                        how to center logo vertically and horizontally
Hello,
I want to center the logo in the middle of the specified area without stretching it vertically and horizontally. Logo sizes are not standard, they vary according to the member If the logo is larger than the specified area, it should be reduced by keeping the aspect ratio.

$spreadsheet->getActiveSheet()->mergeCells('D3:G7');
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$spreadsheet->getActiveSheet()->getStyle('D3:G7')
    ->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet()->getStyle('D3:G7')
    ->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$drawing->setName('Logo');
$drawing->setDescription('Logo');
$drawing->setPath($logo);
$drawing->setCoordinates('D3');
$drawing->setWorksheet($spreadsheet->getActiveSheet());
Is it possible to do something like this?
Thank you from now
Try making it a two-cell anchor drawing by adding the following statement before setWorksheet:
$drawing->setCoordinates2('G7');
BTW, I don't think the two alignment statements have any effect on the drawing.
Thank you for the answer I tried but nothing changed Always aligning left and top
Try:
$drawing->setOffsetX2($drawing->getImageWidth());
$drawing->setOffsetY2($drawing->getImageHeight());
Again no alignment
$spreadsheet->getActiveSheet()->mergeCells('D3:G7'); // LOGO ALANI
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$spreadsheet->getActiveSheet()->getStyle('D3:G7')
    ->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet()->getStyle('D3:G7')
    ->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$drawing->setName('Logo');
$drawing->setDescription('Logo');
$drawing->setPath($logo_excel);
$drawing->setCoordinates('D3');
$drawing->setOffsetX2($drawing->getImageWidth());
$drawing->setOffsetY2($drawing->getImageHeight());
$drawing->setWorksheet($spreadsheet->getActiveSheet());
Cell Alignment relates to content of a cell only; an Image isn't content, effectively an image is overlaid on the worksheet
So image alignment is not possible?
It's only possible if you do the math to work it out yourself: top-left corner of the image will be positioned top-left corner of the specified coordinate cell, then offset from that position as defined by offsetX/offsetY. If you're using two-cell anchoring, then you can also specify the positioning for the bottom-right corner of the image.
With a very few exceptions like sparklines and icon sets, MS Excel treats images as an overlay to the cell grid, not as part of the cell grid
I am using this library for the first time and also I know very little php I am using 4 columns, 5 rows for the logo
I am using the following values as column width
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setAutoSize(false);
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(70,'px');
$spreadsheet->getActiveSheet()->getColumnDimension('E')->setAutoSize(false);
$spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(95,'px');
$spreadsheet->getActiveSheet()->getColumnDimension('F')->setAutoSize(false);
$spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(70,'px');
$spreadsheet->getActiveSheet()->getColumnDimension('G')->setAutoSize(false);
$spreadsheet->getActiveSheet()->getColumnDimension('G')->setWidth(130,'px');
I am using the following values as row height
$spreadsheet->getActiveSheet()->getRowDimension(3)->setRowHeight(25,'px');
$spreadsheet->getActiveSheet()->getRowDimension(4)->setRowHeight(25,'px');
$spreadsheet->getActiveSheet()->getRowDimension(5)->setRowHeight(25,'px');
$spreadsheet->getActiveSheet()->getRowDimension(6)->setRowHeight(25,'px');
$spreadsheet->getActiveSheet()->getRowDimension(7)->setRowHeight(25,'px');
Can you suggest a sample code for centering the logo?
Since I am a beginner, my codes are beginner's work.
In the code you submitted after I suggested setOffsetX2, I do not see my original recommendation setCoordinates2. My experiments indicate that specifying setCoordinates2 causes the image to fill the rectangle with SetCoordinates in the top left and setCoordinates2 in the bottom right. Are you seeing something different when you do that? If so, please upload the image and the spreadsheet so that I can investigate further.
I don't fully understand but is there a sample run website like "https://jsfiddle.net/"? I would prepare an example