Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 117 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,141 +108,166 @@
}
}

$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);
}

echo json_encode([
'file' => $file,
'images' => $srcImages,
]);
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