Skip to content

Enable twoCellAnchor for images - auto move and resize function #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/PhpSpreadsheet/Worksheet/BaseDrawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ class BaseDrawing implements IComparable
*/
protected $height;

/**
* Move and resize type.
*
* @var string
*/
protected $resizeType = "oneCellAnchor";

/**
* Proportional resize.
*
Expand Down Expand Up @@ -431,7 +438,31 @@ public function setResizeProportional($pValue)

return $this;
}

/**
* Get ResizeType.
*
* @return string
*/
public function getResizeType()
{
return $this->resizeType;
}

/**
* Set ResizeType.
*
* @param string $pValue
*
* @return BaseDrawing
*/
public function setResizeType($pValue)
{
$this->resizeType = $pValue;

return $this;
}

/**
* Get Rotation.
*
Expand Down
16 changes: 13 additions & 3 deletions src/PhpSpreadsheet/Writer/Xlsx/Drawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public function writeChart(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Chart
public function writeDrawing(XMLWriter $objWriter, BaseDrawing $pDrawing, $pRelationId = -1, $hlinkClickId = null)
{
if ($pRelationId >= 0) {
// xdr:oneCellAnchor
$objWriter->startElement('xdr:oneCellAnchor');
// xdr oneCell or twoCell Anchor
$objWriter->startElement('xdr:'.$pDrawing->getResizeType());
// Image location
$aCoordinates = Coordinate::coordinateFromString($pDrawing->getCoordinates());
$aCoordinates[0] = Coordinate::columnIndexFromString($aCoordinates[0]);
Expand All @@ -175,7 +175,17 @@ public function writeDrawing(XMLWriter $objWriter, BaseDrawing $pDrawing, $pRela
$objWriter->writeElement('xdr:row', $aCoordinates[1] - 1);
$objWriter->writeElement('xdr:rowOff', \PhpOffice\PhpSpreadsheet\Shared\Drawing::pixelsToEMU($pDrawing->getOffsetY()));
$objWriter->endElement();


// xdr:to for twoCellAnchor
if($pDrawing->getResizeType()==='twoCellAnchor'){
$objWriter->startElement('xdr:to');
$objWriter->writeElement('xdr:col', $aCoordinates[0]);
$objWriter->writeElement('xdr:colOff', \PhpOffice\PhpSpreadsheet\Shared\Drawing::pixelsToEMU($pDrawing->getOffsetX()));
$objWriter->writeElement('xdr:row', $aCoordinates[1]);
$objWriter->writeElement('xdr:rowOff', \PhpOffice\PhpSpreadsheet\Shared\Drawing::pixelsToEMU($pDrawing->getOffsetY()));
$objWriter->endElement();
}

// xdr:ext
$objWriter->startElement('xdr:ext');
$objWriter->writeAttribute('cx', \PhpOffice\PhpSpreadsheet\Shared\Drawing::pixelsToEMU($pDrawing->getWidth()));
Expand Down