4 * @file src/Model/Photo.php
5 * @brief This file contains the Photo class for database interface
7 namespace Friendica\Model;
9 use Friendica\Core\Cache;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Object\Image;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Util\Network;
17 use Friendica\Util\Security;
19 require_once 'include/dba.php';
22 * Class to handle photo dabatase table
27 * @param Image $Image image
28 * @param integer $uid uid
29 * @param integer $cid cid
30 * @param integer $rid rid
31 * @param string $filename filename
32 * @param string $album album name
33 * @param integer $scale scale
34 * @param integer $profile optional, default = 0
35 * @param string $allow_cid optional, default = ''
36 * @param string $allow_gid optional, default = ''
37 * @param string $deny_cid optional, default = ''
38 * @param string $deny_gid optional, default = ''
39 * @param string $desc optional, default = ''
42 public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
44 $photo = DBA::selectFirst('photo', ['guid'], ["`resource-id` = ? AND `guid` != ?", $rid, '']);
45 if (DBA::isResult($photo)) {
46 $guid = $photo['guid'];
48 $guid = System::createGUID();
51 $existing_photo = DBA::selectFirst('photo', ['id'], ['resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale]);
57 'resource-id' => $rid,
58 'created' => DateTimeFormat::utcNow(),
59 'edited' => DateTimeFormat::utcNow(),
60 'filename' => basename($filename),
61 'type' => $Image->getType(),
63 'height' => $Image->getHeight(),
64 'width' => $Image->getWidth(),
65 'datasize' => strlen($Image->asString()),
66 'data' => $Image->asString(),
68 'profile' => $profile,
69 'allow_cid' => $allow_cid,
70 'allow_gid' => $allow_gid,
71 'deny_cid' => $deny_cid,
72 'deny_gid' => $deny_gid,
76 if (DBA::isResult($existing_photo)) {
77 $r = DBA::update('photo', $fields, ['id' => $existing_photo['id']]);
79 $r = DBA::insert('photo', $fields);
86 * @param string $image_url Remote URL
87 * @param integer $uid user id
88 * @param integer $cid contact id
89 * @param boolean $quit_on_error optional, default false
92 public static function importProfilePhoto($image_url, $uid, $cid, $quit_on_error = false)
97 $photo = DBA::selectFirst(
98 'photo', ['resource-id'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos']
100 if (x($photo['resource-id'])) {
101 $hash = $photo['resource-id'];
103 $hash = self::newResource();
106 $photo_failure = false;
108 $filename = basename($image_url);
109 $img_str = Network::fetchUrl($image_url, true);
111 if ($quit_on_error && ($img_str == "")) {
115 $type = Image::guessType($image_url, true);
116 $Image = new Image($img_str, $type);
117 if ($Image->isValid()) {
118 $Image->scaleToSquare(300);
120 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
123 $photo_failure = true;
126 $Image->scaleDown(80);
128 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
131 $photo_failure = true;
134 $Image->scaleDown(48);
136 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
139 $photo_failure = true;
142 $suffix = '?ts=' . time();
144 $image_url = System::baseUrl() . '/photo/' . $hash . '-4.' . $Image->getExt() . $suffix;
145 $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $Image->getExt() . $suffix;
146 $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $Image->getExt() . $suffix;
148 // Remove the cached photo
150 $basepath = $a->getBasePath();
152 if (is_dir($basepath . "/photo")) {
153 $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
154 if (file_exists($filename)) {
157 $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
158 if (file_exists($filename)) {
161 $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
162 if (file_exists($filename)) {
167 $photo_failure = true;
170 if ($photo_failure && $quit_on_error) {
174 if ($photo_failure) {
175 $image_url = System::baseUrl() . '/images/person-300.jpg';
176 $thumb = System::baseUrl() . '/images/person-80.jpg';
177 $micro = System::baseUrl() . '/images/person-48.jpg';
180 return [$image_url, $thumb, $micro];
184 * @param string $exifCoord coordinate
185 * @param string $hemi hemi
188 public static function getGps($exifCoord, $hemi)
190 $degrees = count($exifCoord) > 0 ? self::gps2Num($exifCoord[0]) : 0;
191 $minutes = count($exifCoord) > 1 ? self::gps2Num($exifCoord[1]) : 0;
192 $seconds = count($exifCoord) > 2 ? self::gps2Num($exifCoord[2]) : 0;
194 $flip = ($hemi == 'W' || $hemi == 'S') ? -1 : 1;
196 return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
200 * @param string $coordPart coordPart
203 private static function gps2Num($coordPart)
205 $parts = explode('/', $coordPart);
207 if (count($parts) <= 0) {
211 if (count($parts) == 1) {
215 return floatval($parts[0]) / floatval($parts[1]);
219 * @brief Fetch the photo albums that are available for a viewer
221 * The query in this function is cost intensive, so it is cached.
223 * @param int $uid User id of the photos
224 * @param bool $update Update the cache
226 * @return array Returns array of the photo albums
228 public static function getAlbums($uid, $update = false)
230 $sql_extra = Security::getPermissionsSQLByUserId($uid);
232 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
233 $albums = Cache::get($key);
234 if (is_null($albums) || $update) {
235 if (!Config::get('system', 'no_count', false)) {
236 /// @todo This query needs to be renewed. It is really slow
237 // At this time we just store the data in the cache
238 $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
240 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
241 GROUP BY `album` ORDER BY `created` DESC",
243 DBA::escape('Contact Photos'),
244 DBA::escape(L10n::t('Contact Photos'))
247 // This query doesn't do the count and is much faster
248 $albums = q("SELECT DISTINCT(`album`), '' AS `total`
249 FROM `photo` USE INDEX (`uid_album_scale_created`)
250 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra",
252 DBA::escape('Contact Photos'),
253 DBA::escape(L10n::t('Contact Photos'))
256 Cache::set($key, $albums, Cache::DAY);
262 * @param int $uid User id of the photos
265 public static function clearAlbumCache($uid)
267 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
268 Cache::set($key, null, Cache::DAY);
272 * Generate a unique photo ID.
276 public static function newResource()
278 return system::createGUID(32, false);