4 * @file src/Model/Photo.php
5 * @brief This file contains the Photo class for database interface
8 namespace Friendica\Model;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
12 use Friendica\Object\Image;
15 require_once "include/photos.php";
17 * Class to handle photo dabatase table
22 * @param integer $uid uid
23 * @param integer $cid cid
24 * @param integer $rid rid
25 * @param string $filename filename
26 * @param string $album album name
27 * @param integer $scale scale
28 * @param integer $profile optional, default = 0
29 * @param string $allow_cid optional, default = ''
30 * @param string $allow_gid optional, default = ''
31 * @param string $deny_cid optional, default = ''
32 * @param string $deny_gid optional, default = ''
33 * @param string $desc optional, default = ''
36 public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
38 $r = dba::select('photo', array('guid'), array("`resource-id` = ? AND `guid` != ?", $rid, ''), array('limit' => 1));
39 if (DBM::is_result($r)) {
45 $x = dba::select('photo', array('id'), array('resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale), array('limit' => 1));
51 'resource-id' => $rid,
52 'created' => datetime_convert(),
53 'edited' => datetime_convert(),
54 'filename' => basename($filename),
55 'type' => $Image->getType(),
57 'height' => $Image->getHeight(),
58 'width' => $Image->getWidth(),
59 'datasize' => strlen($Image->asString()),
60 'data' => $Image->asString(),
62 'profile' => $profile,
63 'allow_cid' => $allow_cid,
64 'allow_gid' => $allow_gid,
65 'deny_cid' => $deny_cid,
66 'deny_gid' => $deny_gid,
70 if (DBM::is_result($x)) {
71 $r = dba::update('photo', $fields, array('id' => $x['id']));
73 $r = dba::insert('photo', $fields);
80 * @param string $photo photo
81 * @param integer $uid user id
82 * @param integer $cid contact id
83 * @param boolean $quit_on_error optional, default false
86 public static function importProfilePhoto($photo, $uid, $cid, $quit_on_error = false)
89 'photo', array('resource-id'), array('uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos'), array('limit' => 1)
92 if (DBM::is_result($r) && strlen($r['resource-id'])) {
93 $hash = $r['resource-id'];
95 $hash = photo_new_resource();
98 $photo_failure = false;
100 $filename = basename($photo);
101 $img_str = fetch_url($photo, true);
103 if ($quit_on_error && ($img_str == "")) {
107 $type = Image::guessType($photo, true);
108 $Image = new Image($img_str, $type);
109 if ($Image->isValid()) {
110 $Image->scaleToSquare(175);
112 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
115 $photo_failure = true;
118 $Image->scaleDown(80);
120 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
123 $photo_failure = true;
126 $Image->scaleDown(48);
128 $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
131 $photo_failure = true;
134 $suffix = '?ts=' . time();
136 $photo = System::baseUrl() . '/photo/' . $hash . '-4.' . $Image->getExt() . $suffix;
137 $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $Image->getExt() . $suffix;
138 $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $Image->getExt() . $suffix;
140 // Remove the cached photo
142 $basepath = $a->get_basepath();
144 if (is_dir($basepath . "/photo")) {
145 $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
146 if (file_exists($filename)) {
149 $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
150 if (file_exists($filename)) {
153 $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
154 if (file_exists($filename)) {
159 $photo_failure = true;
162 if ($photo_failure && $quit_on_error) {
166 if ($photo_failure) {
167 $photo = System::baseUrl() . '/images/person-175.jpg';
168 $thumb = System::baseUrl() . '/images/person-80.jpg';
169 $micro = System::baseUrl() . '/images/person-48.jpg';
172 return array($photo, $thumb, $micro);