Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #117 #478

Merged
merged 1 commit into from
Apr 17, 2020
Merged
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
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