PhpSpreadsheet icon indicating copy to clipboard operation
PhpSpreadsheet copied to clipboard

how to center logo vertically and horizontally

Open Ademgenc53 opened this issue 3 years ago • 10 comments

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.

Ekran görüntüsü 2022-08-27 173615

$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

Ademgenc53 avatar Aug 27 '22 14:08 Ademgenc53

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.

oleibman avatar Aug 27 '22 22:08 oleibman

Thank you for the answer I tried but nothing changed Always aligning left and top

Ademgenc53 avatar Aug 28 '22 16:08 Ademgenc53

Try:

$drawing->setOffsetX2($drawing->getImageWidth());
$drawing->setOffsetY2($drawing->getImageHeight());

oleibman avatar Aug 28 '22 17:08 oleibman

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

Ademgenc53 avatar Aug 28 '22 18:08 Ademgenc53

Cell Alignment relates to content of a cell only; an Image isn't content, effectively an image is overlaid on the worksheet

MarkBaker avatar Aug 28 '22 22:08 MarkBaker

So image alignment is not possible?

Ademgenc53 avatar Aug 29 '22 07:08 Ademgenc53

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

MarkBaker avatar Aug 29 '22 07:08 MarkBaker

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.

Ademgenc53 avatar Aug 29 '22 10:08 Ademgenc53

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.

oleibman avatar Sep 02 '22 03:09 oleibman

I don't fully understand but is there a sample run website like "https://jsfiddle.net/"? I would prepare an example

Ademgenc53 avatar Sep 02 '22 11:09 Ademgenc53