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\PConfig;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
13 use Friendica\Object\Image;
16 require_once 'include/dba.php';
19 * Class to handle photo dabatase table
24 * @param Image $Image image
25 * @param integer $uid uid
26 * @param integer $cid cid
27 * @param integer $rid rid
28 * @param string $filename filename
29 * @param string $album album name
30 * @param integer $scale scale
31 * @param integer $profile optional, default = 0
32 * @param string $allow_cid optional, default = ''
33 * @param string $allow_gid optional, default = ''
34 * @param string $deny_cid optional, default = ''
35 * @param string $deny_gid optional, default = ''
36 * @param string $desc optional, default = ''
39 public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
41 $photo = dba::selectFirst('photo', ['guid'], ["`resource-id` = ? AND `guid` != ?", $rid, '']);
42 if (DBM::is_result($photo)) {
43 $guid = $photo['guid'];
48 $existing_photo = dba::selectFirst('photo', ['id'], ['resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale]);
54 'resource-id' => $rid,
55 'created' => datetime_convert(),
56 'edited' => datetime_convert(),
57 'filename' => basename($filename),
58 'type' => $Image->getType(),
60 'height' => $Image->getHeight(),
61 'width' => $Image->getWidth(),
62 'datasize' => strlen($Image->asString()),
63 'data' => $Image->asString(),
65 'profile' => $profile,
66 'allow_cid' => $allow_cid,
67 'allow_gid' => $allow_gid,
68 'deny_cid' => $deny_cid,
69 'deny_gid' => $deny_gid,
73 if (DBM::is_result($existing_photo)) {
74 $r = dba::update('photo', $fields, ['id' => $existing_photo['id']]);
76 $r = dba::insert('photo', $fields);
83 * @param string $image_url Remote URL
84 * @param integer $uid user id
85 * @param integer $cid contact id
86 * @param boolean $quit_on_error optional, default false
89 public static function importProfilePhoto($image_url, $uid, $cid, $quit_on_error = false)
91 $photo = dba::selectFirst(
92 'photo', ['resource-id'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos']
94 if (x($photo['resource-id'])) {
95 $hash = $photo['resource-id'];
97 $hash = photo_new_resource();
100 $photo_failure = false;
102 $filename = basename($image_url);
103 $img_str = fetch_url($image_url, true);
105 if ($quit_on_error && ($img_str == "")) {
109 $type = Image::guessType($image_url, true);
110 $Image = new Image($img_str, $type);
111 if ($Image->isValid()) {
112 $Image->scaleToSquare(175);
114 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
117 $photo_failure = true;
120 $Image->scaleDown(80);
122 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
125 $photo_failure = true;
128 $Image->scaleDown(48);
130 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
133 $photo_failure = true;
136 $suffix = '?ts=' . time();
138 $image_url = System::baseUrl() . '/photo/' . $hash . '-4.' . $Image->getExt() . $suffix;
139 $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $Image->getExt() . $suffix;
140 $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $Image->getExt() . $suffix;
142 // Remove the cached photo
144 $basepath = $a->get_basepath();
146 if (is_dir($basepath . "/photo")) {
147 $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
148 if (file_exists($filename)) {
151 $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
152 if (file_exists($filename)) {
155 $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
156 if (file_exists($filename)) {
161 $photo_failure = true;
164 if ($photo_failure && $quit_on_error) {
168 if ($photo_failure) {
169 $image_url = System::baseUrl() . '/images/person-175.jpg';
170 $thumb = System::baseUrl() . '/images/person-80.jpg';
171 $micro = System::baseUrl() . '/images/person-48.jpg';
174 return array($image_url, $thumb, $micro);
178 * @param string $exifCoord coordinate
179 * @param string $hemi hemi
182 public static function getGps($exifCoord, $hemi)
184 $degrees = count($exifCoord) > 0 ? self::gps2Num($exifCoord[0]) : 0;
185 $minutes = count($exifCoord) > 1 ? self::gps2Num($exifCoord[1]) : 0;
186 $seconds = count($exifCoord) > 2 ? self::gps2Num($exifCoord[2]) : 0;
188 $flip = ($hemi == 'W' || $hemi == 'S') ? -1 : 1;
190 return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
194 * @param string $coordPart coordPart
197 private static function gps2Num($coordPart)
199 $parts = explode('/', $coordPart);
201 if (count($parts) <= 0) {
205 if (count($parts) == 1) {
209 return floatval($parts[0]) / floatval($parts[1]);
213 * @brief Fetch the photo albums that are available for a viewer
215 * The query in this function is cost intensive, so it is cached.
217 * @param int $uid User id of the photos
218 * @param bool $update Update the cache
220 * @return array Returns array of the photo albums
222 public static function getAlbums($uid, $update = false)
224 $sql_extra = permissions_sql($uid);
226 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
227 $albums = Cache::get($key);
228 if (is_null($albums) || $update) {
229 if (!Config::get('system', 'no_count', false)) {
230 /// @todo This query needs to be renewed. It is really slow
231 // At this time we just store the data in the cache
232 $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
234 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
235 GROUP BY `album` ORDER BY `created` DESC",
237 dbesc('Contact Photos'),
238 dbesc(t('Contact Photos'))
241 // This query doesn't do the count and is much faster
242 $albums = q("SELECT DISTINCT(`album`), '' AS `total`
243 FROM `photo` USE INDEX (`uid_album_scale_created`)
244 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra",
246 dbesc('Contact Photos'),
247 dbesc(t('Contact Photos'))
250 Cache::set($key, $albums, CACHE_DAY);
256 * @param int $uid User id of the photos
259 public static function clearAlbumCache($uid)
261 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
262 Cache::set($key, null, CACHE_DAY);