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/dba.php';
16 require_once "include/photos.php";
19 * Class to handle photo dabatase table
24 * @param integer $uid uid
25 * @param integer $cid cid
26 * @param integer $rid rid
27 * @param string $filename filename
28 * @param string $album album name
29 * @param integer $scale scale
30 * @param integer $profile optional, default = 0
31 * @param string $allow_cid optional, default = ''
32 * @param string $allow_gid optional, default = ''
33 * @param string $deny_cid optional, default = ''
34 * @param string $deny_gid optional, default = ''
35 * @param string $desc optional, default = ''
38 public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
40 $r = dba::select('photo', array('guid'), array("`resource-id` = ? AND `guid` != ?", $rid, ''), array('limit' => 1));
41 if (DBM::is_result($r)) {
47 $x = dba::select('photo', array('id'), array('resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale), array('limit' => 1));
53 'resource-id' => $rid,
54 'created' => datetime_convert(),
55 'edited' => datetime_convert(),
56 'filename' => basename($filename),
57 'type' => $Image->getType(),
59 'height' => $Image->getHeight(),
60 'width' => $Image->getWidth(),
61 'datasize' => strlen($Image->asString()),
62 'data' => $Image->asString(),
64 'profile' => $profile,
65 'allow_cid' => $allow_cid,
66 'allow_gid' => $allow_gid,
67 'deny_cid' => $deny_cid,
68 'deny_gid' => $deny_gid,
72 if (DBM::is_result($x)) {
73 $r = dba::update('photo', $fields, array('id' => $x['id']));
75 $r = dba::insert('photo', $fields);
82 * @param string $photo photo
83 * @param integer $uid user id
84 * @param integer $cid contact id
85 * @param boolean $quit_on_error optional, default false
88 public static function importProfilePhoto($photo, $uid, $cid, $quit_on_error = false)
91 'photo', array('resource-id'), array('uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos'), array('limit' => 1)
94 if (DBM::is_result($r) && strlen($r['resource-id'])) {
95 $hash = $r['resource-id'];
97 $hash = photo_new_resource();
100 $photo_failure = false;
102 $filename = basename($photo);
103 $img_str = fetch_url($photo, true);
105 if ($quit_on_error && ($img_str == "")) {
109 $type = Image::guessType($photo, 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 $photo = 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 $photo = 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($photo, $thumb, $micro);