]> git.mxchange.org Git - friendica.git/blob - include/Photo.php
Merge pull request #317 from CatoTH/master
[friendica.git] / include / Photo.php
1 <?php
2
3 if(! class_exists("Photo")) {
4 class Photo {
5
6         private $image;
7         private $width;
8         private $height;
9         private $valid;
10
11         public function __construct($data) {
12                 $this->valid = false;
13                 $this->image = @imagecreatefromstring($data);
14                 if($this->image !== FALSE) {
15                         $this->width  = imagesx($this->image);
16                         $this->height = imagesy($this->image);
17                         $this->valid  = true;
18                 }
19         }
20
21         public function __destruct() {
22                 if($this->image)
23                         imagedestroy($this->image);
24         }
25
26         public function is_valid() {
27                 return $this->valid;
28         }
29
30         public function getWidth() {
31                 return $this->width;
32         }
33
34         public function getHeight() {
35                 return $this->height;
36         }
37
38         public function getImage() {
39                 return $this->image;
40         }
41
42         public function scaleImage($max) {
43
44                 $width = $this->width;
45                 $height = $this->height;
46
47                 $dest_width = $dest_height = 0;
48
49                 if((! $width)|| (! $height))
50                         return FALSE;
51
52                 if($width > $max && $height > $max) {
53                         if($width > $height) {
54                                 $dest_width = $max;
55                                 $dest_height = intval(( $height * $max ) / $width);
56                         }
57                         else {
58                                 $dest_width = intval(( $width * $max ) / $height);
59                                 $dest_height = $max;
60                         }
61                 }
62                 else {
63                         if( $width > $max ) {
64                                 $dest_width = $max;
65                                 $dest_height = intval(( $height * $max ) / $width);
66                         }
67                         else {
68                                 if( $height > $max ) {
69                                         $dest_width = intval(( $width * $max ) / $height);
70                                         $dest_height = $max;
71                                 }
72                                 else {
73                                         $dest_width = $width;
74                                         $dest_height = $height;
75                                 }
76                         }
77                 }
78
79
80                 $dest = imagecreatetruecolor( $dest_width, $dest_height );
81                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
82                 if($this->image)
83                         imagedestroy($this->image);
84                 $this->image = $dest;
85                 $this->width  = imagesx($this->image);
86                 $this->height = imagesy($this->image);
87
88         }
89
90         public function rotate($degrees) {
91                 $this->image  = imagerotate($this->image,$degrees,0);
92                 $this->width  = imagesx($this->image);
93                 $this->height = imagesy($this->image);
94         }       
95
96
97
98         public function scaleImageUp($min) {
99
100                 $width = $this->width;
101                 $height = $this->height;
102
103                 $dest_width = $dest_height = 0;
104
105                 if((! $width)|| (! $height))
106                         return FALSE;
107
108                 if($width < $min && $height < $min) {
109                         if($width > $height) {
110                                 $dest_width = $min;
111                                 $dest_height = intval(( $height * $min ) / $width);
112                         }
113                         else {
114                                 $dest_width = intval(( $width * $min ) / $height);
115                                 $dest_height = $min;
116                         }
117                 }
118                 else {
119                         if( $width < $min ) {
120                                 $dest_width = $min;
121                                 $dest_height = intval(( $height * $min ) / $width);
122                         }
123                         else {
124                                 if( $height < $min ) {
125                                         $dest_width = intval(( $width * $min ) / $height);
126                                         $dest_height = $min;
127                                 }
128                                 else {
129                                         $dest_width = $width;
130                                         $dest_height = $height;
131                                 }
132                         }
133                 }
134
135
136                 $dest = imagecreatetruecolor( $dest_width, $dest_height );
137                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
138                 if($this->image)
139                         imagedestroy($this->image);
140                 $this->image = $dest;
141                 $this->width  = imagesx($this->image);
142                 $this->height = imagesy($this->image);
143
144         }
145
146
147
148         public function scaleImageSquare($dim) {
149
150                 $dest = imagecreatetruecolor( $dim, $dim );
151                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
152                 if($this->image)
153                         imagedestroy($this->image);
154                 $this->image = $dest;
155                 $this->width  = imagesx($this->image);
156                 $this->height = imagesy($this->image);
157         }
158
159
160         public function cropImage($max,$x,$y,$w,$h) {
161                 $dest = imagecreatetruecolor( $max, $max );
162                 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
163                 if($this->image)
164                         imagedestroy($this->image);
165                 $this->image = $dest;
166                 $this->width  = imagesx($this->image);
167                 $this->height = imagesy($this->image);
168         }
169
170         public function saveImage($path) {
171                 $quality = get_config('system','jpeg_quality');
172                 if((! $quality) || ($quality > 100))
173                         $quality = JPEG_QUALITY;
174                 imagejpeg($this->image,$path,$quality);
175         }
176
177         public function imageString() {
178                 ob_start();
179
180                 $quality = get_config('system','jpeg_quality');
181                 if((! $quality) || ($quality > 100))
182                         $quality = JPEG_QUALITY;
183
184                 imagejpeg($this->image,NULL,$quality);
185                 $s = ob_get_contents();
186                 ob_end_clean();
187                 return $s;
188         }
189
190
191
192         public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
193
194                 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
195                         dbesc($rid)
196                 );
197                 if(count($r))
198                         $guid = $r[0]['guid'];
199                 else
200                         $guid = get_guid();
201
202                 $r = q("INSERT INTO `photo`
203                         ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
204                         VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
205                         intval($uid),
206                         intval($cid),
207                         dbesc($guid),
208                         dbesc($rid),
209                         dbesc(datetime_convert()),
210                         dbesc(datetime_convert()),
211                         dbesc(basename($filename)),
212                         dbesc($album),
213                         intval($this->height),
214                         intval($this->width),
215                         dbesc($this->imageString()),
216                         intval($scale),
217                         intval($profile),
218                         dbesc($allow_cid),
219                         dbesc($allow_gid),
220                         dbesc($deny_cid),
221                         dbesc($deny_gid)
222                 );
223                 return $r;
224         }
225
226
227
228
229
230 }}
231
232
233 function import_profile_photo($photo,$uid,$cid) {
234
235         $a = get_app();
236
237         $photo_failure = false;
238
239         $filename = basename($photo);
240         $img_str = fetch_url($photo,true);
241         $img = new Photo($img_str);
242         if($img->is_valid()) {
243
244                 $img->scaleImageSquare(175);
245                                         
246                 $hash = photo_new_resource();
247
248                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
249
250                 if($r === false)
251                         $photo_failure = true;
252
253                 $img->scaleImage(80);
254
255                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
256
257                 if($r === false)
258                         $photo_failure = true;
259
260                 $img->scaleImage(48);
261
262                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
263
264                 if($r === false)
265                         $photo_failure = true;
266
267
268
269                 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
270                 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
271                 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.jpg';
272         }
273         else
274                 $photo_failure = true;
275
276         if($photo_failure) {
277                 $photo = $a->get_baseurl() . '/images/person-175.jpg';
278                 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
279                 $micro = $a->get_baseurl() . '/images/person-48.jpg';
280         }
281
282         return(array($photo,$thumb,$micro));
283
284 }