mirror of
https://github.com/nextcloud/server.git
synced 2026-04-15 22:11:17 -04:00
Fix for oc-972, oc-1144 and oc-1191
This commit is contained in:
parent
91da4b05b7
commit
3e53bf4a86
2 changed files with 112 additions and 28 deletions
|
|
@ -25,6 +25,15 @@ class DatabaseManager {
|
|||
}
|
||||
}
|
||||
|
||||
public function setFileData($path, $width, $height) {
|
||||
$stmt = \OCP\DB::prepare('INSERT INTO *PREFIX*pictures_images_cache (uid_owner, path, width, height) VALUES (?, ?, ?, ?)');
|
||||
$stmt->execute(array(\OCP\USER::getUser(), $path, $width, $height));
|
||||
$ret = array('path' => $path, 'width' => $width, 'height' => $height);
|
||||
unset($image);
|
||||
$this->cache[$dir][$path] = $ret;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function getFileData($path) {
|
||||
$gallery_path = \OCP\Config::getSystemValue( 'datadirectory' ).'/'.\OC_User::getUser().'/gallery';
|
||||
$path = $gallery_path.$path;
|
||||
|
|
@ -39,9 +48,7 @@ class DatabaseManager {
|
|||
if (!$image->loadFromFile($path)) {
|
||||
return false;
|
||||
}
|
||||
$stmt = \OCP\DB::prepare('INSERT INTO *PREFIX*pictures_images_cache (uid_owner, path, width, height) VALUES (?, ?, ?, ?)');
|
||||
$stmt->execute(array(\OCP\USER::getUser(), $path, $image->width(), $image->height()));
|
||||
$ret = array('path' => $path, 'width' => $image->width(), 'height' => $image->height());
|
||||
$ret = $this->setFileData($path, $image->width(), $image->height());
|
||||
unset($image);
|
||||
$this->cache[$dir][$path] = $ret;
|
||||
return $ret;
|
||||
|
|
@ -76,7 +83,7 @@ class ThumbnailsManager {
|
|||
|
||||
$image->fixOrientation();
|
||||
|
||||
$ret = $image->preciseResize(floor((150*$image->width())/$image->height()), 150);
|
||||
$ret = $image->preciseResize($this->getThumbnailWidth($image), $this->getThumbnailHeight($image));
|
||||
|
||||
if (!$ret) {
|
||||
\OC_Log::write(self::TAG, 'Couldn\'t resize image', \OC_Log::ERROR);
|
||||
|
|
@ -87,13 +94,28 @@ class ThumbnailsManager {
|
|||
$image->save($gallery_path.'/'.$path);
|
||||
return $image;
|
||||
}
|
||||
|
||||
|
||||
public function getThumbnailWidth($image) {
|
||||
return floor((150*$image->widthTopLeft())/$image->heightTopLeft());
|
||||
}
|
||||
|
||||
public function getThumbnailHeight($image) {
|
||||
return 150;
|
||||
}
|
||||
|
||||
public function getThumbnailInfo($path) {
|
||||
$arr = DatabaseManager::getInstance()->getFileData($path);
|
||||
if (!$arr) {
|
||||
$thubnail = $this->getThumbnail($path);
|
||||
unset($thubnail);
|
||||
$arr = DatabaseManager::getInstance()->getFileData($path);
|
||||
if (!\OC_Filesystem::file_exists($path)) {
|
||||
\OC_Log::write(self::TAG, 'File '.$path.' don\'t exists', \OC_Log::WARN);
|
||||
return false;
|
||||
}
|
||||
$image = new \OC_Image();
|
||||
$image->loadFromFile(\OC_Filesystem::getLocalFile($path));
|
||||
if (!$image->valid()) {
|
||||
return false;
|
||||
}
|
||||
$arr = DatabaseManager::getInstance()->setFileData($path, $this->getThumbnailWidth($image), $this->getThumbnailHeight($image));
|
||||
}
|
||||
$ret = array('filepath' => $arr['path'],
|
||||
'width' => $arr['width'],
|
||||
|
|
|
|||
102
lib/image.php
102
lib/image.php
|
|
@ -107,6 +107,56 @@ class OC_Image {
|
|||
return $this->valid() ? imagesy($this->resource) : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the width when the image orientation is top-left.
|
||||
* @returns int
|
||||
*/
|
||||
public function widthTopLeft() {
|
||||
$o = $this->getOrientation();
|
||||
OC_Log::write('core','OC_Image->widthTopLeft() Orientation: '.$o, OC_Log::DEBUG);
|
||||
switch($o) {
|
||||
case -1:
|
||||
case 1:
|
||||
case 2: // Not tested
|
||||
case 3:
|
||||
case 4: // Not tested
|
||||
return $this->width();
|
||||
break;
|
||||
case 5: // Not tested
|
||||
case 6:
|
||||
case 7: // Not tested
|
||||
case 8:
|
||||
return $this->height();
|
||||
break;
|
||||
}
|
||||
return $this->width();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the height when the image orientation is top-left.
|
||||
* @returns int
|
||||
*/
|
||||
public function heightTopLeft() {
|
||||
$o = $this->getOrientation();
|
||||
OC_Log::write('core','OC_Image->heightTopLeft() Orientation: '.$o, OC_Log::DEBUG);
|
||||
switch($o) {
|
||||
case -1:
|
||||
case 1:
|
||||
case 2: // Not tested
|
||||
case 3:
|
||||
case 4: // Not tested
|
||||
return $this->height();
|
||||
break;
|
||||
case 5: // Not tested
|
||||
case 6:
|
||||
case 7: // Not tested
|
||||
case 8:
|
||||
return $this->width();
|
||||
break;
|
||||
}
|
||||
return $this->height();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Outputs the image.
|
||||
* @returns bool
|
||||
|
|
@ -207,36 +257,48 @@ class OC_Image {
|
|||
return base64_encode($this->data());
|
||||
}
|
||||
|
||||
/**
|
||||
* (I'm open for suggestions on better method name ;)
|
||||
* @brief Get the orientation based on EXIF data.
|
||||
* @returns The orientation or -1 if no EXIF data is available.
|
||||
*/
|
||||
public function getOrientation() {
|
||||
if(!is_callable('exif_read_data')){
|
||||
OC_Log::write('core','OC_Image->fixOrientation() Exif module not enabled.', OC_Log::DEBUG);
|
||||
return -1;
|
||||
}
|
||||
if(!$this->valid()) {
|
||||
OC_Log::write('core','OC_Image->fixOrientation() No image loaded.', OC_Log::DEBUG);
|
||||
return -1;
|
||||
}
|
||||
if(is_null($this->filepath) || !is_readable($this->filepath)) {
|
||||
OC_Log::write('core','OC_Image->fixOrientation() No readable file path set.', OC_Log::DEBUG);
|
||||
return -1;
|
||||
}
|
||||
$exif = @exif_read_data($this->filepath, 'IFD0');
|
||||
if(!$exif) {
|
||||
return -1;
|
||||
}
|
||||
if(!isset($exif['Orientation'])) {
|
||||
return -1;
|
||||
}
|
||||
return $exif['Orientation'];
|
||||
}
|
||||
|
||||
/**
|
||||
* (I'm open for suggestions on better method name ;)
|
||||
* @brief Fixes orientation based on EXIF data.
|
||||
* @returns bool.
|
||||
*/
|
||||
public function fixOrientation() {
|
||||
if(!is_callable('exif_read_data')){
|
||||
OC_Log::write('core','OC_Image->fixOrientation() Exif module not enabled.', OC_Log::DEBUG);
|
||||
return false;
|
||||
}
|
||||
if(!$this->valid()) {
|
||||
OC_Log::write('core','OC_Image->fixOrientation() No image loaded.', OC_Log::DEBUG);
|
||||
return false;
|
||||
}
|
||||
if(is_null($this->filepath) || !is_readable($this->filepath)) {
|
||||
OC_Log::write('core','OC_Image->fixOrientation() No readable file path set.', OC_Log::DEBUG);
|
||||
return false;
|
||||
}
|
||||
$exif = @exif_read_data($this->filepath, 'IFD0');
|
||||
if(!$exif) {
|
||||
return false;
|
||||
}
|
||||
if(!isset($exif['Orientation'])) {
|
||||
return true; // Nothing to fix
|
||||
}
|
||||
$o = $exif['Orientation'];
|
||||
$o = $this->getOrienation();
|
||||
OC_Log::write('core','OC_Image->fixOrientation() Orientation: '.$o, OC_Log::DEBUG);
|
||||
$rotate = 0;
|
||||
$flip = false;
|
||||
switch($o) {
|
||||
case -1:
|
||||
return false; //Nothing to fix
|
||||
break;
|
||||
case 1:
|
||||
$rotate = 0;
|
||||
$flip = false;
|
||||
|
|
|
|||
Loading…
Reference in a new issue