I'm trying to align an image using PHPExcel but i can't because the image is overlaid above the worksheet.
// Create new picture object
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('my_img.jpg');
// Insert picture
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
// Style cell
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
The text align of A1 change to right align but the image it still aligned on the left.
解决方案
Here is an idea:
You need to determine the maximum width / height (by experimentation). Save the values.
// Logo
$maxWidth = 700;
$maxHeight = 40;
Here is the rest of the code:
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objDrawing->setName("Logo");
$objDrawing->setDescription("Company Logo");
$objDrawing->setPath('logo.png');
$objDrawing->setCoordinates('A1');
$objDrawing->setHeight($maxHeight);
// This is the "magic" formula
$offsetX =$maxWidth - $objDrawing->getWidth();
$objDrawing->setOffsetX($offsetX);
Hope this helps.