* Construct a photo array for a system resource image
*
* @param string $filename Image file name relative to code root
- * @param string $mimetype Image mime type. Defaults to "image/jpeg"
+ * @param string $mimetype Image mime type. Is guessed by file name when empty.
*
* @return array
* @throws \Exception
*/
- public static function createPhotoForSystemResource($filename, $mimetype = "image/jpeg")
+ public static function createPhotoForSystemResource($filename, $mimetype = '')
{
+ if (empty($mimetype)) {
+ $mimetype = Images::guessTypeByExtension($filename);
+ }
+
$fields = self::getFields();
$values = array_fill(0, count($fields), "");
* Construct a photo array for an external resource image
*
* @param string $url Image URL
- * @param string $mimetype Image mime type. Defaults to "image/jpeg"
+ * @param int $uid User ID of the requesting person
+ * @param string $mimetype Image mime type. Is guessed by file name when empty.
*
* @return array
* @throws \Exception
*/
- public static function createPhotoForExternalResource($url, $mimetype = "image/jpeg")
+ public static function createPhotoForExternalResource($url, $uid = 0, $mimetype = '')
{
+ if (empty($mimetype)) {
+ $mimetype = Images::guessTypeByExtension($url);
+ }
+
$fields = self::getFields();
$values = array_fill(0, count($fields), "");
$photo = array_combine($fields, $values);
$photo['backend-class'] = ExternalResource::NAME;
- $photo['backend-ref'] = $url;
+ $photo['backend-ref'] = json_encode(['url' => $url, 'uid' => $uid]);
$photo['type'] = $mimetype;
- $photo['cacheable'] = false;
+ $photo['cacheable'] = true;
return $photo;
}
}
/**
- * Returns the GUID from picture links
+ * Fetch the guid and scale from picture links
*
* @param string $name Picture link
- * @return string GUID
- * @throws \Exception
+ * @return array
*/
- public static function getGUID($name)
+ public static function getResourceData(string $name):array
{
$base = DI::baseUrl()->get();
$guid = str_replace([Strings::normaliseLink($base), '/photo/'], '', Strings::normaliseLink($name));
+ if (parse_url($guid, PHP_URL_SCHEME)) {
+ return [];
+ }
+
$guid = self::stripExtension($guid);
if (substr($guid, -2, 1) != "-") {
- return '';
+ return [];
}
$scale = intval(substr($guid, -1, 1));
if (!is_numeric($scale)) {
- return '';
+ return [];
}
$guid = substr($guid, 0, -2);
- return $guid;
+ return ['guid' => $guid, 'scale' => $scale];
}
/**
*/
public static function isLocal($name)
{
- $guid = self::getGUID($name);
+ return (bool)self::getIdForName($name);
+ }
- if (empty($guid)) {
- return false;
+ /**
+ * Return the id of a local photo
+ *
+ * @param string $name Picture link
+ * @return int
+ */
+ public static function getIdForName($name)
+ {
+ $data = self::getResourceData($name);
+ if (empty($data)) {
+ return 0;
}
- return DBA::exists('photo', ['resource-id' => $guid]);
+ $photo = DBA::selectFirst('photo', ['id'], ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
+ if (!empty($photo['id'])) {
+ return $photo['id'];
+ }
+ return 0;
}
/**