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 $r = dba::select('photo', array('guid'), array("`resource-id` = ? AND `guid` != ?", $rid, ''), array('limit' => 1));
42 if (DBM::is_result($r)) {
48 $x = dba::select('photo', array('id'), array('resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale), array('limit' => 1));
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($x)) {
74 $r = dba::update('photo', $fields, array('id' => $x['id']));
76 $r = dba::insert('photo', $fields);
83 * @param string $photo photo
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($photo, $uid, $cid, $quit_on_error = false)
92 'photo', array('resource-id'), array('uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos'), array('limit' => 1)
95 if (DBM::is_result($r) && strlen($r['resource-id'])) {
96 $hash = $r['resource-id'];
98 $hash = photo_new_resource();
101 $photo_failure = false;
103 $filename = basename($photo);
104 $img_str = fetch_url($photo, true);
106 if ($quit_on_error && ($img_str == "")) {
110 $type = Image::guessType($photo, true);
111 $Image = new Image($img_str, $type);
112 if ($Image->isValid()) {
113 $Image->scaleToSquare(175);
115 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
118 $photo_failure = true;
121 $Image->scaleDown(80);
123 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
126 $photo_failure = true;
129 $Image->scaleDown(48);
131 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
134 $photo_failure = true;
137 $suffix = '?ts=' . time();
139 $photo = System::baseUrl() . '/photo/' . $hash . '-4.' . $Image->getExt() . $suffix;
140 $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $Image->getExt() . $suffix;
141 $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $Image->getExt() . $suffix;
143 // Remove the cached photo
145 $basepath = $a->get_basepath();
147 if (is_dir($basepath . "/photo")) {
148 $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
149 if (file_exists($filename)) {
152 $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
153 if (file_exists($filename)) {
156 $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
157 if (file_exists($filename)) {
162 $photo_failure = true;
165 if ($photo_failure && $quit_on_error) {
169 if ($photo_failure) {
170 $photo = System::baseUrl() . '/images/person-175.jpg';
171 $thumb = System::baseUrl() . '/images/person-80.jpg';
172 $micro = System::baseUrl() . '/images/person-48.jpg';
175 return array($photo, $thumb, $micro);
179 * @param string $exifCoord coordinate
180 * @param string $hemi hemi
183 public static function getGps($exifCoord, $hemi)
185 $degrees = count($exifCoord) > 0 ? self::gps2Num($exifCoord[0]) : 0;
186 $minutes = count($exifCoord) > 1 ? self::gps2Num($exifCoord[1]) : 0;
187 $seconds = count($exifCoord) > 2 ? self::gps2Num($exifCoord[2]) : 0;
189 $flip = ($hemi == 'W' || $hemi == 'S') ? -1 : 1;
191 return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
195 * @param string $coordPart coordPart
198 private static function gps2Num($coordPart)
200 $parts = explode('/', $coordPart);
202 if (count($parts) <= 0) {
206 if (count($parts) == 1) {
210 return floatval($parts[0]) / floatval($parts[1]);
214 * @brief Fetch the photo albums that are available for a viewer
216 * The query in this function is cost intensive, so it is cached.
218 * @param int $uid User id of the photos
219 * @param bool $update Update the cache
221 * @return array Returns array of the photo albums
223 public static function getAlbums($uid, $update = false)
225 $sql_extra = permissions_sql($uid);
227 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
228 $albums = Cache::get($key);
229 if (is_null($albums) || $update) {
230 if (!Config::get('system', 'no_count', false)) {
231 /// @todo This query needs to be renewed. It is really slow
232 // At this time we just store the data in the cache
233 $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
235 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
236 GROUP BY `album` ORDER BY `created` DESC",
238 dbesc('Contact Photos'),
239 dbesc(t('Contact Photos'))
242 // This query doesn't do the count and is much faster
243 $albums = q("SELECT DISTINCT(`album`), '' AS `total`
244 FROM `photo` USE INDEX (`uid_album_scale_created`)
245 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra",
247 dbesc('Contact Photos'),
248 dbesc(t('Contact Photos'))
251 Cache::set($key, $albums, CACHE_DAY);
257 * @param int $uid User id of the photos
260 public static function clearAlbumCache($uid)
262 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
263 Cache::set($key, null, CACHE_DAY);