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

Commit

Permalink
feature: add option to add all images from collage to gallery
Browse files Browse the repository at this point in the history
Change-Id: Ia21593f8948ce44a1964afdaacc2f347e9dde2de
  • Loading branch information
andi34 committed Sep 27, 2021
1 parent 224ad98 commit e032bab
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 157 deletions.
218 changes: 116 additions & 102 deletions api/applyEffects.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@

$file = $_POST['file'];

$filename_photo = $config['foldersAbs']['images'] . DIRECTORY_SEPARATOR . $file;
$filename_keying = $config['foldersAbs']['keying'] . DIRECTORY_SEPARATOR . $file;
$filename_tmp = $config['foldersAbs']['tmp'] . DIRECTORY_SEPARATOR . $file;
$filename_thumb = $config['foldersAbs']['thumbs'] . DIRECTORY_SEPARATOR . $file;
$picture_frame = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $config['picture']['frame']);
$picture_permissions = $config['picture']['permissions'];
$thumb_size = substr($config['picture']['thumb_size'], 0, -2);
$chroma_size = substr($config['keying']['size'], 0, -2);
Expand Down Expand Up @@ -88,11 +83,6 @@
}
} else {
// Check picture configuration
if (!file_exists($filename_tmp)) {
$errormsg = 'File ' . $filename_tmp . ' does not exist';
logErrorAndDie($errormsg);
}

if ($config['picture']['take_frame']) {
if (is_dir($picture_frame)) {
$errormsg = 'Frame not set! ' . $picture_frame . ' is a path but needs to be a png!';
Expand All @@ -118,136 +108,160 @@
}
}

$srcImages = [];
$srcImages[] = $file;

$filename_tmp = $config['foldersAbs']['tmp'] . DIRECTORY_SEPARATOR . $file;

// Process Collage
if ($_POST['style'] === 'collage') {
$collageBasename = substr($filename_tmp, 0, -4);
$singleImageBase = substr($file, 0, -4);

$collageSrcImagePaths = [];

for ($i = 0; $i < $config['collage']['limit']; $i++) {
$collageSrcImagePaths[] = $collageBasename . '-' . $i . '.jpg';
if ($config['collage']['keep_single_images']) {
$srcImages[] = $singleImageBase . '-' . $i . '.jpg';
}
}

if (!createCollage($collageSrcImagePaths, $filename_tmp, $image_filter)) {
$errormsg = 'Could not create collage';
logErrorAndDie($errormsg);
}

if (!$config['picture']['keep_original']) {
foreach ($collageSrcImagePaths as $tmp) {
unlink($tmp);
}
}
}

$imageResource = imagecreatefromjpeg($filename_tmp);
foreach ($srcImages as $image) {
$filename_photo = $config['foldersAbs']['images'] . DIRECTORY_SEPARATOR . $image;
$filename_keying = $config['foldersAbs']['keying'] . DIRECTORY_SEPARATOR . $image;
$filename_tmp = $config['foldersAbs']['tmp'] . DIRECTORY_SEPARATOR . $image;
$filename_thumb = $config['foldersAbs']['thumbs'] . DIRECTORY_SEPARATOR . $image;

if ($_POST['style'] !== 'collage') {
// Only jpg/jpeg are supported
if (!$imageResource) {
$errormsg = 'Could not read jpeg file. Are you taking raws?';
if (!file_exists($filename_tmp)) {
$errormsg = 'File ' . $filename_tmp . ' does not exist';
logErrorAndDie($errormsg);
}

if ($config['picture']['flip'] !== 'off') {
if ($config['picture']['flip'] === 'horizontal') {
imageflip($imageResource, IMG_FLIP_HORIZONTAL);
} elseif ($config['picture']['flip'] === 'vertical') {
imageflip($imageResource, IMG_FLIP_VERTICAL);
} elseif ($config['picture']['flip'] === 'both') {
imageflip($imageResource, IMG_FLIP_BOTH);
}
$imageModified = true;
$imageResource = imagecreatefromjpeg($filename_tmp);

if ($_POST['style'] === 'collage' && $file != $image) {
$editSingleCollage = true;
$picture_frame = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $config['collage']['frame']);
} else {
$editSingleCollage = false;
$picture_frame = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $config['picture']['frame']);
}

// apply filter
if ($image_filter) {
applyFilter($image_filter, $imageResource);
$imageModified = true;
if ($_POST['style'] !== 'collage' || $editSingleCollage) {
// Only jpg/jpeg are supported
if (!$imageResource) {
$errormsg = 'Could not read jpeg file. Are you taking raws?';
logErrorAndDie($errormsg);
}

if ($config['picture']['flip'] !== 'off') {
if ($config['picture']['flip'] === 'horizontal') {
imageflip($imageResource, IMG_FLIP_HORIZONTAL);
} elseif ($config['picture']['flip'] === 'vertical') {
imageflip($imageResource, IMG_FLIP_VERTICAL);
} elseif ($config['picture']['flip'] === 'both') {
imageflip($imageResource, IMG_FLIP_BOTH);
}
$imageModified = true;
}

// apply filter
if ($image_filter) {
applyFilter($image_filter, $imageResource);
$imageModified = true;
}

if ($config['picture']['rotation'] !== '0') {
$rotatedImg = imagerotate($imageResource, $config['picture']['rotation'], 0);
$imageResource = $rotatedImg;
$imageModified = true;
}

if ($config['picture']['polaroid_effect'] && $_POST['style'] !== 'collage') {
$polaroid_rotation = $config['picture']['polaroid_rotation'];
$imageResource = effectPolaroid($imageResource, $polaroid_rotation, 200, 200, 200);
$imageModified = true;
}

if ($config['picture']['take_frame'] || ($editSingleCollage && $config['collage']['take_frame'] === 'always')) {
$frame = imagecreatefrompng($picture_frame);
$frame = resizePngImage($frame, imagesx($imageResource), imagesy($imageResource));
$x = imagesx($imageResource) / 2 - imagesx($frame) / 2;
$y = imagesy($imageResource) / 2 - imagesy($frame) / 2;
imagecopy($imageResource, $frame, $x, $y, 0, 0, imagesx($frame), imagesy($frame));
$imageModified = true;
}
}

if ($config['picture']['rotation'] !== '0') {
$rotatedImg = imagerotate($imageResource, $config['picture']['rotation'], 0);
$imageResource = $rotatedImg;
$imageModified = true;
if ($config['keying']['enabled'] || $_POST['style'] === 'chroma') {
$chromaCopyResource = resizeImage($imageResource, $chroma_size, $chroma_size);
imagejpeg($chromaCopyResource, $filename_keying, $config['jpeg_quality']['chroma']);
imagedestroy($chromaCopyResource);
}

if ($config['picture']['polaroid_effect'] && $_POST['style'] !== 'collage') {
$polaroid_rotation = $config['picture']['polaroid_rotation'];
$imageResource = effectPolaroid($imageResource, $polaroid_rotation, 200, 200, 200);
if ($config['textonpicture']['enabled'] && $_POST['style'] !== 'collage') {
imagejpeg($imageResource, $filename_photo, $config['jpeg_quality']['image']);
imagedestroy($imageResource);
ApplyText($filename_photo, $fontsize, $fontrot, $fontlocx, $fontlocy, $fontcolor, $fontpath, $line1text, $line2text, $line3text, $linespacing);
$imageModified = true;
$imageResource = imagecreatefromjpeg($filename_photo);
}

if ($config['picture']['take_frame']) {
$frame = imagecreatefrompng($picture_frame);
$frame = resizePngImage($frame, imagesx($imageResource), imagesy($imageResource));
$x = imagesx($imageResource) / 2 - imagesx($frame) / 2;
$y = imagesy($imageResource) / 2 - imagesy($frame) / 2;
imagecopy($imageResource, $frame, $x, $y, 0, 0, imagesx($frame), imagesy($frame));
$imageModified = true;
// image scale, create thumbnail
$thumbResource = resizeImage($imageResource, $thumb_size, $thumb_size);

imagejpeg($thumbResource, $filename_thumb, $config['jpeg_quality']['thumb']);
imagedestroy($thumbResource);

if ($imageModified || ($config['jpeg_quality']['image'] >= 0 && $config['jpeg_quality']['image'] < 100)) {
imagejpeg($imageResource, $filename_photo, $config['jpeg_quality']['image']);
// preserve jpeg meta data
if ($config['picture']['preserve_exif_data'] && $config['exiftool']['cmd']) {
$cmd = sprintf($config['exiftool']['cmd'], $filename_tmp, $filename_photo);
$cmd .= ' 2>&1'; //Redirect stderr to stdout, otherwise error messages get lost.

exec($cmd, $output, $returnValue);

if ($returnValue) {
$ErrorData = [
'error' => 'exiftool returned with an error code',
'cmd' => $cmd,
'returnValue' => $returnValue,
'output' => $output,
];
$ErrorString = json_encode($ErrorData);
logError($ErrorData);
die($ErrorString);
}
}
} else {
copy($filename_tmp, $filename_photo);
}
}

if ($config['keying']['enabled'] || $_POST['style'] === 'chroma') {
$chromaCopyResource = resizeImage($imageResource, $chroma_size, $chroma_size);
imagejpeg($chromaCopyResource, $filename_keying, $config['jpeg_quality']['chroma']);
imagedestroy($chromaCopyResource);
}
if (!$config['picture']['keep_original']) {
unlink($filename_tmp);
}

if ($config['textonpicture']['enabled'] && $_POST['style'] !== 'collage') {
imagejpeg($imageResource, $filename_photo, $config['jpeg_quality']['image']);
imagedestroy($imageResource);
ApplyText($filename_photo, $fontsize, $fontrot, $fontlocx, $fontlocy, $fontcolor, $fontpath, $line1text, $line2text, $line3text, $linespacing);
$imageModified = true;
$imageResource = imagecreatefromjpeg($filename_photo);
}

// image scale, create thumbnail
$thumbResource = resizeImage($imageResource, $thumb_size, $thumb_size);

imagejpeg($thumbResource, $filename_thumb, $config['jpeg_quality']['thumb']);
imagedestroy($thumbResource);

if ($imageModified || ($config['jpeg_quality']['image'] >= 0 && $config['jpeg_quality']['image'] < 100)) {
imagejpeg($imageResource, $filename_photo, $config['jpeg_quality']['image']);
// preserve jpeg meta data
if ($config['picture']['preserve_exif_data'] && $config['exiftool']['cmd']) {
$cmd = sprintf($config['exiftool']['cmd'], $filename_tmp, $filename_photo);
$cmd .= ' 2>&1'; //Redirect stderr to stdout, otherwise error messages get lost.

exec($cmd, $output, $returnValue);

if ($returnValue) {
$ErrorData = [
'error' => 'exiftool returned with an error code',
'cmd' => $cmd,
'returnValue' => $returnValue,
'output' => $output,
];
$ErrorString = json_encode($ErrorData);
logError($ErrorData);
die($ErrorString);
// insert into database
if ($config['database']['enabled']) {
if ($_POST['style'] !== 'chroma' || ($_POST['style'] === 'chroma' && $config['live_keying']['show_all'] === true)) {
appendImageToDB($image);
}
}
} else {
copy($filename_tmp, $filename_photo);
}

if (!$config['picture']['keep_original']) {
unlink($filename_tmp);
}

imagedestroy($imageResource);

// insert into database
if ($config['database']['enabled']) {
if ($_POST['style'] !== 'chroma' || ($_POST['style'] === 'chroma' && $config['live_keying']['show_all'] === true)) {
appendImageToDB($file);
}
// Change permissions
chmod($filename_photo, octdec($picture_permissions));
}

// Change permissions
chmod($filename_photo, octdec($picture_permissions));

if ($_POST['style'] === 'chroma' && $config['live_keying']['show_all'] === false) {
unlink($filename_photo);
unlink($filename_thumb);
Expand Down
1 change: 1 addition & 0 deletions config/config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
$config['collage']['continuous_time'] = '5';
// possible layout values: '2x2', '2x2-2', '2x4', '2x4-2', '1+3', '1+3-2', '3+1', '1+2'
$config['collage']['layout'] = '2x2-2';
$config['collage']['keep_single_images'] = false;
// specify key id (e.g. 13 is the enter key) to use that key to take a collage (collage key)
// use for example https://keycode.info to get the key code
$config['collage']['key'] = null;
Expand Down
Loading

0 comments on commit e032bab

Please sign in to comment.