3 require_once("Photo.php");
5 function profile_photo_init(&$a) {
7 if((! x($_SESSION,'authenticated')) && (x($_SESSION,'uid'))) {
8 $_SESSION['sysmsg'] .= "Permission denied." . EOL;
12 require_once("mod/profile.php");
13 profile_load($a,$_SESSION['uid']);
17 function profile_photo_post(&$a) {
21 if((! x($_SESSION,'authenticated')) && (! (x($_SESSION,'uid')))) {
22 $_SESSION['sysmsg'] .= "Permission denied." . EOL;
27 $profile_id = intval($a->argv[1]);
29 if(x($_POST,'xstart') !== false) {
30 // phase 2 - we have finished cropping
32 $_SESSION['sysmsg'] .= "Image uploaded but image cropping failed." . EOL;
35 $image_id = $a->argv[2];
36 if(substr($image_id,-2,1) == '-') {
37 $scale = substr($image_id,-1,1);
38 $image_id = substr($image_id,0,-2);
42 $srcX = $_POST['xstart'];
43 $srcY = $_POST['ystart'];
44 $srcW = $_POST['xfinal'] - $srcX;
45 $srcH = $_POST['yfinal'] - $srcY;
47 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d LIMIT 1",
50 if($r !== NULL && (count($r))) {
51 $im = new Photo($r[0]['data']);
52 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
53 $s = $im->imageString();
55 $y = $im->getHeight();
57 $ret = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
58 `height`, `width`, `data`, `scale` )
59 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 4 )",
60 intval($_SESSION['uid']),
61 dbesc($r[0]['resource-id']),
64 dbesc($r[0]['filename']),
69 $_SESSION['sysmsg'] .= "Image size reduction (175) failed." . EOL;
72 $s = $im->imageString();
74 $y = $im->getHeight();
75 $ret = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
76 `height`, `width`, `data`, `scale` )
77 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 5 )",
78 intval($_SESSION['uid']),
79 dbesc($r[0]['resource-id']),
82 dbesc($r[0]['filename']),
87 $_SESSION['sysmsg'] .= "Image size reduction (80) failed." . EOL;
88 $r = q("UPDATE `profile` SET `photo` = '%s', `thumb` = '%s' WHERE `id` = %d LIMIT 1",
89 dbesc($a->get_baseurl() . '/photo/' . $image_id . '-4.jpg'),
90 dbesc($a->get_baseurl() . '/photo/' . $image_id . '-5.jpg'),
94 $_SESSION['sysmsg'] .= "Failed to add image to profile." . EOL;
95 // We should really only do this if requested, or if it is the default profile. The contact
96 //table images are used in coversations.
97 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s' WHERE `uid` = %d AND `self` = 1 LIMIT 1",
98 dbesc($a->get_baseurl() . '/photo/' . $image_id . '-4.jpg'),
99 dbesc($a->get_baseurl() . '/photo/' . $image_id . '-5.jpg'),
100 intval($_SESSION['uid'])
103 notice("Failed to add photo to contact table." .EOL );
106 goaway($a->get_baseurl() . '/profiles');
109 $extra_sql = (($profile_id) ? " AND `id` = " . intval($profile_id) : " AND `is-default` = 1 " );
112 $r = q("SELECT `id` FROM `profile` WHERE `uid` = %d $extra_sql LIMIT 1", intval($_SESSION['uid']));
113 if($r === NULL || (! count($r))) {
114 $_SESSION['sysmsg'] .= "Profile unavailable." . EOL;
118 $src = $_FILES['userfile']['tmp_name'];
119 $filename = basename($_FILES['userfile']['name']);
120 $filesize = intval($_FILES['userfile']['size']);
122 $imagedata = @file_get_contents($src);
123 $ph = new Photo($imagedata);
125 if(! ($image = $ph->getImage())) {
126 $_SESSION['sysmsg'] .= "Unable to process image." . EOL;
132 $width = $ph->getWidth();
133 $height = $ph->getHeight();
135 if($width < 175 || $width < 175) {
136 $ph->scaleImageUp(200);
137 $width = $ph->getWidth();
138 $height = $ph->getHeight();
141 $hash = hash('md5',uniqid(mt_rand(),true));
143 $str_image = $ph->imageString();
145 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
146 `height`, `width`, `data`, `scale` )
147 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 0 )",
148 intval($_SESSION['uid']),
152 dbesc(basename($filename)),
157 $_SESSION['sysmsg'] .= "Image uploaded successfully." . EOL;
159 $_SESSION['sysmsg'] .= "Image upload failed." . EOL;
161 if($width > 640 || $height > 640) {
162 $ph->scaleImage(640);
163 $str_image = $ph->imageString();
164 $width = $ph->getWidth();
165 $height = $ph->getHeight();
167 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
168 `height`, `width`, `data`, `scale` )
169 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 1 )",
170 intval($_SESSION['uid']),
174 dbesc(basename($filename)),
179 $_SESSION['sysmsg'] .= "Image size reduction (640) failed." . EOL;
184 $a->config['imagecrop'] = $hash;
185 $a->config['imagecrop_resolution'] = $smallest;
186 $a->page['htmlhead'] .= file_get_contents("view/crophead.tpl");
191 if(! function_exists('profile_photo_content')) {
192 function profile_photo_content(&$a) {
195 if(! x($a->config,'imagecrop')) {
196 if((! x($_SESSION['authenticated'])) && (! (x($_SESSION,'uid')))) {
197 $_SESSION['sysmsg'] .= "Permission denied." . EOL;
202 $profile_id = intval($a->argv[1]);
204 $extra_sql = (($profile_id) ? " AND `id` = $profile_id " : " AND `is-default` = 1 " );
207 $r = q("SELECT `id` FROM `profile` WHERE `uid` = %d $extra_sql LIMIT 1", intval($_SESSION['uid']));
208 if($r === NULL || (! count($r))) {
209 $_SESSION['sysmsg'] .= "Profile unavailable." . EOL;
213 $o = file_get_contents('view/profile_photo.tpl');
215 $o = replace_macros($o,array(
216 '$profile_id' => $r[0]['id'],
217 '$uid' => $_SESSION['uid'],
223 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
224 $resolution = $a->config['imagecrop_resolution'];
225 $o = file_get_contents("view/cropbody.tpl");
226 $o = replace_macros($o,array(
227 '$filename' => $filename,
228 '$profile_id' => $a->argv[1],
229 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
230 '$image_url' => $a->get_baseurl() . '/photo/' . $filename