]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
Merge branch 'friendika-master'
[friendica.git] / mod / profile_photo.php
1 <?php
2
3 require_once("Photo.php");
4
5 function profile_photo_init(&$a) {
6
7         if(! local_user()) {
8                 return;
9         }
10
11         profile_load($a,$a->user['nickname']);
12
13 }
14
15
16 function profile_photo_post(&$a) {
17
18         if(! local_user()) {
19                 notice ( t('Permission denied.') . EOL );
20                 return;
21         }
22
23         if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
24
25                 // phase 2 - we have finished cropping
26
27                 if($a->argc != 2) {
28                         notice( t('Image uploaded but image cropping failed.') . EOL );
29                         return;
30                 }
31
32                 $image_id = $a->argv[1];
33
34                 if(substr($image_id,-2,1) == '-') {
35                         $scale = substr($image_id,-1,1);
36                         $image_id = substr($image_id,0,-2);
37                 }
38                         
39
40                 $srcX = $_POST['xstart'];
41                 $srcY = $_POST['ystart'];
42                 $srcW = $_POST['xfinal'] - $srcX;
43                 $srcH = $_POST['yfinal'] - $srcY;
44
45                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
46                         dbesc($image_id),
47                         dbesc(local_user()),
48                         intval($scale));
49
50                 if(count($r)) {
51
52                         $base_image = $r[0];
53
54                         $im = new Photo($base_image['data']);
55                         if($im->is_valid()) {
56                                 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
57
58                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, 1);
59
60                                 if($r === false)
61                                         notice ( t('Image size reduction [175] failed.') . EOL );
62
63                                 $im->scaleImage(80);
64
65                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, 1);
66                         
67                                 if($r === false)
68                                         notice( t('Image size reduction [80] failed.') . EOL );
69
70                                 $im->scaleImage(48);
71
72                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, 1);
73                         
74                                 if($r === false)
75                                         notice( t('Image size reduction [48] failed.') . EOL );
76
77                                 // Unset the profile photo flag from any other photos I own
78
79                                 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
80                                         dbesc($base_image['resource-id']),
81                                         intval(local_user())
82                                 );
83
84                                 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
85                                         dbesc(datetime_convert()),
86                                         intval(local_user())
87                                 );
88
89                                 // Update global directory in background
90                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
91                                 $url = $_SESSION['my_url'];
92                                 if($url && strlen(get_config('system','directory_submit_url')))
93                                         //proc_close(proc_open("\"$php_path\" \"include/directory.php\" \"$url\" &",array(),$foo));
94                                         proc_run($php_path,"include/directory.php","$url");
95                         }
96                         else
97                                 notice( t('Unable to process image') . EOL);
98                 }
99
100                 goaway($a->get_baseurl() . '/profiles');
101                 return; // NOTREACHED
102         }
103
104         $src      = $_FILES['userfile']['tmp_name'];
105         $filename = basename($_FILES['userfile']['name']);
106         $filesize = intval($_FILES['userfile']['size']);
107
108         $maximagesize = get_config('system','maximagesize');
109
110         if(($maximagesize) && ($filesize > $maximagesize)) {
111                 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
112                 @unlink($src);
113                 return;
114         }
115
116         $imagedata = @file_get_contents($src);
117         $ph = new Photo($imagedata);
118
119         if(! $ph->is_valid()) {
120                 notice( t('Unable to process image.') . EOL );
121                 @unlink($src);
122                 return;
123         }
124
125         @unlink($src);
126
127         $width = $ph->getWidth();
128         $height = $ph->getHeight();
129
130         if($width < 175 || $height < 175) {
131                 $ph->scaleImageUp(200);
132                 $width = $ph->getWidth();
133                 $height = $ph->getHeight();
134         }
135
136         $hash = photo_new_resource();
137         
138
139         $smallest = 0;
140
141         $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );   
142
143         if($r)
144                 notice( t('Image uploaded successfully.') . EOL );
145         else
146                 notice( t('Image upload failed.') . EOL );
147
148         if($width > 640 || $height > 640) {
149                 $ph->scaleImage(640);
150                 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );   
151
152                 if($r === false)
153                         notice( t('Image size reduction [640] failed.') . EOL );
154                 else
155                         $smallest = 1;
156         }
157
158         $a->config['imagecrop'] = $hash;
159         $a->config['imagecrop_resolution'] = $smallest;
160         $a->page['htmlhead'] .= load_view_file("view/crophead.tpl");
161         return;
162 }
163
164
165 if(! function_exists('profile_photo_content')) {
166 function profile_photo_content(&$a) {
167
168         if(! local_user()) {
169                 notice( t('Permission denied.') . EOL );
170                 return;
171         }
172
173         if(! x($a->config,'imagecrop')) {
174         
175                 $tpl = load_view_file('view/profile_photo.tpl');
176
177                 $o .= replace_macros($tpl,array(
178
179                 ));
180
181                 return $o;
182         }
183         else {
184                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
185                 $resolution = $a->config['imagecrop_resolution'];
186                 $tpl = load_view_file("view/cropbody.tpl");
187                 $o .= replace_macros($tpl,array(
188                         '$filename' => $filename,
189                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
190                         '$image_url' => $a->get_baseurl() . '/photo/' . $filename
191                         ));
192
193                 return $o;
194         }
195
196         return; // NOTREACHED
197 }}