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