Skip to content

Commit

Permalink
Add the possibility to rotate pictures (#616)
Browse files Browse the repository at this point in the history
* Adding rotation to server side
* Added width and height swap
* Added config file migrate
* sync Lychee-front
* add coverage
  • Loading branch information
gardiol authored Jun 10, 2020
1 parent c51722a commit c75811e
Show file tree
Hide file tree
Showing 23 changed files with 441 additions and 16 deletions.
126 changes: 126 additions & 0 deletions app/Http/Controllers/PhotoEditorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/** @noinspection PhpUndefinedClassInspection */

namespace App\Http\Controllers;

use App\Configs;
use App\Logs;
use App\Photo;
use Illuminate\Http\Request;
use Storage;

class PhotoEditorController extends Controller
{
public function __construct()
{
}

/**
* Given a photoID and a direction (+1: 90° clockwise, -1: 90° counterclockwise) rotate an image.
*
* @param Request $request
*
* @return tring
*/
public function rotate(Request $request)
{
// Safety check...
if (!Configs::get_value('editor_enabled', '0')) {
return 'false';
}

if (!Configs::hasImagick()) {
// @codeCoverageIgnoreStart
return 'false';
// @codeCoverageIgnoreEnd
}

$request->validate([
'photoID' => 'string|required',
'direction' => 'integer|required',
]);

$photo = Photo::find($request['photoID']);
$direction = $request['direction'];

// Photo not found?
if ($photo == null) {
Logs::error(__METHOD__, __LINE__, 'Could not find specified photo');

return 'false';
}

// direction is valid?
if (($direction != 1) && ($direction != -1)) {
Logs::error(__METHOD__, __LINE__, 'Direction must be 1 or -1');

return 'false';
}

// Abort on symlinks to avoid messing with originals linked
if (is_link(Storage::path('big/') . $photo->url)) {
// @codeCoverageIgnoreStart
Logs::error(__METHOD__, __LINE__, 'Synlinked images cannot be rotated');

return 'false';
// @codeCoverageIgnoreEnd
}

// We must rotate all the various formats
$img_types = ['big', 'medium', 'medium2x', 'small', 'small2x', 'thumb', 'thumb2x'];
$save_photo = false;
foreach ($img_types as $img_type) {
// This will be FALSE if not 2x, or the position of the '2' char otherwise
$image_2x = strpos($img_type, '2');

// Build path to stored image
if (substr($img_type, 0, 5) !== 'thumb') {
// Rotate image sizes
$filename = $photo->url;
if (!is_null($photo->{$img_type})) {
$x_pos = strpos($photo->{$img_type}, 'x');
$old_w = substr($photo->{$img_type}, 0, $x_pos);
$old_h = substr($photo->{$img_type}, $x_pos + 1);
$photo->{$img_type} = $old_h . 'x' . $old_w;
$save_photo = true;
}
} else {
$filename = $photo->thumbUrl;
}
if ($image_2x !== false) {
$img_type = substr($img_type, 0, $image_2x);
$filename = preg_replace('/^(.*)\.(.*)$/', '\1@2x.\2', $filename);
}
$uploadFolder = Storage::path($img_type . '/');
$img_path = $uploadFolder . $photo->url;

// Rotate the image
$img_path = $uploadFolder . $filename;
if (file_exists($img_path)) {
$image = new \Imagick();
$image->readImage($img_path);

if ($direction == 1) {
$image->rotateImage(new \ImagickPixel(), 90);
} else {
$image->rotateImage(new \ImagickPixel(), -90);
}
$save_photo = true;
$image->writeImage();
$image->clear();
$image->destroy();
}
}
if ($save_photo) {
// rotate image width and height and save to the database
$old_w = $photo->width;
$old_h = $photo->height;
$photo->width = $old_h;
$photo->height = $old_w;
$photo->save();
}

return 'true';
}
}
3 changes: 3 additions & 0 deletions app/Locale/ChineseSimplified.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Square thumb HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Lychee Photo View:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
Expand Down
3 changes: 3 additions & 0 deletions app/Locale/Czech.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Čtvercový náhled HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Zobrazení foto Lychee:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
Expand Down
3 changes: 3 additions & 0 deletions app/Locale/Dutch.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Square thumb HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Lychee Photo View:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
Expand Down
5 changes: 4 additions & 1 deletion app/Locale/English.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,11 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Square thumb HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Lychee Photo View:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
}
}
}
3 changes: 3 additions & 0 deletions app/Locale/French.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Square thumb HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Lychee Photo View:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
Expand Down
3 changes: 3 additions & 0 deletions app/Locale/German.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Quadratische Miniaturansicht HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video des Live-Photos',
'PHOTO_VIEW' => 'Lychee Foto Ansicht:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
Expand Down
3 changes: 3 additions & 0 deletions app/Locale/Greek.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Τετράγωνη Μικρογραφία HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Lychee Προβολή Φωτογραφιών:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
Expand Down
3 changes: 3 additions & 0 deletions app/Locale/Italian.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Square thumb HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Lychee Photo View:',

