aboutsummaryrefslogtreecommitdiffstats
path: root/include/photo/photo_driver.php
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2018-01-09 09:00:20 +0100
committerMario <mario@mariovavti.com>2018-01-09 09:00:20 +0100
commit4f4d0e416eac87121898b8a27b1afa6065ff17a2 (patch)
treeaae7f2582b2b9c6596dcbf87c06a836434140830 /include/photo/photo_driver.php
parent22c89b6c660e185d5c5c6362acf23b145d932d15 (diff)
parent8fde0f01b8472082158b38386046ed606bcfbc49 (diff)
downloadvolse-hubzilla-3.0.tar.gz
volse-hubzilla-3.0.tar.bz2
volse-hubzilla-3.0.zip
Merge branch '3.0RC'3.0
Diffstat (limited to 'include/photo/photo_driver.php')
-rw-r--r--include/photo/photo_driver.php138
1 files changed, 84 insertions, 54 deletions
diff --git a/include/photo/photo_driver.php b/include/photo/photo_driver.php
index 5eb1f9113..6af753195 100644
--- a/include/photo/photo_driver.php
+++ b/include/photo/photo_driver.php
@@ -129,7 +129,14 @@ abstract class photo_driver {
return $this->types[$this->getType()];
}
- public function scaleImage($max) {
+ /**
+ * @brief scale image
+ * int $max maximum pixel size in either dimension
+ * boolean $float_height - if true allow height to float to any length on tall images,
+ * constraining only the width
+ */
+
+ public function scaleImage($max, $float_height = true) {
if(!$this->is_valid())
return FALSE;
@@ -146,7 +153,7 @@ abstract class photo_driver {
// very tall image (greater than 16:9)
// constrain the width - let the height float.
- if((($height * 9) / 16) > $width) {
+ if(((($height * 9) / 16) > $width) && ($float_height)) {
$dest_width = $max;
$dest_height = intval(( $height * $max ) / $width);
}
@@ -173,7 +180,7 @@ abstract class photo_driver {
// very tall image (greater than 16:9)
// but width is OK - don't do anything
- if((($height * 9) / 16) > $width) {
+ if(((($height * 9) / 16) > $width) && ($float_height)) {
$dest_width = $width;
$dest_height = $height;
}
@@ -241,74 +248,94 @@ abstract class photo_driver {
}
+ /**
+ * @brief reads exif data from filename
+ */
+ public function exif($filename) {
- public function orient($filename) {
- /**
- * This function is a bit unusual, because it is operating on a file, but you must
- * first create an image from that file to initialise the type and check validity
- * of the image.
- */
-
- if(! $this->is_valid())
- return false;
-
- if((! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg'))
+ if((! function_exists('exif_read_data'))
+ || (! in_array($this->getType(), [ 'image/jpeg' , 'image/tiff'] ))) {
return false;
+ }
- $exif = @exif_read_data($filename,null,true);
-
- if($exif) {
- $ort = $exif['IFD0']['Orientation'];
-
- switch($ort)
- {
- case 1: // nothing
- break;
+ /*
+ * PHP 7.2 allows you to use a stream resource, which should reduce/avoid
+ * memory exhaustion on large images.
+ */
- case 2: // horizontal flip
- $this->flip();
- break;
+ if(version_compare(PHP_VERSION,'7.2.0') >= 0) {
+ $f = @fopen($filename,'rb');
+ }
+ else {
+ $f = $filename;
+ }
- case 3: // 180 rotate left
- $this->rotate(180);
- break;
+ if($f) {
+ return @exif_read_data($f);
+ }
- case 4: // vertical flip
- $this->flip(false, true);
- break;
+ return false;
+ }
- case 5: // vertical flip + 90 rotate right
- $this->flip(false, true);
- $this->rotate(-90);
- break;
+ /**
+ * @brief orients current image based on exif orientation information
+ */
- case 6: // 90 rotate right
- $this->rotate(-90);
- break;
+ public function orient($exif) {
- case 7: // horizontal flip + 90 rotate right
- $this->flip();
- $this->rotate(-90);
- break;
+ if(! ($this->is_valid() && $exif)) {
+ return false;
+ }
- case 8: // 90 rotate left
- $this->rotate(90);
- break;
- }
+ $ort = $exif['IFD0']['Orientation'];
- return $exif;
+ if(! $ort) {
+ return false;
+ }
+ switch($ort) {
+ case 1: // nothing
+ break;
+ case 2: // horizontal flip
+ $this->flip();
+ break;
+ case 3: // 180 rotate left
+ $this->rotate(180);
+ break;
+ case 4: // vertical flip
+ $this->flip(false, true);
+ break;
+ case 5: // vertical flip + 90 rotate right
+ $this->flip(false, true);
+ $this->rotate(-90);
+ break;
+ case 6: // 90 rotate right
+ $this->rotate(-90);
+ break;
+ case 7: // horizontal flip + 90 rotate right
+ $this->flip();
+ $this->rotate(-90);
+ break;
+ case 8: // 90 rotate left
+ $this->rotate(90);
+ break;
+ default:
+ break;
}
-
- return false;
+ return true;
}
public function save($arr) {
+ if(! $this->is_valid()) {
+ logger('attempt to store invalid photo.');
+ return false;
+ }
+
$p = array();
$p['aid'] = ((intval($arr['aid'])) ? intval($arr['aid']) : 0);
@@ -331,6 +358,8 @@ abstract class photo_driver {
$p['os_path'] = $arr['os_path'];
$p['os_syspath'] = ((array_key_exists('os_syspath',$arr)) ? $arr['os_syspath'] : '');
$p['display_path'] = (($arr['display_path']) ? $arr['display_path'] : '');
+ $p['width'] = (($arr['width']) ? $arr['width'] : $this->getWidth());
+ $p['height'] = (($arr['height']) ? $arr['height'] : $this->getHeight());
if(! intval($p['imgscale']))
logger('save: ' . print_r($arr,true), LOGGER_DATA);
@@ -378,8 +407,8 @@ abstract class photo_driver {
dbesc(basename($p['filename'])),
dbesc($this->getType()),
dbesc($p['album']),
- intval($this->getHeight()),
- intval($this->getWidth()),
+ intval($p['height']),
+ intval($p['width']),
(intval($p['os_storage']) ? dbescbin($p['os_syspath']) : dbescbin($this->imageString())),
intval($p['os_storage']),
intval(strlen($this->imageString())),
@@ -409,8 +438,8 @@ abstract class photo_driver {
dbesc(basename($p['filename'])),
dbesc($this->getType()),
dbesc($p['album']),
- intval($this->getHeight()),
- intval($this->getWidth()),
+ intval($p['height']),
+ intval($p['width']),
(intval($p['os_storage']) ? dbescbin($p['os_syspath']) : dbescbin($this->imageString())),
intval($p['os_storage']),
intval(strlen($this->imageString())),
@@ -426,6 +455,7 @@ abstract class photo_driver {
dbesc($p['deny_gid'])
);
}
+ logger('photo save ' . $p['imgscale'] . ' returned ' . intval($r));
return $r;
}