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