3 * @file mod/profile_photo.php
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
12 use Friendica\Model\Contact;
13 use Friendica\Model\Photo;
14 use Friendica\Model\Profile;
15 use Friendica\Object\Image;
16 use Friendica\Util\DateTimeFormat;
18 function profile_photo_init(App $a)
24 Profile::load($a, $a->user['nickname']);
27 function profile_photo_post(App $a) {
30 notice(L10n::t('Permission denied.') . EOL );
34 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
36 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
38 // unless proven otherwise
39 $is_default_profile = 1;
41 if($_REQUEST['profile']) {
42 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
43 intval($_REQUEST['profile']),
46 if (DBM::is_result($r) && (! intval($r[0]['is-default'])))
47 $is_default_profile = 0;
52 // phase 2 - we have finished cropping
55 notice(L10n::t('Image uploaded but image cropping failed.') . EOL );
59 $image_id = $a->argv[1];
61 if(substr($image_id,-2,1) == '-') {
62 $scale = substr($image_id,-1,1);
63 $image_id = substr($image_id,0,-2);
67 $srcX = $_POST['xstart'];
68 $srcY = $_POST['ystart'];
69 $srcW = $_POST['xfinal'] - $srcX;
70 $srcH = $_POST['yfinal'] - $srcY;
72 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
77 if (DBM::is_result($r)) {
81 $Image = new Image($base_image['data'], $base_image['type']);
82 if ($Image->isValid()) {
83 $Image->crop(175,$srcX,$srcY,$srcW,$srcH);
85 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'],$base_image['filename'], L10n::t('Profile Photos'), 4, $is_default_profile);
88 notice(L10n::t('Image size reduction [%s] failed.', "175") . EOL);
91 $Image->scaleDown(80);
93 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'],$base_image['filename'], L10n::t('Profile Photos'), 5, $is_default_profile);
96 notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
99 $Image->scaleDown(48);
101 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'],$base_image['filename'], L10n::t('Profile Photos'), 6, $is_default_profile);
104 notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
107 // If setting for the default profile, unset the profile photo flag from any other photos I own
109 if ($is_default_profile) {
110 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
111 dbesc($base_image['resource-id']),
115 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
116 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
117 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
118 intval($_REQUEST['profile']),
123 Contact::updateSelfFromUserID(local_user(), true);
125 info(L10n::t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
126 // Update global directory in background
127 $url = System::baseUrl() . '/profile/' . $a->user['nickname'];
128 if ($url && strlen(Config::get('system','directory'))) {
129 Worker::add(PRIORITY_LOW, "Directory", $url);
132 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
134 notice(L10n::t('Unable to process image') . EOL);
138 goaway(System::baseUrl() . '/profiles');
139 return; // NOTREACHED
142 $src = $_FILES['userfile']['tmp_name'];
143 $filename = basename($_FILES['userfile']['name']);
144 $filesize = intval($_FILES['userfile']['size']);
145 $filetype = $_FILES['userfile']['type'];
146 if ($filetype == "") {
147 $filetype = Image::guessType($filename);
150 $maximagesize = Config::get('system', 'maximagesize');
152 if (($maximagesize) && ($filesize > $maximagesize)) {
153 notice(L10n::t('Image exceeds size limit of %s', formatBytes($maximagesize)) . EOL);
158 $imagedata = @file_get_contents($src);
159 $ph = new Image($imagedata, $filetype);
161 if (! $ph->isValid()) {
162 notice(L10n::t('Unable to process image.') . EOL);
169 return profile_photo_crop_ui_head($a, $ph);
173 function profile_photo_content(App $a) {
175 if (! local_user()) {
176 notice(L10n::t('Permission denied.') . EOL );
182 if($a->argc == 2 && $a->argv[1] === 'new')
185 if( $a->argv[1]=='use'){
187 notice(L10n::t('Permission denied.') . EOL );
191 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
193 $resource_id = $a->argv[2];
194 //die(":".local_user());
195 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
196 intval(local_user()),
199 if (!DBM::is_result($r)){
200 notice(L10n::t('Permission denied.') . EOL );
204 foreach ($r as $rr) {
205 if($rr['scale'] == 5)
209 // set an already uloaded photo as profile photo
210 // if photo is in 'Profile Photos', change it in db
211 if (($r[0]['album']== L10n::t('Profile Photos')) && ($havescale)){
212 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
213 intval(local_user()));
215 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
216 intval(local_user()),
220 Contact::updateSelfFromUserID(local_user(), true);
222 // Update global directory in background
223 $url = $_SESSION['my_url'];
224 if ($url && strlen(Config::get('system','directory'))) {
225 Worker::add(PRIORITY_LOW, "Directory", $url);
228 goaway(System::baseUrl() . '/profiles');
229 return; // NOTREACHED
231 $ph = new Image($r[0]['data'], $r[0]['type']);
232 profile_photo_crop_ui_head($a, $ph);
233 // go ahead as we have jus uploaded a new photo to crop
236 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
241 if(! x($a->config,'imagecrop')) {
243 $tpl = get_markup_template('profile_photo.tpl');
245 $o = replace_macros($tpl,[
246 '$user' => $a->user['nickname'],
247 '$lbl_upfile' => L10n::t('Upload File:'),
248 '$lbl_profiles' => L10n::t('Select a profile:'),
249 '$title' => L10n::t('Upload Profile Photo'),
250 '$submit' => L10n::t('Upload'),
251 '$profiles' => $profiles,
252 '$form_security_token' => get_form_security_token("profile_photo"),
253 '$select' => sprintf('%s %s', L10n::t('or'), ($newuser) ? '<a href="' . System::baseUrl() . '">' . L10n::t('skip this step') . '</a>' : '<a href="'. System::baseUrl() . '/photos/' . $a->user['nickname'] . '">' . L10n::t('select a photo from your photo albums') . '</a>')
259 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
260 $tpl = get_markup_template("cropbody.tpl");
261 $o = replace_macros($tpl,[
262 '$filename' => $filename,
263 '$profile' => intval($_REQUEST['profile']),
264 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
265 '$image_url' => System::baseUrl() . '/photo/' . $filename,
266 '$title' => L10n::t('Crop Image'),
267 '$desc' => L10n::t('Please adjust the image cropping for optimum viewing.'),
268 '$form_security_token' => get_form_security_token("profile_photo"),
269 '$done' => L10n::t('Done Editing')
274 return; // NOTREACHED
278 function profile_photo_crop_ui_head(App $a, Image $Image) {
279 $max_length = Config::get('system','max_image_length');
281 $max_length = MAX_IMAGE_LENGTH;
283 if ($max_length > 0) {
284 $Image->scaleDown($max_length);
287 $width = $Image->getWidth();
288 $height = $Image->getHeight();
290 if ($width < 175 || $height < 175) {
291 $Image->scaleUp(200);
292 $width = $Image->getWidth();
293 $height = $Image->getHeight();
296 $hash = Photo::newResource();
302 $r = Photo::store($Image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
305 info(L10n::t('Image uploaded successfully.') . EOL);
307 notice(L10n::t('Image upload failed.') . EOL);
310 if ($width > 640 || $height > 640) {
311 $Image->scaleDown(640);
312 $r = Photo::store($Image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
315 notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
321 $a->config['imagecrop'] = $hash;
322 $a->config['imagecrop_resolution'] = $smallest;
323 $a->config['imagecrop_ext'] = $Image->getExt();
324 $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), []);
325 $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), []);