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'),
93 $_SESSION['sysmsg'] .= "Failed to add image to profile." . EOL;
96 goaway($a->get_baseurl() . '/profiles');
99 $extra_sql = (($profile_id) ? " AND `id` = " . intval($profile_id) : " AND `is-default` = 1 " );
102 $r = q("SELECT `id` FROM `profile` WHERE `uid` = %d $extra_sql LIMIT 1", intval($_SESSION['uid']));
103 if($r === NULL || (! count($r))) {
104 $_SESSION['sysmsg'] .= "Profile unavailable." . EOL;
108 $src = $_FILES['userfile']['tmp_name'];
109 $filename = basename($_FILES['userfile']['name']);
110 $filesize = intval($_FILES['userfile']['size']);
112 $imagedata = @file_get_contents($src);
113 $ph = new Photo($imagedata);
115 if(! ($image = $ph->getImage())) {
116 $_SESSION['sysmsg'] .= "Unable to process image." . EOL;
122 $width = $ph->getWidth();
123 $height = $ph->getHeight();
125 if($width < 175 || $width < 175) {
126 $ph->scaleImageUp(200);
127 $width = $ph->getWidth();
128 $height = $ph->getHeight();
131 $hash = hash('md5',uniqid(mt_rand(),true));
133 $str_image = $ph->imageString();
135 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
136 `height`, `width`, `data`, `scale` )
137 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 0 )",
138 intval($_SESSION['uid']),
142 dbesc(basename($filename)),
147 $_SESSION['sysmsg'] .= "Image uploaded successfully." . EOL;
149 $_SESSION['sysmsg'] .= "Image upload failed." . EOL;
151 if($width > 640 || $height > 640) {
152 $ph->scaleImage(640);
153 $str_image = $ph->imageString();
154 $width = $ph->getWidth();
155 $height = $ph->getHeight();
157 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
158 `height`, `width`, `data`, `scale` )
159 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 1 )",
160 intval($_SESSION['uid']),
164 dbesc(basename($filename)),
169 $_SESSION['sysmsg'] .= "Image size reduction (640) failed." . EOL;
174 $a->config['imagecrop'] = $hash;
175 $a->config['imagecrop_resolution'] = $smallest;
176 $a->page['htmlhead'] .= file_get_contents("view/crophead.tpl");
181 if(! function_exists('profile_photo_content')) {
182 function profile_photo_content(&$a) {
185 if(! x($a->config,'imagecrop')) {
186 if((! x($_SESSION['authenticated'])) && (! (x($_SESSION,'uid')))) {
187 $_SESSION['sysmsg'] .= "Permission denied." . EOL;
192 $profile_id = intval($a->argv[1]);
194 $extra_sql = (($profile_id) ? " AND `id` = $profile_id " : " AND `is-default` = 1 " );
197 $r = q("SELECT `id` FROM `profile` WHERE `uid` = %d $extra_sql LIMIT 1", intval($_SESSION['uid']));
198 if($r === NULL || (! count($r))) {
199 $_SESSION['sysmsg'] .= "Profile unavailable." . EOL;
203 $o = file_get_contents('view/profile_photo.tpl');
205 $o = replace_macros($o,array(
206 '$profile_id' => $r[0]['id'],
207 '$uid' => $_SESSION['uid'],
213 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
214 $resolution = $a->config['imagecrop_resolution'];
215 $o = file_get_contents("view/cropbody.tpl");
216 $o = replace_macros($o,array(
217 '$filename' => $filename,
218 '$profile_id' => $a->argv[1],
219 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
220 '$image_url' => $a->get_baseurl() . '/photo/' . $filename