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