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

Server support for full HiDPI #92

Merged
merged 2 commits into from
Feb 28, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ public function gen_thumbs($return)
{

$return['thumbs'] = array();
$return['thumbs2x'] = array();
$return['types'] = array();

$alb = $this->get_all_subalbums();
$alb[] = $this->id;

$thumbs_types = Photo::select('thumbUrl', 'type')
$thumbs_types = Photo::select('thumbUrl', 'thumb2x', 'type')
->whereIn('album_id', $alb)
->orderBy('star', 'DESC')
->orderBy(Configs::get_value('sortingPhotos_col'), Configs::get_value('sortingPhotos_order'))
Expand All @@ -94,6 +95,14 @@ public function gen_thumbs($return)
$k = 0;
foreach ($thumbs_types as $thumb_types) {
$return['thumbs'][$k] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumb_types->thumbUrl;
if ($thumb_types->thumb2x == '1') {
$thumbUrl2x = explode(".", $thumb_types->thumbUrl);
$thumbUrl2x = $thumbUrl2x[0].'@2x.'.$thumbUrl2x[1];
$return['thumbs2x'][$k] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumbUrl2x;
}
else {
$return['thumbs2x'][$k] = '';
}
$return['types'][$k] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumb_types->type;
$k++;
}
Expand Down
3 changes: 3 additions & 0 deletions app/Configs.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ class Configs extends Model
'sortingPhotos_col',
'sortingPhotos_order',
'default_license',
'thumb_2x',
'small_max_width',
'small_max_height',
'small_2x',
'medium_max_width',
'medium_max_height',
'medium_2x',
];


Expand Down
7 changes: 4 additions & 3 deletions app/Console/Commands/medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function handle()
$timeout = $this->argument('tm');
set_time_limit($timeout);

