Memory optimization

This commit is contained in:
Tobias Reich 2014-04-19 17:32:33 +02:00
parent fbf8309e44
commit db64df8820

View File

@ -163,16 +163,12 @@ class Photo extends Module {
# Create 1st version # Create 1st version
$thumb->cropThumbnailImage($width, $height); $thumb->cropThumbnailImage($width, $height);
$thumb->writeImage($newUrl); $thumb->writeImage($newUrl);
$thumb->clear();
$thumb->destroy();
# Create 2nd version # Create 2nd version
$thumb2x->cropThumbnailImage($width*2, $height*2); $thumb2x->cropThumbnailImage($width*2, $height*2);
$thumb2x->writeImage($newUrl2x); $thumb2x->writeImage($newUrl2x);
# Close thumb
$thumb->clear();
$thumb->destroy();
# Close thumb2
$thumb2x->clear(); $thumb2x->clear();
$thumb2x->destroy(); $thumb2x->destroy();
@ -200,14 +196,21 @@ class Photo extends Module {
case 'image/png': $sourceImg = imagecreatefrompng($url); break; case 'image/png': $sourceImg = imagecreatefrompng($url); break;
case 'image/gif': $sourceImg = imagecreatefromgif($url); break; case 'image/gif': $sourceImg = imagecreatefromgif($url); break;
case 'image/webp': $sourceImg = imagecreatefromwebp($url); break; case 'image/webp': $sourceImg = imagecreatefromwebp($url); break;
default: return false; default: return false; break;
} }
# Create thumb
imagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $width, $height, $newSize, $newSize); imagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $width, $height, $newSize, $newSize);
imagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $width*2, $height*2, $newSize, $newSize);
imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']); imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']);
imagedestroy($thumb);
# Create retina thumb
imagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $width*2, $height*2, $newSize, $newSize);
imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']); imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']);
imagedestroy($thumb2x);
# Free memory
imagedestroy($sourceImg);
} }
@ -248,7 +251,7 @@ class Photo extends Module {
} }
if ($rotateImage) { if ($rotateImage!==0) {
$image = new Imagick(); $image = new Imagick();
$image->readImage($path); $image->readImage($path);
$image->rotateImage(new ImagickPixel(), $rotateImage); $image->rotateImage(new ImagickPixel(), $rotateImage);
@ -262,7 +265,7 @@ class Photo extends Module {
$newWidth = $info['width']; $newWidth = $info['width'];
$newHeight = $info['height']; $newHeight = $info['height'];
$process = false;
$sourceImg = imagecreatefromjpeg($path); $sourceImg = imagecreatefromjpeg($path);
switch ($info['orientation']) { switch ($info['orientation']) {
@ -273,6 +276,7 @@ class Photo extends Module {
break; break;
case 3: case 3:
$process = true;
$sourceImg = imagerotate($sourceImg, -180, 0); $sourceImg = imagerotate($sourceImg, -180, 0);
break; break;
@ -287,6 +291,7 @@ class Photo extends Module {
break; break;
case 6: case 6:
$process = true;
$sourceImg = imagerotate($sourceImg, -90, 0); $sourceImg = imagerotate($sourceImg, -90, 0);
$newWidth = $info['height']; $newWidth = $info['height'];
$newHeight = $info['width']; $newHeight = $info['width'];
@ -298,6 +303,7 @@ class Photo extends Module {
break; break;
case 8: case 8:
$process = true;
$sourceImg = imagerotate($sourceImg, 90, 0); $sourceImg = imagerotate($sourceImg, 90, 0);
$newWidth = $info['height']; $newWidth = $info['height'];
$newHeight = $info['width']; $newHeight = $info['width'];
@ -305,11 +311,20 @@ class Photo extends Module {
} }
$newSourceImg = imagecreatetruecolor($newWidth, $newHeight); # Need to adjust photo?
if ($process===true) {
# Recreate photo
$newSourceImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight); imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
imagejpeg($newSourceImg, $path, 100); imagejpeg($newSourceImg, $path, 100);
# Free memory
imagedestroy($sourceImg);
imagedestroy($newSourceImg);
}
} }
# Call plugins # Call plugins