Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
lib (collage): rotate images where needed
Browse files Browse the repository at this point in the history
Change-Id: I1dcc7c245b01e13d56a7267ff2c1146b012563aa
  • Loading branch information
andi34 committed Dec 30, 2020
1 parent a44267c commit 2257771
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions lib/collage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ function createCollage($srcImagePaths, $destImagePath, $takeFrame, $takeFrameAlw
return false;
}

list($width, $height) = getimagesize($srcImagePaths[0]);
if ($width > $height) {
$degrees = 90;
$landscape = true;
} else {
$degrees = 0;
$landscape = false;
list($height, $width) = getimagesize($srcImagePaths[0]);
}

switch (LAYOUT) {
case '2x2':
list($width, $height) = getimagesize($srcImagePaths[0]);

$my_collage = imagecreatetruecolor($width, $height);
$background = imagecolorallocate($my_collage, 0, 0, 0);
imagecolortransparent($my_collage, $background);
Expand All @@ -25,6 +33,16 @@ function createCollage($srcImagePaths, $destImagePath, $takeFrame, $takeFrameAlw
return false;
}

if (!$landscape) {
$rotate_after_creation = true;
$tempImage = imagecreatefromjpeg($srcImagePaths[$i]);
$tempSubRotated = imagerotate($tempImage, 90, 0);
imagejpeg($tempSubRotated, $srcImagePaths[$i]);
imagedestroy($tempImage);
} else {
$rotate_after_creation = false;
}

$tempSubImage = imagecreatefromjpeg($srcImagePaths[$i]);

if ($takeFrame && $takeFrameAlways) {
Expand All @@ -45,18 +63,10 @@ function createCollage($srcImagePaths, $destImagePath, $takeFrame, $takeFrameAlw
$y = imagesy($my_collage) / 2 - imagesy($frame) / 2;
imagecopy($my_collage, $frame, $x, $y, 0, 0, $width, $height);
}
imagejpeg($my_collage, $destImagePath);
imagedestroy($my_collage);
break;
case '2x4':
$degrees = 90;

list($width_before, $height_before) = getimagesize($srcImagePaths[0]);

$my_collage_width = $width_before;
$my_collage_height = $height_before;

$my_collage = imagecreatetruecolor($my_collage_width, $my_collage_height);
$rotate_after_creation = true;
$my_collage = imagecreatetruecolor($width, $height);
$background = imagecolorallocate($my_collage, 240, 240, 240);
imagefill($my_collage, 0, 0, $background);

Expand All @@ -78,22 +88,22 @@ function createCollage($srcImagePaths, $destImagePath, $takeFrame, $takeFrameAlw
}

$tempSubRotated = imagerotate($tempSubImage, $degrees, 0);
$images_rotated[] = resizeImage($tempSubRotated, $height_before / 3.3, $width_before / 3.5);
$images_rotated[] = resizeImage($tempSubRotated, $height / 3.3, $width / 3.5);
}

if ($takeFrame && !$takeFrameAlways) {
$frame = imagecreatefrompng($framePath);
$frame = resizePngImage($frame, $my_collage_width, $my_collage_height);
$frame = resizePngImage($frame, $width, $height);
$x = imagesx($my_collage) / 2 - imagesx($frame) / 2;
$y = imagesy($my_collage) / 2 - imagesy($frame) / 2;
imagecopy($my_collage, $frame, $x, $y, 0, 0, $my_collage_width, $my_collage_height);
imagecopy($my_collage, $frame, $x, $y, 0, 0, $width, $height);
}

$new_width = imagesx($images_rotated[0]);
$new_height = imagesy($images_rotated[0]);

$height_offset = ($my_collage_height / 2 - $new_height) / 2;
$width_offset = ($my_collage_width - $new_width * 4) / 5;
$height_offset = ($height / 2 - $new_height) / 2;
$width_offset = ($width - $new_width * 4) / 5;

$positions_top = [
[$width_offset, $height_offset],
Expand All @@ -116,13 +126,10 @@ function createCollage($srcImagePaths, $destImagePath, $takeFrame, $takeFrameAlw
imagecopy($my_collage, $images_rotated[$i], $position_bottom[0], $position_bottom[1], 0, 0, $new_width, $new_height);
}

imagescale($my_collage, $width_before, $height_before);

imagejpeg($my_collage, $destImagePath);
imagedestroy($my_collage);
imagescale($my_collage, $width, $height);
break;
case '2x4BI':
$degrees = 90;
$rotate_after_creation = true;
$widthNew = 321;
$heightNew = 482;
$PositionsX = [63, 423, 785, 1146]; //X offset in Pixel
Expand Down Expand Up @@ -150,7 +157,6 @@ function createCollage($srcImagePaths, $destImagePath, $takeFrame, $takeFrameAlw
}

$tempSubRotated = imagerotate($tempSubImage, $degrees, 0); // Rotate image
list($width, $height) = getimagesize($srcImagePaths[0]);
imagecopyresized($my_collage, $tempSubRotated, $dX, $dY, 0, 0, $widthNew, $heightNew, $height, $width); // copy image to background
imagedestroy($tempSubRotated); // Destroy temporary images
imagedestroy($tempSubImage); // Destroy temporary images
Expand All @@ -163,16 +169,21 @@ function createCollage($srcImagePaths, $destImagePath, $takeFrame, $takeFrameAlw
$y = imagesy($my_collage) / 2 - imagesy($frame) / 2;
imagecopy($my_collage, $frame, $x, $y, 0, 0, $bg_width, $bg_height);
}
imagejpeg($my_collage, $destImagePath); // Transfer image to destImagePath with returns the image to core
imagedestroy($my_collage); // Destroy the created collage in memory
break;
default:
list($width, $height) = getimagesize($srcImagePaths[0]);

$my_collage = imagecreatetruecolor($width, $height);
imagejpeg($my_collage, $destImagePath); // Transfer image to destImagePath with returns the image to core
imagedestroy($my_collage); // Destroy the created collage in memory
break;
}

imagejpeg($my_collage, $destImagePath); // Transfer image to destImagePath with returns the image to core
imagedestroy($my_collage); // Destroy the created collage in memory

// Rotate image if needed
if ($rotate_after_creation) {
$tempRotatedImage = imagecreatefromjpeg($destImagePath);
$resultRotated = imagerotate($tempRotatedImage, -90, 0);
imagejpeg($resultRotated, $destImagePath);
imagedestroy($tempRotatedImage);
}
return true;
}

0 comments on commit 2257771

Please sign in to comment.