$photos = Photo::where('medium', '=', 0)->limit($argument)->get();
$photos = Photo::where('medium', '=', '')->limit($argument)->get();
if (count($photos) == 0) {
$this->line('No pictures requires medium.');
return false;
Expand All @@ -63,9 +63,10 @@ public function handle()
if ($this->photoFunctions->createMedium(
$photo,
intval(Configs::get_value('medium_max_width')),
intval(Configs::get_value('medium_max_height')))
intval(Configs::get_value('medium_max_height')),
$resWidth, $resHeight, false, 'MEDIUM')
) {
$photo->medium = 1;
$photo->medium = $resWidth . 'x' . $resHeight;
$photo->save();
$this->line('medium for '.$photo->title.' created.');
} else {
Expand Down
7 changes: 4 additions & 3 deletions app/Console/Commands/small.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function handle()
$timeout = $this->argument('tm');
set_time_limit($timeout);

$photos = Photo::where('small', '=', 0)->limit($argument)->get();
$photos = Photo::where('small', '=', '')->limit($argument)->get();
if (count($photos) == 0) {
$this->line('No pictures requires small.');
return false;
Expand All @@ -63,9 +63,10 @@ public function handle()
if ($this->photoFunctions->createMedium(
$photo,
intval(Configs::get_value('small_max_width')),
intval(Configs::get_value('small_max_height')), 'SMALL')
intval(Configs::get_value('small_max_height')),
$resWidth, $resHeight, false, 'SMALL')
) {
$photo->small = 1;
$photo->small = $resWidth . 'x' . $resHeight;
$photo->save();
$this->line('small for '.$photo->title.' created.');
}
Expand Down
17 changes: 13 additions & 4 deletions app/Http/Controllers/AlbumsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,22 @@ static private function gen_return($return, $photos_sql, $kind)

$return[$kind] = array(
'thumbs' => array(),
'thumbs2x' => array(),
'types' => array(),
'num' => $photos_sql->count()
);

foreach ($photos as $photo) {
if ($i < 3) {
$return[$kind]['thumbs'][$i] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$photo->thumbUrl;
if ($photo->thumb2x == '1') {
$thumbUrl2x = explode(".", $photo->thumbUrl);
$thumbUrl2x = $thumbUrl2x[0].'@2x.'.$thumbUrl2x[1];
$return[$kind]['thumbs2x'][$i] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumbUrl2x;
}
else {
$return[$kind]['thumbs2x'][$i] = '';
}
$return[$kind]['types'][$i] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$photo->type;
$i++;
}
Expand Down Expand Up @@ -156,25 +165,25 @@ private function getSmartAlbums()
/**
* Unsorted
*/
$photos_sql = Photo::select_unsorted(Photo::OwnedBy(Session::get('UserID'))->select('thumbUrl', 'type'))->limit(3);
$photos_sql = Photo::select_unsorted(Photo::OwnedBy(Session::get('UserID'))->select('thumbUrl', 'thumb2x', 'type'))->limit(3);
$return = self::gen_return($return, $photos_sql, 'unsorted');

/**
* Starred
*/
$photos_sql = Photo::select_stars(Photo::OwnedBy(Session::get('UserID'))->select('thumbUrl', 'type'))->limit(3);
$photos_sql = Photo::select_stars(Photo::OwnedBy(Session::get('UserID'))->select('thumbUrl', 'thumb2x', 'type'))->limit(3);
$return = self::gen_return($return, $photos_sql, 'starred');

/**
* Public
*/
$photos_sql = Photo::select_public(Photo::OwnedBy(Session::get('UserID'))->select('thumbUrl', 'type'))->limit(3);
$photos_sql = Photo::select_public(Photo::OwnedBy(Session::get('UserID'))->select('thumbUrl', 'thumb2x', 'type'))->limit(3);
$return = self::gen_return($return, $photos_sql, 'public');

/**
* Recent
*/
$photos_sql = Photo::select_recent(Photo::OwnedBy(Session::get('UserID'))->select('thumbUrl'))->limit(3);
$photos_sql = Photo::select_recent(Photo::OwnedBy(Session::get('UserID'))->select('thumbUrl', 'thumb2x', 'type'))->limit(3);
$return = self::gen_return($return, $photos_sql, 'recent');

// Return SmartAlbums
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function getRandom(Request $request)

$return = array();
$return['thumb'] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$photo->thumbUrl;
if ($photo->medium == '1') {
if ($photo->medium != '') {
$return['url'] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_MEDIUM').$photo->url;
}
else {
Expand Down Expand Up @@ -390,10 +390,13 @@ function duplicate(Request $request)
$duplicate->takestamp = $photo->takestamp;
$duplicate->star = $photo->star;
$duplicate->thumbUrl = $photo->thumbUrl;
$duplicate->thumb2x = $photo->thumb2x;
$duplicate->album_id = $photo->album_id;
$duplicate->checksum = $photo->checksum;
$duplicate->medium = $photo->medium;
$duplicate->medium2x = $photo->medium2x;
$duplicate->small = $photo->small;
$duplicate->small2x = $photo->small2x;
$duplicate->owner_id = $photo->owner_id;
$no_error &= $duplicate->save();
}
Expand Down
7 changes: 6 additions & 1 deletion app/Image/GdHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public function scale(
string $source,
string $destination,
int $newWidth,
int $newHeight
int $newHeight,
int &$resWidth,
int &$resHeight
) : bool {
list($width, $height, $mime) = getimagesize($source);

Expand Down Expand Up @@ -57,6 +59,9 @@ public function scale(
imagedestroy($image);
imagedestroy($sourceImg);

$resWidth = $newWidth;
$resHeight = $newHeight;

return true;
}

Expand Down
4 changes: 3 additions & 1 deletion app/Image/ImageHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function scale(
string $source,
string $destination,
int $newWidth,
int $newHeight
int $newHeight,
int &$resWidth,
int &$resHeight
) : bool ;

/**
Expand Down
6 changes: 5 additions & 1 deletion app/Image/ImagickHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function scale(
string $source,
string $destination,
int $newWidth,
int $newHeight
int $newHeight,
int &$resWidth,
int &$resHeight
): bool
{
try {
Expand All @@ -45,6 +47,8 @@ public function scale(
$image->scaleImage($newWidth, $newHeight, ($newWidth != 0));
$image->writeImage($destination);
Logs::notice(__METHOD__, __LINE__, 'Saving thumb to '.$destination);
$resWidth = $image->getImageWidth();
$resHeight = $image->getImageHeight();
$image->clear();
$image->destroy();
}
Expand Down
90 changes: 64 additions & 26 deletions app/ModelFunctions/PhotoFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function createVideoThumb(Photo $photo, string $path) : string
200,
200
);
$photo->thumb2x = 0;

Logs::notice(__METHOD__, __LINE__, 'Video thumb saved to '.Config::get('defines.dirs.LYCHEE_UPLOADS_THUMB').$thumbUrl);

Expand All @@ -122,13 +123,20 @@ public function createThumb(Photo $photo)
200
);

// Retina thumbs
$this->imageHandler->crop(
Config::get('defines.dirs.LYCHEE_UPLOADS_BIG').$photo->url,
Config::get('defines.dirs.LYCHEE_UPLOADS_THUMB').$photoName[0].'@2x.jpeg',
400,
400
);
if (Configs::get_value('thumb_2x') === '1' &&
$photo->width >= 400 && $photo->height >= 400) {
// Retina thumbs
$this->imageHandler->crop(
Config::get('defines.dirs.LYCHEE_UPLOADS_BIG').$photo->url,
Config::get('defines.dirs.LYCHEE_UPLOADS_THUMB').$photoName[0].'@2x.jpeg',
400,
400
);
$photo->thumb2x = 1;
}
else {
$photo->thumb2x = 0;
}

return true;
}
Expand All @@ -141,10 +149,13 @@ public function createThumb(Photo $photo)
* @param Photo $photo
* @param int $newWidth
* @param int $newHeight
* @param $resWidth
* @param $resHeight
* @param bool $x2
* @param string $kind
* @return boolean Returns true when successful.
*/
public function createMedium(Photo $photo, $newWidth = 1920, $newHeight = 1080, $kind = 'MEDIUM')
public function createMedium(Photo $photo, $newWidth, $newHeight, &$resWidth, &$resHeight, $x2 = false, $kind = 'MEDIUM')
{
$filename = $photo->url;
$width = $photo->width;
Expand All @@ -161,15 +172,23 @@ public function createMedium(Photo $photo, $newWidth = 1920, $newHeight = 1080,

}

if ($x2) {
$newWidth *= 2;
$newHeight *= 2;

$photoName = explode('.', $filename);
$filename = $photoName[0].'@2x.'.$photoName[1];
}

// Is photo big enough?
if ($width <= $newWidth && $height <= $newHeight) {
if (($width <= $newWidth || $newWidth == 0) && $height <= $newHeight) {
Logs::notice(__METHOD__, __LINE__, 'No resize (image is too small)!');
return false;
}

$newUrl = Config::get('defines.dirs.LYCHEE_UPLOADS_'.$kind).$filename;

return $this->imageHandler->scale($url, $newUrl, $newWidth, $newHeight);
return $this->imageHandler->scale($url, $newUrl, $newWidth, $newHeight, $resWidth, $resHeight);

}

Expand Down Expand Up @@ -280,9 +299,12 @@ public function add(array $file, $albumID_in = 0)
if ($exists !== false) {
$photo_name = $exists->url;
$path = Config::get('defines.dirs.LYCHEE_UPLOADS_BIG').$exists->url;
$path_thumb = $exists->thumbUrl;
$medium = $exists->medium;
$small = $exists->small;
$photo->thumbUrl = $exists->thumbUrl;
$photo->thumb2x = $exists->thumb2x;
$photo->medium = $exists->medium;
$photo->medium2x = $exists->medium2x;
$photo->small = $exists->small;
$photo->small2x = $exists->small2x;
$exists = true;
}

Expand Down Expand Up @@ -349,8 +371,6 @@ public function add(array $file, $albumID_in = 0)
$photo->star = $star;
$photo->checksum = $checksum;
$photo->album_id = $albumID;
$photo->medium = 0;
$photo->small = 0;

if ($exists === false) {

Expand Down Expand Up @@ -387,28 +407,46 @@ public function add(array $file, $albumID_in = 0)
}

Logs::notice(__METHOD__, __LINE__, $path_thumb);
$photo->thumbUrl = $path_thumb;

$resWidth = 0;
$resHeight = 0;

// Create Medium
if ($this->createMedium($photo, intval(Configs::get_value('medium_max_width')), intval(Configs::get_value('medium_max_height')))) {
$medium = 1;
if ($this->createMedium($photo, intval(Configs::get_value('medium_max_width')), intval(Configs::get_value('medium_max_height')), $resWidth, $resHeight)) {
$photo->medium = $resWidth . 'x' . $resHeight;

if (Configs::get_value('medium_2x') === '1' &&
$this->createMedium($photo, intval(Configs::get_value('medium_max_width')), intval(Configs::get_value('medium_max_height')), $resWidth, $resHeight, true)) {
$photo->medium2x = $resWidth . 'x' . $resHeight;
}
else {
$photo->medium2x = '';
}
}
else {
$medium = 0;
$photo->medium = '';
$photo->medium2x = '';
}

// Create Small
if ($this->createMedium($photo, intval(Configs::get_value('small_max_width')), intval(Configs::get_value('small_max_height')), 'SMALL')) {
$small = 1;
if ($this->createMedium($photo, intval(Configs::get_value('small_max_width')), intval(Configs::get_value('small_max_height')), $resWidth, $resHeight, false, 'SMALL')) {
$photo->small = $resWidth . 'x' . $resHeight;

if (Configs::get_value('small_2x') === '1' &&
$this->createMedium($photo, intval(Configs::get_value('small_max_width')), intval(Configs::get_value('small_max_height')), $resWidth, $resHeight, true, 'SMALL')) {
$photo->small2x = $resWidth . 'x' . $resHeight;
}
else {
$photo->small2x = '';
}
}
else {
$small = 0;
$photo->small = '';
$photo->small2x = '';
}
}

$photo->thumbUrl = $path_thumb;
$photo->medium = $medium;
$photo->small = $small;

return $this->save($photo, $albumID);
}

Expand Down Expand Up @@ -546,4 +584,4 @@ public function getValidExtensions(): array
{
return $this->validExtensions;
}
}
}