Skip to content

Commit

Permalink
Destroy images when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Mar 10, 2020

Unverified

This user has not yet uploaded their public signing key.
1 parent 63214ec commit 371041b
Showing 2 changed files with 8 additions and 20 deletions.
26 changes: 6 additions & 20 deletions src/Writer/PngWriter.php
Original file line number Diff line number Diff line change
@@ -48,18 +48,18 @@ public function writeString(QrCodeInterface $qrCode): string
return $string;
}

/** @return resource */
private function createImage(array $data, QrCodeInterface $qrCode)
{
$baseSize = $qrCode->getRoundBlockSize() ? $data['block_size'] : 25;

$baseImage = $this->createBaseImage($baseSize, $data, $qrCode);
$interpolatedImage = $this->createInterpolatedImage($baseImage, $data, $qrCode);

imagedestroy($baseImage);

return $interpolatedImage;
}

/** @return resource */
private function createBaseImage(int $baseSize, array $data, QrCodeInterface $qrCode)
{
$image = imagecreatetruecolor($data['block_count'] * $baseSize, $data['block_count'] * $baseSize);
@@ -83,11 +83,6 @@ private function createBaseImage(int $baseSize, array $data, QrCodeInterface $qr
return $image;
}

/**
* @param resource $baseImage
*
* @return resource
*/
private function createInterpolatedImage($baseImage, array $data, QrCodeInterface $qrCode)
{
$image = imagecreatetruecolor($data['outer_width'], $data['outer_height']);
@@ -104,11 +99,6 @@ private function createInterpolatedImage($baseImage, array $data, QrCodeInterfac
return $image;
}

/**
* @param resource $sourceImage
*
* @return resource
*/
private function addLogo($sourceImage, string $logoPath, int $logoWidth = null, int $logoHeight = null)
{
$mimeType = $this->getMimeType($logoPath);
@@ -139,14 +129,11 @@ private function addLogo($sourceImage, string $logoPath, int $logoWidth = null,

imagecopyresampled($sourceImage, $logoImage, intval($logoX), intval($logoY), 0, 0, $logoWidth, $logoHeight, $logoSourceWidth, $logoSourceHeight);

imagedestroy($logoImage);

return $sourceImage;
}

/**
* @param resource $sourceImage
*
* @return resource
*/
private function addLabel($sourceImage, string $label, string $labelFontPath, int $labelFontSize, string $labelAlignment, array $labelMargin, array $foregroundColor, array $backgroundColor)
{
if (!function_exists('imagettfbbox')) {
@@ -176,6 +163,8 @@ private function addLabel($sourceImage, string $label, string $labelFontPath, in
// Copy source image to target image
imagecopyresampled($targetImage, $sourceImage, 0, 0, 0, 0, $sourceWidth, $sourceHeight, $sourceWidth, $sourceHeight);

imagedestroy($sourceImage);

switch ($labelAlignment) {
case LabelAlignment::LEFT:
$labelX = $labelMargin['l'];
@@ -194,9 +183,6 @@ private function addLabel($sourceImage, string $label, string $labelFontPath, in
return $targetImage;
}

/**
* @param resource $image
*/
private function imageToString($image): string
{
ob_start();
2 changes: 2 additions & 0 deletions src/Writer/SvgWriter.php
Original file line number Diff line number Diff line change
@@ -104,6 +104,8 @@ private function addLogo(SimpleXMLElement $svg, int $imageWidth, int $imageHeigh
$logoSourceWidth = imagesx($logoImage);
$logoSourceHeight = imagesy($logoImage);

imagedestroy($logoImage);

if (null === $logoWidth) {
$logoWidth = $logoSourceWidth;
}

0 comments on commit 371041b

Please sign in to comment.