'PHOTO_EDIT_ROTATECWISE' => 'Ruota in senso orario',
'PHOTO_EDIT_ROTATECCWISE' => 'Ruota in senso anti-orario',
];

return $locale;
Expand Down
5 changes: 4 additions & 1 deletion app/Locale/Russian.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,11 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Square thumb HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Lychee Photo View:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
}
}
}
3 changes: 3 additions & 0 deletions app/Locale/Slovak.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Štvorcový náhľad HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Zobrazenie foto Lychee:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
Expand Down
3 changes: 3 additions & 0 deletions app/Locale/Spanish.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Cuadrado de Miniatura HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Vista de Foto de Lychee',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
Expand Down
3 changes: 3 additions & 0 deletions app/Locale/Swedish.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ public static function get_locale()
'PHOTO_THUMB_HIDPI' => 'Square thumb HiDPI',
'PHOTO_LIVE_VIDEO' => 'Video part of live-photo',
'PHOTO_VIEW' => 'Lychee Photo View:',

'PHOTO_EDIT_ROTATECWISE' => 'Rotate clockwise',
'PHOTO_EDIT_ROTATECCWISE' => 'Rotate counter-clockwise',
];

return $locale;
Expand Down
6 changes: 3 additions & 3 deletions database/migrations/2020_05_12_114228_rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ public function up()
[
'key' => 'rss_enable',
'value' => '0',
'confidentiality' => 0,
'confidentiality' => '0',
'cat' => 'Mod RSS',
'type_range' => BOOL,
],
[
'key' => 'rss_recent_days',
'value' => '7',
'confidentiality' => 0,
'confidentiality' => '0',
'cat' => 'Mod RSS',
'type_range' => INT,
],
[
'key' => 'rss_max_items',
'value' => '100',
'confidentiality' => 0,
'confidentiality' => '0',
'cat' => 'Mod RSS',
'type_range' => INT,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function up()
DB::table('configs')->insert([
[
'key' => 'prefer_available_xmp_metadata',
'value' => 0,
'confidentiality' => 2,
'value' => '0',
'confidentiality' => '2',
'cat' => 'Image Processing',
'type_range' => BOOL,
],
Expand Down
36 changes: 36 additions & 0 deletions database/migrations/2020_06_04_104605_config_editor_enabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use App\Configs;
use Illuminate\Database\Migrations\Migration;

class ConfigEditorEnabled extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
defined('BOOL') or define('BOOL', '0|1');
DB::table('configs')->insert([
[
'key' => 'editor_enabled',
'value' => '1',
'confidentiality' => '2',
'cat' => 'Image Processing',
'type_range' => BOOL,
],
]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Configs::where('key', '=', 'editor_enabled')->delete();
}
}
2 changes: 1 addition & 1 deletion public/Lychee-front
Loading

0 comments on commit c75811e

Please sign in to comment.