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\DBM;
14 use Friendica\Object\Image;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Util\Network;
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 (DBM::is_result($photo)) {
46 $guid = $photo['guid'];
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 (DBM::is_result($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)
94 $photo = dba::selectFirst(
95 'photo', ['resource-id'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos']
97 if (x($photo['resource-id'])) {
98 $hash = $photo['resource-id'];
100 $hash = photo_new_resource();
103 $photo_failure = false;
105 $filename = basename($image_url);
106 $img_str = Network::fetchUrl($image_url, true);
108 if ($quit_on_error && ($img_str == "")) {
112 $type = Image::guessType($image_url, true);
113 $Image = new Image($img_str, $type);
114 if ($Image->isValid()) {
115 $Image->scaleToSquare(175);
117 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
120 $photo_failure = true;
123 $Image->scaleDown(80);
125 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
128 $photo_failure = true;
131 $Image->scaleDown(48);
133 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
136 $photo_failure = true;
139 $suffix = '?ts=' . time();
141 $image_url = System::baseUrl() . '/photo/' . $hash . '-4.' . $Image->getExt() . $suffix;
142 $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $Image->getExt() . $suffix;
143 $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $Image->getExt() . $suffix;
145 // Remove the cached photo
147 $basepath = $a->get_basepath();
149 if (is_dir($basepath . "/photo")) {
150 $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
151 if (file_exists($filename)) {
154 $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
155 if (file_exists($filename)) {
158 $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
159 if (file_exists($filename)) {
164 $photo_failure = true;
167 if ($photo_failure && $quit_on_error) {
171 if ($photo_failure) {
172 $image_url = System::baseUrl() . '/images/person-175.jpg';
173 $thumb = System::baseUrl() . '/images/person-80.jpg';
174 $micro = System::baseUrl() . '/images/person-48.jpg';
177 return [$image_url, $thumb, $micro];
181 * @param string $exifCoord coordinate
182 * @param string $hemi hemi
185 public static function getGps($exifCoord, $hemi)
187 $degrees = count($exifCoord) > 0 ? self::gps2Num($exifCoord[0]) : 0;
188 $minutes = count($exifCoord) > 1 ? self::gps2Num($exifCoord[1]) : 0;
189 $seconds = count($exifCoord) > 2 ? self::gps2Num($exifCoord[2]) : 0;
191 $flip = ($hemi == 'W' || $hemi == 'S') ? -1 : 1;
193 return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
197 * @param string $coordPart coordPart
200 private static function gps2Num($coordPart)
202 $parts = explode('/', $coordPart);
204 if (count($parts) <= 0) {
208 if (count($parts) == 1) {
212 return floatval($parts[0]) / floatval($parts[1]);
216 * @brief Fetch the photo albums that are available for a viewer
218 * The query in this function is cost intensive, so it is cached.
220 * @param int $uid User id of the photos
221 * @param bool $update Update the cache
223 * @return array Returns array of the photo albums
225 public static function getAlbums($uid, $update = false)
227 $sql_extra = permissions_sql($uid);
229 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
230 $albums = Cache::get($key);
231 if (is_null($albums) || $update) {
232 if (!Config::get('system', 'no_count', false)) {
233 /// @todo This query needs to be renewed. It is really slow
234 // At this time we just store the data in the cache
235 $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
237 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
238 GROUP BY `album` ORDER BY `created` DESC",
240 dbesc('Contact Photos'),
241 dbesc(L10n::t('Contact Photos'))
244 // This query doesn't do the count and is much faster
245 $albums = q("SELECT DISTINCT(`album`), '' AS `total`
246 FROM `photo` USE INDEX (`uid_album_scale_created`)
247 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra",
249 dbesc('Contact Photos'),
250 dbesc(L10n::t('Contact Photos'))
253 Cache::set($key, $albums, CACHE_DAY);
259 * @param int $uid User id of the photos
262 public static function clearAlbumCache($uid)
264 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
265 Cache::set($key, null, CACHE_DAY);