Skip to content

Commit

Permalink
handle unreadable jpeg while taking a picture
Browse files Browse the repository at this point in the history
also minor performance optimization (related #117)
  • Loading branch information
sualko committed Oct 11, 2019
1 parent 08fe198 commit d36065d
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions api/takePic.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,24 @@ function takePicture($filename)
}
}

function ResizeJpgImage($image, $max_width, $max_height)
function resizeImage($image, $max_width, $max_height)
{
if (!$image) {
return false;
}

$old_width = imagesx($image);
$old_height = imagesy($image);

$scale = min($max_width/$old_width, $max_height/$old_height);

$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);

$new = imagecreatetruecolor($new_width, $new_height);

imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);

return $new;
}

Expand Down Expand Up @@ -319,11 +328,11 @@ function applyFilter($imgfilter, $source, $destination)
}

// apply filter
if ($use_filter == true) {
if ($use_filter) {
applyFilter($imgfilter, $filename_orig, $filename_photo);
}

if ($config['polaroid_effect'] == true) {
if ($config['polaroid_effect']) {
if ($_POST['style'] === 'photo') {
$tmp = imagecreatefromjpeg($filename_orig);
imagejpeg($tmp, $filename_photo);
Expand All @@ -334,27 +343,35 @@ function applyFilter($imgfilter, $source, $destination)
imagedestroy($polaroid);
}

$imageResource = imagecreatefromjpeg($filename_photo);

if (!$imageResource) {
die(json_encode([
'error' => 'Could not read jpeg file. Are you taking raws?',
]));
}

if ($config['chroma_keying'] == true) {
$source = imagecreatefromjpeg($filename_photo);
$source = ResizeJpgImage($source, 1500, 1000);
imagejpeg($source, $filename_keying, 100);
$source = resizeImage($imageResource, 1500, 1000);

imagejpeg($source, $filename_keying, 70);
imagedestroy($source);
}

// image scale, create thumbnail
list($width, $height) = getimagesize($filename_photo);
$newwidth = 500;
$newheight = $height * (1 / $width * 500);
$source = imagecreatefromjpeg($filename_photo);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, $filename_thumb);
$thumb = resizeImage($imageResource, 500, 500);

imagejpeg($thumb, $filename_thumb, 60);

imagedestroy($source);
imagedestroy($thumb);
imagedestroy($imageResource);

// insert into database
$images[] = $file;
file_put_contents(__DIR__ . '/../data/db.txt', json_encode($images));

// send imagename to frontend
echo json_encode(array('success' => 'image', 'img' => $file));
echo json_encode([
'success' => 'image',
'img' => $file,
]);

0 comments on commit d36065d

Please sign in to comment.