]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
more photo progress
[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 //dbg(3);
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                         $r = $im->store($_SESSION['uid'], 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($_SESSION['uid'], 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                         // Unset the profile photo flag from any other photos I own
71
72                         $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
73                                 dbesc($base_image['resource-id']),
74                                 intval($_SESSION['uid'])
75                         );
76
77                 }
78                 goaway($a->get_baseurl() . '/profiles');
79                 return; // NOTREACHED
80         }
81
82         $src      = $_FILES['userfile']['tmp_name'];
83         $filename = basename($_FILES['userfile']['name']);
84         $filesize = intval($_FILES['userfile']['size']);
85
86         $imagedata = @file_get_contents($src);
87         $ph = new Photo($imagedata);
88
89         if(! ($image = $ph->getImage())) {
90                 notice( t('Unable to process image.') . EOL );
91                 @unlink($src);
92                 return;
93         }
94
95         @unlink($src);
96
97         $width = $ph->getWidth();
98         $height = $ph->getHeight();
99
100         if($width < 175 || $height < 175) {
101                 $ph->scaleImageUp(200);
102                 $width = $ph->getWidth();
103                 $height = $ph->getHeight();
104         }
105
106         $hash = hash('md5',uniqid(mt_rand(),true));
107         
108
109         $smallest = 0;
110
111         $r = $ph->store($_SESSION['uid'], 0 , $hash, $filename, t('Profile Photos'), 0 );       
112
113         if($r)
114                 notice( t('Image uploaded successfully.') . EOL );
115         else
116                 notice( t('Image upload failed.') . EOL );
117
118         if($width > 640 || $height > 640) {
119                 $ph->scaleImage(640);
120                 $r = $ph->store($_SESSION['uid'], 0 , $hash, $filename, t('Profile Photos'), 1 );       
121
122                 if($r === false)
123                         notice( t('Image size reduction (640) failed.') . EOL );
124                 else
125                         $smallest = 1;
126         }
127
128         $a->config['imagecrop'] = $hash;
129         $a->config['imagecrop_resolution'] = $smallest;
130         $a->page['htmlhead'] .= file_get_contents("view/crophead.tpl");
131         return;
132 }
133
134
135 if(! function_exists('profile_photo_content')) {
136 function profile_photo_content(&$a) {
137
138         if(! local_user()) {
139                 notice( t('Permission denied.') . EOL );
140                 return;
141         }
142
143         if(! x($a->config,'imagecrop')) {
144         
145                 $tpl = file_get_contents('view/profile_photo.tpl');
146
147                 $o .= replace_macros($tpl,array(
148
149                 ));
150
151                 return $o;
152         }
153         else {
154                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
155                 $resolution = $a->config['imagecrop_resolution'];
156                 $tpl = file_get_contents("view/cropbody.tpl");
157                 $o .= replace_macros($tpl,array(
158                         '$filename' => $filename,
159                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
160                         '$image_url' => $a->get_baseurl() . '/photo/' . $filename
161                         ));
162
163                 return $o;
164         }
165
166         return; // NOTREACHED
167 }}