]> git.mxchange.org Git - friendica.git/blob - src/Model/Photo.php
Merge pull request #4101 from Rudloff/feature/block_api
[friendica.git] / src / Model / Photo.php
1 <?php
2
3 /**
4  * @file src/Model/Photo.php
5  * @brief This file contains the Photo class for database interface
6  */
7
8 namespace Friendica\Model;
9
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
12 use Friendica\Object\Image;
13 use dba;
14
15 require_once 'include/dba.php';
16 require_once "include/photos.php";
17
18 /**
19  * Class to handle photo dabatase table
20  */
21 class Photo
22 {
23         /**
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 = ''
36          * @return object
37          */
38         public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
39         {
40                 $r = dba::select('photo', array('guid'), array("`resource-id` = ? AND `guid` != ?", $rid, ''), array('limit' => 1));
41                 if (DBM::is_result($r)) {
42                         $guid = $r['guid'];
43                 } else {
44                         $guid = get_guid();
45                 }
46
47                 $x = dba::select('photo', array('id'), array('resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale), array('limit' => 1));
48
49                 $fields = array(
50                         'uid' => $uid,
51                         'contact-id' => $cid,
52                         'guid' => $guid,
53                         'resource-id' => $rid,
54                         'created' => datetime_convert(),
55                         'edited' => datetime_convert(),
56                         'filename' => basename($filename),
57                         'type' => $Image->getType(),
58                         'album' => $album,
59                         'height' => $Image->getHeight(),
60                         'width' => $Image->getWidth(),
61                         'datasize' => strlen($Image->asString()),
62                         'data' => $Image->asString(),
63                         'scale' => $scale,
64                         'profile' => $profile,
65                         'allow_cid' => $allow_cid,
66                         'allow_gid' => $allow_gid,
67                         'deny_cid' => $deny_cid,
68                         'deny_gid' => $deny_gid,
69                         'desc' => $desc
70                 );
71
72                 if (DBM::is_result($x)) {
73                         $r = dba::update('photo', $fields, array('id' => $x['id']));
74                 } else {
75                         $r = dba::insert('photo', $fields);
76                 }
77
78                 return $r;
79         }
80
81         /**
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
86          * @return array
87          */
88         public static function importProfilePhoto($photo, $uid, $cid, $quit_on_error = false)
89         {
90                 $r = dba::select(
91                         'photo', array('resource-id'), array('uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos'), array('limit' => 1)
92                 );
93
94                 if (DBM::is_result($r) && strlen($r['resource-id'])) {
95                         $hash = $r['resource-id'];
96                 } else {
97                         $hash = photo_new_resource();
98                 }
99
100                 $photo_failure = false;
101
102                 $filename = basename($photo);
103                 $img_str = fetch_url($photo, true);
104
105                 if ($quit_on_error && ($img_str == "")) {
106                         return false;
107                 }
108
109                 $type = Image::guessType($photo, true);
110                 $Image = new Image($img_str, $type);
111                 if ($Image->isValid()) {
112                         $Image->scaleToSquare(175);
113
114                         $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
115
116                         if ($r === false) {
117                                 $photo_failure = true;
118                         }
119
120                         $Image->scaleDown(80);
121
122                         $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
123
124                         if ($r === false) {
125                                 $photo_failure = true;
126                         }
127
128                         $Image->scaleDown(48);
129
130                         $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
131
132                         if ($r === false) {
133                                 $photo_failure = true;
134                         }
135
136                         $suffix = '?ts=' . time();
137
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;
141
142                         // Remove the cached photo
143                         $a = get_app();
144                         $basepath = $a->get_basepath();
145
146                         if (is_dir($basepath . "/photo")) {
147                                 $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
148                                 if (file_exists($filename)) {
149                                         unlink($filename);
150                                 }
151                                 $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
152                                 if (file_exists($filename)) {
153                                         unlink($filename);
154                                 }
155                                 $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
156                                 if (file_exists($filename)) {
157                                         unlink($filename);
158                                 }
159                         }
160                 } else {
161                         $photo_failure = true;
162                 }
163
164                 if ($photo_failure && $quit_on_error) {
165                         return false;
166                 }
167
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';
172                 }
173
174                 return array($photo, $thumb, $micro);
175         }
176 }