Clean up and documentation in photo_drive.

This commit is contained in:
Klaus Weidenbach 2019-02-01 00:21:01 +01:00
parent 3217452b02
commit d70bba2806

View File

@ -1,18 +1,30 @@
<?php /** @file */
<?php
/**
* @brief Return a photo_driver object.
*
* Use this factory when manipulating images.
*
* Return a photo driver object implementing ImageMagick or GD.
*
* @param string $data Image data
* @param string $type
* @return null|photo_driver
* NULL if unsupported image type or failure, otherwise photo driver object
*/
function photo_factory($data, $type = null) {
$ph = null;
$m = null;
$unsupported_types = array(
$unsupported_types = [
'image/bmp',
'image/vnd.microsoft.icon',
'image/tiff',
'image/svg+xml'
);
'image/svg+xml',
];
if($type && in_array(strtolower($type), $unsupported_types)) {
logger('photo_factory: unsupported image type');
logger('Unsupported image type ' . $type);
return null;
}
@ -24,8 +36,7 @@ function photo_factory($data, $type = null) {
if(version_compare($m[1], '6.6.7') >= 0) {
require_once('include/photo/photo_imagick.php');
$ph = new photo_imagick($data, $type);
}
else {
} else {
// earlier imagick versions have issues with scaling png's
// don't log this because it will just fill the logfile.
// leave this note here so those who are looking for why
@ -42,8 +53,10 @@ function photo_factory($data, $type = null) {
}
/**
* @brief Abstract photo driver class.
*
*/
abstract class photo_driver {
protected $image;
@ -99,12 +112,14 @@ abstract class photo_driver {
public function getWidth() {
if(!$this->is_valid())
return FALSE;
return $this->width;
}
public function getHeight() {
if(!$this->is_valid())
return FALSE;
return $this->height;
}
@ -112,6 +127,7 @@ abstract class photo_driver {
public function saveImage($path) {
if(!$this->is_valid())
return FALSE;
return (file_put_contents($path, $this->imageString()) ? true : false);
}
@ -131,12 +147,14 @@ abstract class photo_driver {
}
/**
* @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
* @brief Scale image to max pixel size in either dimension.
*
* @param int $max maximum pixel size in either dimension
* @param boolean $float_height (optional)
* if true allow height to float to any length on tall images, constraining
* only the width
* @return boolean|void false on failure, otherwise void
*/
public function scaleImage($max, $float_height = true) {
if(!$this->is_valid())
return FALSE;
@ -203,7 +221,6 @@ abstract class photo_driver {
if(!$this->is_valid())
return FALSE;
$width = $this->width;
$height = $this->height;
@ -241,21 +258,27 @@ abstract class photo_driver {
$this->doScaleImage($dest_width,$dest_height);
}
/**
* @brief Scales image to a square.
*
* @param int $dim Pixel of squre image
* @return boolean|void false on failure, otherwise void
*/
public function scaleImageSquare($dim) {
if(!$this->is_valid())
return FALSE;
$this->doScaleImage($dim, $dim);
}
/**
* @brief reads exif data from filename
* @brief reads exif data from filename.
*
* @param string $filename
* @return boolean|array
*/
public function exif($filename) {
if((! function_exists('exif_read_data'))
|| (! in_array($this->getType(), [ 'image/jpeg' , 'image/tiff'] ))) {
return false;
@ -281,9 +304,11 @@ abstract class photo_driver {
}
/**
* @brief orients current image based on exif orientation information
* @brief Orients current image based on exif orientation information.
*
* @param array $exif
* @return boolean true if oriented, otherwise false
*/
public function orient($exif) {
if(! ($this->is_valid() && $exif)) {
@ -329,15 +354,21 @@ abstract class photo_driver {
return true;
}
/**
* @brief Save photo to database.
*
* @param array $arr
* @param boolean $skipcheck (optional) default false
* @return boolean|array
*/
public function save($arr, $skipcheck = false) {
if(! ($skipcheck || $this->is_valid())) {
logger('attempt to store invalid photo.');
logger('Attempt to store invalid photo.');
return false;
}
$p = array();
$p = [];
$p['aid'] = ((intval($arr['aid'])) ? intval($arr['aid']) : 0);
$p['uid'] = ((intval($arr['uid'])) ? intval($arr['uid']) : 0);
@ -463,29 +494,32 @@ abstract class photo_driver {
dbescdate($p['expires'])
);
}
logger('photo save ' . $p['imgscale'] . ' returned ' . intval($r));
logger('Photo save ' . $p['imgscale'] . ' returned ' . intval($r));
return $r;
}
}
/**
* Guess image mimetype from filename or from Content-Type header
* @brief Guess image mimetype from filename or from Content-Type header.
*
* @arg $filename string Image filename
* @arg $headers string Headers to check for Content-Type (from curl request)
* @param string $filename
* Image filename
* @param string $headers (optional)
* Headers to check for Content-Type (from curl request)
* @return null|string Guessed mimetype
*/
function guess_image_type($filename, $headers = '') {
// logger('Photo: guess_image_type: '.$filename . ($headers?' from curl headers':''), LOGGER_DEBUG);
$type = null;
$m = null;
if($headers) {
$hdrs=array();
$hdrs = [];
$h = explode("\n", $headers);
foreach ($h as $l) {
list($k,$v) = array_map("trim", explode(":", trim($l), 2));
list($k,$v) = array_map('trim', explode(':', trim($l), 2));
$hdrs[strtolower($k)] = $v;
}
logger('Curl headers: ' .var_export($hdrs, true), LOGGER_DEBUG);
@ -551,11 +585,17 @@ function guess_image_type($filename, $headers = '') {
}
logger('Photo: guess_image_type: filename = ' . $filename . ' type = ' . $type, LOGGER_DEBUG);
return $type;
return $type;
}
/**
* @brief Delete thing photo from database.
*
* @param string $url
* @param string $ob_hash
* @return void
*/
function delete_thing_photo($url, $ob_hash) {
$hash = basename($url);
@ -566,18 +606,15 @@ function delete_thing_photo($url,$ob_hash) {
if((! $ob_hash) || (strlen($hash) < 16))
return;
$r = q("delete from photo where xchan = '%s' and photo_usage = %d and resource_id = '%s'",
q("delete from photo where xchan = '%s' and photo_usage = %d and resource_id = '%s'",
dbesc($ob_hash),
intval(PHOTO_THING),
dbesc($hash)
);
}
/**
* @brief fetches an photo from external site and prepares its miniatures.
* @brief Fetches a photo from external site and prepares its miniatures.
*
* @param string $photo
* external URL to fetch base image
@ -596,30 +633,24 @@ function delete_thing_photo($url,$ob_hash) {
* * \e boolean \b 4 => TRUE if fetch failure
* * \e string \b 5 => modification date
*/
function import_xchan_photo($photo, $xchan, $thing = false, $force = false) {
$modified = '';
$o = null;
$flags = (($thing) ? PHOTO_THING : PHOTO_XCHAN);
$album = (($thing) ? 'Things' : 'Contact Photos');
logger('import_xchan_photo: updating channel photo from ' . $photo . ' for ' . $xchan, LOGGER_DEBUG);
logger('Updating channel photo from ' . $photo . ' for ' . $xchan, LOGGER_DEBUG);
if($thing) {
$hash = photo_new_resource();
}
else {
$r = q("select resource_id, edited, mimetype from photo where xchan = '%s' and photo_usage = %d and imgscale = 4 limit 1",
dbesc($xchan),
intval(PHOTO_XCHAN)
);
} else {
$r = q("select resource_id, edited, mimetype from photo where xchan = '%s' and photo_usage = %d and imgscale = 4 limit 1", dbesc($xchan), intval(PHOTO_XCHAN));
if($r) {
$hash = $r[0]['resource_id'];
$modified = $r[0]['edited'];
$type = $r[0]['mimetype'];
}
else {
} else {
$hash = photo_new_resource();
}
}
@ -628,13 +659,15 @@ function import_xchan_photo($photo,$xchan,$thing = false,$force = false) {
$img_str = '';
if($photo) {
$filename = basename($photo);
if($force || $modified == '') {
$result = z_fetch_url($photo, true);
}
else {
$h = array('headers' => array("If-Modified-Since: " . gmdate("D, d M Y H:i:s", strtotime($modified . "Z")) . " GMT"));
} else {
$h = [
'headers' => [
'If-Modified-Since: ' . gmdate('D, d M Y H:i:s', strtotime($modified . 'Z')) . ' GMT'
]
];
$result = z_fetch_url($photo, true, 0, $h);
}
@ -644,19 +677,16 @@ function import_xchan_photo($photo,$xchan,$thing = false,$force = false) {
$modified = gmdate('Y-m-d H:i:s', (preg_match('/last-modified: (.+) \S+/i', $result['header'], $o) ? strtotime($o[1] . 'Z') : time()));
if(is_null($type))
$photo_failure = true;
}
elseif($result['return_code'] == 304) {
} elseif($result['return_code'] == 304) {
$photo = z_root() . '/photo/' . $hash . '-4';
$thumb = z_root() . '/photo/' . $hash . '-5';
$micro = z_root() . '/photo/' . $hash . '-6';
}
else {
} else {
$photo_failure = true;
}
}
else
} else {
$photo_failure = true;
}
if(!$photo_failure && $result['return_code'] != 304) {
$img = photo_factory($img_str, $type);
@ -669,58 +699,48 @@ function import_xchan_photo($photo,$xchan,$thing = false,$force = false) {
// crop out the sides
$margin = $width - $height;
$img->cropImage(300, ($margin / 2), 0, $height, $height);
}
elseif(($height / $width) > 1.2) {
} elseif(($height / $width) > 1.2) {
// crop out the bottom
$margin = $height - $width;
$img->cropImage(300, 0, 0, $width, $width);
}
else {
} else {
$img->scaleImageSquare(300);
}
}
else
} else {
$photo_failure = true;
}
$p = array(
$p = [
'xchan' => $xchan,
'resource_id' => $hash,
'filename' => basename($photo),
'album' => $album,
'photo_usage' => $flags,
'imgscale' => 4,
'edited' => $modified
);
'edited' => $modified,
];
$r = $img->save($p);
if($r === false)
$photo_failure = true;
$img->scaleImage(80);
$p['imgscale'] = 5;
$r = $img->save($p);
if($r === false)
$photo_failure = true;
$img->scaleImage(48);
$p['imgscale'] = 6;
$r = $img->save($p);
if($r === false)
$photo_failure = true;
$photo = z_root() . '/photo/' . $hash . '-4';
$thumb = z_root() . '/photo/' . $hash . '-5';
$micro = z_root() . '/photo/' . $hash . '-6';
}
else {
logger('import_xchan_photo: invalid image from ' . $photo);
} else {
logger('Invalid image from ' . $photo);
$photo_failure = true;
}
}
@ -733,86 +753,95 @@ function import_xchan_photo($photo,$xchan,$thing = false,$force = false) {
$modified = gmdate('Y-m-d H:i:s', filemtime($default));
}
logger('HTTP code: ' . $result['return_code'] . '; modified: ' . $modified . '; failure: ' . ($photo_failure ? 'yes' : 'no') . '; URL: ' . $photo, LOGGER_DEBUG);
return(array($photo,$thumb,$micro,$type,$photo_failure,$modified));
logger('HTTP code: ' . $result['return_code'] . '; modified: ' . $modified
. '; failure: ' . ($photo_failure ? 'yes' : 'no') . '; URL: ' . $photo, LOGGER_DEBUG);
return(array($photo,$thumb,$micro,$type,$photo_failure,$modified));
}
/**
* @brief Import channel photo from a URL.
*
* @param string $photo URL to a photo
* @param int $aid
* @param int $uid channel_id
* @return null|string Guessed image mimetype or null.
*/
function import_channel_photo_from_url($photo, $aid, $uid) {
$type = null;
if($photo) {
$filename = basename($photo);
$result = z_fetch_url($photo, true);
if($result['success']) {
$img_str = $result['body'];
$type = guess_image_type($photo, $result['header']);
if(is_null($type))
$photo_failure = true;
}
}
else {
$photo_failure = true;
}
import_channel_photo($img_str, $type, $aid, $uid);
}
}
return $type;
}
/**
* @brief Import a channel photo and prepare its miniatures.
*
* @param string $photo Image data
* @param string $type
* @param int $aid
* @param int $uid channel_id
* @return boolean|string false on failure, otherwise resource_id of photo
*/
function import_channel_photo($photo, $type, $aid, $uid) {
logger('import_channel_photo: importing channel photo for ' . $uid, LOGGER_DEBUG);
$hash = photo_new_resource();
logger('Importing channel photo for ' . $uid, LOGGER_DEBUG);
$photo_failure = false;
$hash = photo_new_resource();
$filename = $hash;
$img = photo_factory($photo, $type);
if($img->is_valid()) {
// config array for image save method
$p = [
'aid' => $aid,
'uid' => $uid,
'resource_id' => $hash,
'filename' => $filename,
'album' => t('Profile Photos'),
'photo_usage' => PHOTO_PROFILE,
'imgscale' => 4,
];
// photo size
$img->scaleImageSquare(300);
$p = array('aid' => $aid, 'uid' => $uid, 'resource_id' => $hash, 'filename' => $filename, 'album' => t('Profile Photos'), 'photo_usage' => PHOTO_PROFILE, 'imgscale' => 4);
$r = $img->save($p);
if($r === false)
$photo_failure = true;
// thumb size
$img->scaleImage(80);
$p['imgscale'] = 5;
$r = $img->save($p);
if($r === false)
$photo_failure = true;
// micro size
$img->scaleImage(48);
$p['imgscale'] = 6;
$r = $img->save($p);
if($r === false)
$photo_failure = true;
}
else {
logger('import_channel_photo: invalid image.');
} else {
logger('Invalid image.');
$photo_failure = true;
}
//return(($photo_failure)? false : true);
if($photo_failure)
return false;
else
return $hash;
}