Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Use a wrapper element to crop and zoom SVG images
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Oct 1, 2014
1 parent 191f6e5 commit f189030
Showing 1 changed file with 13 additions and 43 deletions.
56 changes: 13 additions & 43 deletions system/modules/core/library/Contao/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,59 +556,29 @@ protected function executeResizeSvg()

$svgElement = $doc->documentElement;

// Advanced crop modes
switch ($this->getResizeMode())
{
case 'left_top':
$svgElement->setAttribute('preserveAspectRatio', 'xMinYMin slice');
break;

case 'center_top':
$svgElement->setAttribute('preserveAspectRatio', 'xMidYMin slice');
break;

case 'right_top':
$svgElement->setAttribute('preserveAspectRatio', 'xMaxYMin slice');
break;

case 'left_center':
$svgElement->setAttribute('preserveAspectRatio', 'xMinYMid slice');
break;

case 'center_center':
$svgElement->setAttribute('preserveAspectRatio', 'xMidYMid slice');
break;

case 'right_center':
$svgElement->setAttribute('preserveAspectRatio', 'xMaxYMid slice');
break;

case 'left_bottom':
$svgElement->setAttribute('preserveAspectRatio', 'xMinYMax slice');
break;

case 'center_bottom':
$svgElement->setAttribute('preserveAspectRatio', 'xMidYMax slice');
break;

case 'right_bottom':
$svgElement->setAttribute('preserveAspectRatio', 'xMaxYMax slice');
break;
}

// Set the viewBox attribute from the original dimensions
if (!$svgElement->hasAttribute('viewBox'))
{
$origWidth = $svgElement->getAttribute('width');
$origHeight = $svgElement->getAttribute('height');

$svgElement->setAttribute('viewBox', '0 0 ' . intval($origWidth) . ' ' . intval($origHeight));
$svgElement->setAttribute('viewBox', '0 0 ' . floatval($origWidth) . ' ' . floatval($origHeight));
}

$coordinates = $this->computeResize();

$svgElement->setAttribute('width', $coordinates['width'] . 'px');
$svgElement->setAttribute('height', $coordinates['height'] . 'px');
$svgElement->setAttribute('x', $coordinates['target_x']);
$svgElement->setAttribute('y', $coordinates['target_y']);
$svgElement->setAttribute('width', $coordinates['target_width']);
$svgElement->setAttribute('height', $coordinates['target_height']);

$svgWrapElement = $doc->createElementNS('http://www.w3.org/2000/svg', 'svg');
$svgWrapElement->setAttribute('version', '1.1');
$svgWrapElement->setAttribute('width', $coordinates['width']);
$svgWrapElement->setAttribute('height', $coordinates['height']);
$svgWrapElement->appendChild($svgElement);

$doc->appendChild($svgWrapElement);

if ($this->fileObj->extension == 'svgz')
{
Expand Down

0 comments on commit f189030

Please sign in to comment.