3 * @file src/Model/Photo.php
4 * @brief This file contains the Photo class for database interface
6 namespace Friendica\Model;
8 use Friendica\Core\Cache;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use Friendica\Object\Image;
15 use Friendica\Util\Network;
18 require_once 'include/dba.php';
21 * Class to handle photo dabatase table
26 * @param Image $Image image
27 * @param integer $uid uid
28 * @param integer $cid cid
29 * @param integer $rid rid
30 * @param string $filename filename
31 * @param string $album album name
32 * @param integer $scale scale
33 * @param integer $profile optional, default = 0
34 * @param string $allow_cid optional, default = ''
35 * @param string $allow_gid optional, default = ''
36 * @param string $deny_cid optional, default = ''
37 * @param string $deny_gid optional, default = ''
38 * @param string $desc optional, default = ''
41 public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
43 $photo = dba::selectFirst('photo', ['guid'], ["`resource-id` = ? AND `guid` != ?", $rid, '']);
44 if (DBM::is_result($photo)) {
45 $guid = $photo['guid'];
50 $existing_photo = dba::selectFirst('photo', ['id'], ['resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale]);
56 'resource-id' => $rid,
57 'created' => datetime_convert(),
58 'edited' => datetime_convert(),
59 'filename' => basename($filename),
60 'type' => $Image->getType(),
62 'height' => $Image->getHeight(),
63 'width' => $Image->getWidth(),
64 'datasize' => strlen($Image->asString()),
65 'data' => $Image->asString(),
67 'profile' => $profile,
68 'allow_cid' => $allow_cid,
69 'allow_gid' => $allow_gid,
70 'deny_cid' => $deny_cid,
71 'deny_gid' => $deny_gid,
75 if (DBM::is_result($existing_photo)) {
76 $r = dba::update('photo', $fields, ['id' => $existing_photo['id']]);
78 $r = dba::insert('photo', $fields);
85 * @param string $image_url Remote URL
86 * @param integer $uid user id
87 * @param integer $cid contact id
88 * @param boolean $quit_on_error optional, default false
91 public static function importProfilePhoto($image_url, $uid, $cid, $quit_on_error = false)
93 $photo = dba::selectFirst(
94 'photo', ['resource-id'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos']
96 if (x($photo['resource-id'])) {
97 $hash = $photo['resource-id'];
99 $hash = photo_new_resource();
102 $photo_failure = false;
104 $filename = basename($image_url);
105 $img_str = Network::fetchUrl($image_url, true);
107 if ($quit_on_error && ($img_str == "")) {
111 $type = Image::guessType($image_url, true);
112 $Image = new Image($img_str, $type);
113 if ($Image->isValid()) {
114 $Image->scaleToSquare(175);
116 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
119 $photo_failure = true;
122 $Image->scaleDown(80);
124 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
127 $photo_failure = true;
130 $Image->scaleDown(48);
132 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
135 $photo_failure = true;
138 $suffix = '?ts=' . time();
140 $image_url = System::baseUrl() . '/photo/' . $hash . '-4.' . $Image->getExt() . $suffix;
141 $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $Image->getExt() . $suffix;
142 $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $Image->getExt() . $suffix;
144 // Remove the cached photo
146 $basepath = $a->get_basepath();
148 if (is_dir($basepath . "/photo")) {
149 $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
150 if (file_exists($filename)) {
153 $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
154 if (file_exists($filename)) {
157 $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
158 if (file_exists($filename)) {
163 $photo_failure = true;
166 if ($photo_failure && $quit_on_error) {
170 if ($photo_failure) {
171 $image_url = System::baseUrl() . '/images/person-175.jpg';
172 $thumb = System::baseUrl() . '/images/person-80.jpg';
173 $micro = System::baseUrl() . '/images/person-48.jpg';
176 return [$image_url, $thumb, $micro];
180 * @param string $exifCoord coordinate
181 * @param string $hemi hemi
184 public static function getGps($exifCoord, $hemi)
186 $degrees = count($exifCoord) > 0 ? self::gps2Num($exifCoord[0]) : 0;
187 $minutes = count($exifCoord) > 1 ? self::gps2Num($exifCoord[1]) : 0;
188 $seconds = count($exifCoord) > 2 ? self::gps2Num($exifCoord[2]) : 0;
190 $flip = ($hemi == 'W' || $hemi == 'S') ? -1 : 1;
192 return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
196 * @param string $coordPart coordPart
199 private static function gps2Num($coordPart)
201 $parts = explode('/', $coordPart);
203 if (count($parts) <= 0) {
207 if (count($parts) == 1) {
211 return floatval($parts[0]) / floatval($parts[1]);
215 * @brief Fetch the photo albums that are available for a viewer
217 * The query in this function is cost intensive, so it is cached.
219 * @param int $uid User id of the photos
220 * @param bool $update Update the cache
222 * @return array Returns array of the photo albums
224 public static function getAlbums($uid, $update = false)
226 $sql_extra = permissions_sql($uid);
228 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
229 $albums = Cache::get($key);
230 if (is_null($albums) || $update) {
231 if (!Config::get('system', 'no_count', false)) {
232 /// @todo This query needs to be renewed. It is really slow
233 // At this time we just store the data in the cache
234 $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
236 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
237 GROUP BY `album` ORDER BY `created` DESC",
239 dbesc('Contact Photos'),
240 dbesc(L10n::t('Contact Photos'))
243 // This query doesn't do the count and is much faster
244 $albums = q("SELECT DISTINCT(`album`), '' AS `total`
245 FROM `photo` USE INDEX (`uid_album_scale_created`)
246 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra",
248 dbesc('Contact Photos'),
249 dbesc(L10n::t('Contact Photos'))
252 Cache::set($key, $albums, CACHE_DAY);
258 * @param int $uid User id of the photos
261 public static function clearAlbumCache($uid)
263 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
264 Cache::set($key, null, CACHE_DAY);