Skip to content

Commit

Permalink
fix incorrect altitude by erasing the value and notifying the user in…
Browse files Browse the repository at this point in the history
… the Logs (#478)
  • Loading branch information
ildyria authored Apr 17, 2020
1 parent 77cde36 commit 470bea4
Showing 1 changed file with 86 additions and 61 deletions.
147 changes: 86 additions & 61 deletions app/ModelFunctions/PhotoFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,66 +89,6 @@ public function __construct(Extractor $metadataExtractor, ImageHandlerInterface
$this->sessionFunctions = $sessionFunctions;
}

/**
* @param Photo $photo
*
* @return string Path of the video frame
*/
public function extractVideoFrame(Photo $photo): string
{
if ($photo->aperture === '') {
return '';
}

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open(Storage::path('big/' . $photo->url));
$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($photo->aperture / 2));

$tmp = tempnam(sys_get_temp_dir(), 'lychee');
Logs::notice(__METHOD__, __LINE__, 'Saving frame to ' . $tmp);
$frame->save($tmp);

return $tmp;
}

/**
* Create thumbnail for a picture.
*
* @param Photo $photo
* @param string Path of the video frame
*
* @return bool returns true when successful
*/
public function createThumb(Photo $photo, string $frame_tmp = '')
{
Logs::notice(__METHOD__, __LINE__, 'Photo URL is ' . $photo->url);

$src = ($frame_tmp === '') ? Storage::path('big/' . $photo->url) : $frame_tmp;
$photoName = explode('.', $photo->url);
$this->imageHandler->crop(
$src,
Storage::path('thumb/' . $photoName[0] . '.jpeg'),
200,
200
);

if (Configs::get_value('thumb_2x') === '1' &&
$photo->width >= 400 && $photo->height >= 400) {
// Retina thumbs
$this->imageHandler->crop(
$src,
Storage::path('thumb/' . $photoName[0] . '@2x.jpeg'),
400,
400
);
$photo->thumb2x = 1;
} else {
$photo->thumb2x = 0;
}

return true;
}

/**
* Returns 'photo' if it is a photo
* Returns 'video' if it is a video
Expand Down Expand Up @@ -284,7 +224,6 @@ public function add(array $file, $albumID_in = 0, $delete_imported = false, $for
$photo->checksum = $checksum;
$exists = $photo->isDuplicate($checksum);

// double check that
if ($exists !== false) {
$photo_name = $exists->url;
$path = Storage::path($path_prefix . $exists->url);
Expand Down Expand Up @@ -384,6 +323,8 @@ public function add(array $file, $albumID_in = 0, $delete_imported = false, $for
$photo->public = $public;
$photo->star = $star;

$photo = $this->altitude_fix($photo);

$GoogleMicroVideoOffset = $info['MicroVideoOffset'];

if ($albumID !== null) {
Expand Down Expand Up @@ -524,6 +465,66 @@ public function createSmallerImages(Photo $photo, string $frame_tmp = '')
}
}

/**
* @param Photo $photo
*
* @return string Path of the video frame
*/
public function extractVideoFrame(Photo $photo): string
{
if ($photo->aperture === '') {
return '';
}

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open(Storage::path('big/' . $photo->url));
$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($photo->aperture / 2));

$tmp = tempnam(sys_get_temp_dir(), 'lychee');
Logs::notice(__METHOD__, __LINE__, 'Saving frame to ' . $tmp);
$frame->save($tmp);

return $tmp;
}

/**
* Create thumbnail for a picture.
*
* @param Photo $photo
* @param string Path of the video frame
*
* @return bool returns true when successful
*/
public function createThumb(Photo $photo, string $frame_tmp = '')
{
Logs::notice(__METHOD__, __LINE__, 'Photo URL is ' . $photo->url);

$src = ($frame_tmp === '') ? Storage::path('big/' . $photo->url) : $frame_tmp;
$photoName = explode('.', $photo->url);
$this->imageHandler->crop(
$src,
Storage::path('thumb/' . $photoName[0] . '.jpeg'),
200,
200
);

if (Configs::get_value('thumb_2x') === '1' &&
$photo->width >= 400 && $photo->height >= 400) {
// Retina thumbs
$this->imageHandler->crop(
$src,
Storage::path('thumb/' . $photoName[0] . '@2x.jpeg'),
400,
400
);
$photo->thumb2x = 1;
} else {
$photo->thumb2x = 0;
}

return true;
}

/**
* Creates smaller copies of Photo.
*
Expand Down Expand Up @@ -717,6 +718,30 @@ public function save(Photo $photo, $albumID)
return $photo->id;
}

/**
* Given the information of a photo, makes sure the altitude is in the correct range.
*
* @param Photo $photo
*
* @return Photo
*/
private function altitude_fix(Photo $photo)
{
$altitude = $photo->altitude;
// max value of dec(10,4) is 999' 999.9999
if ($altitude > 999999.9999) {
// we are out of the bound
// we assume this is a bug due to DJI firmware not updated
// we signal the user and erase the value before adding it the database.
$photo->altitude = null;
Logs::warning(__METHOD__, __LINE__, 'altitude set to 0, previous value was out of bounds:' . $altitude . ' for picture ' . $photo->title);
Logs::warning(__METHOD__, __LINE__, $photo->url);
Logs::warning(__METHOD__, __LINE__, 'Manually parse the file with exiftool and edit the database to set the correct value.');
}

return $photo;
}

/**
* Validates whether $type is a valid image type.
*
Expand Down

0 comments on commit 470bea4

Please sign in to comment.