We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26310
    • 130 Posts
    Hello Everybody!

    Ok so I need to have up to 3 images associated with a user. I know this is a pretty big task but I figured if I take it one step at a time maybe I could do it with a little help. First of all we need to upload and handle 3 images. So on line 571 of the webloginpe file I found where we get the function call...
    (this is for registering we will tackle editing the profile when we HAVE to:
    		// Create the user image if necessary.
    		if (!empty($_FILES['photo']['name']))
    		{
    			$photo = $this->CreateUserImage();
    			if (!empty($this->Report))
    			{
    				return;
    			}
    		}
    		else
    		{
    			$photo = $modx->config['site_url'].'assets/snippets/webloginpe/userimages/default_user.jpg';
    		}
    


    To me if we just repeated this conditional statement with using another file field called photo2 we could simply run that function again giving a different file?

    Now this is the part that gets me. Can you put multiple conditions within a switch statement?

     * @return string A URL to the user image created.
    	 * @author Scotty Delicious
    	 */
    	function CreateUserImage()
    	{
    		global $modx;
    		
    		$imageAttributes = str_replace(', ', ',', $this->UserImageSettings);
    		$imageAttributes = explode(',', $imageAttributes);
    		
    		if ($_FILES['photo']['size'] >= $imageAttributes[0])
    		{
    			$sizeInKb = round($imageAttributes[0] / 1024);
    			$sizeError = str_replace('[+000+]', $sizeInKb, $this->LanguageArray[28]);
    			return $this->FormatMessage($sizeError);
    		}
    		
    		$userImage = $modx->config['base_path'].strtolower(str_replace(' ', '-', basename( $_FILES['photo']['name'])));
    		if (!move_uploaded_file($_FILES['photo']['tmp_name'], $userImage))
    		{
    			return $this->FormatMessage($this->LanguageArray[29]);
    		}
    		
    		// License and registration ma'am. I need to se an ID!
    		if ($modx->getLoginUserID())
    		{
    			$currentWebUser = $modx->getWebUserInfo($modx->getLoginUserID());
    			if ($this->Type == 'manager')
    			{
    				$currentWebUser['username'] = $_POST['username'];
    			}
    		}
    		else
    		{
    			$currentWebUser['username'] = $this->Username;
    			if ($this->Username == '' || empty($this->Username))
    			{
    				$currentWebUser['username'] = $_POST['username'];
    			}
    		}
    		
    		// Get dimensions and set new ones.
    		list($width, $height) = getimagesize($userImage);
    		$new_width = $imageAttributes[1];
    		$new_height = $imageAttributes[2];
    		
    		$wm = $width / $new_width;
    		$hm = $height / $new_height;
    		if ($wm > 1 || $hm > 1) // (don't magnify a smaller image)
    		{
    			if ($wm > $hm) $new_height = $height / $wm;
    			else $new_width = $width / $hm;
    		}
    		else { $new_width = $width; $new_height = $height; } // (must set the original size)
    
    		// Resample
    		$image_p = imagecreatetruecolor($new_width, $new_height);
    		
    		switch ($_FILES['photo']['type'])
    		{
    			case 'image/jpeg':
    			case 'image/jpg': // added support for .jpg to the "default" support for .jpeg, so WLPE doesn't give a filetype error
    			case 'image/pjpeg': // fix for IE6, which handles the .jpg filetype incorrectly
    				$image = imagecreatefromjpeg($userImage);
    				$ext = '.jpg';
    				break;
    				
    			case 'image/gif':
    				$image = imagecreatefromgif($userImage);
    				imageSaveAlpha($image, true);
    				imagesavealpha($image_p, true);
    				$trans = imagecolorallocatealpha($image_p,255,255,255,127);
    				imagefill($image_p,0,0,$trans);
    				$ext = '.gif';
    				break;
    				
    			case 'image/png':
    				$image = imagecreatefrompng($userImage);
    				imageSaveAlpha($image, true);
    				imagesavealpha($image_p, true);
    				$trans = imagecolorallocatealpha($image_p,255,255,255,127);
    				imagefill($image_p,0,0,$trans);
    				$ext = '.png';
    				break;
    				
    			default	:
    				return $this->FormatMessage($this->LanguageArray[30]);
    				break;
    		}
    		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    		// Output
    		$userImageFilePath = $modx->config['base_path'].'assets/snippets/webloginpe/userimages/'.str_replace(' ', '_', strtolower($currentWebUser['username'])).$ext;
    		//$userImageFileURL = $modx->config['site_url'].'assets/snippets/webloginpe/userimages/'.str_replace(' ', '_', strtolower($currentWebUser['username'])).$ext;
    		$userImageFileURL = 'assets/snippets/webloginpe/userimages/'.str_replace(' ', '_', strtolower($currentWebUser['username'])).$ext;
    		
    		switch ($_FILES['photo']['type'])
    		{
    			case 'image/jpeg':
    				imagejpeg($image_p, $userImageFilePath, 100);
    				break;
    				
    			case 'image/gif':
    				imagegif($image_p, $userImageFilePath);
    				break;
    				
    			case 'image/png':
    				imagepng($image_p, $userImageFilePath, 0);
    				break;
    				
    			default	:
    				imagejpeg($image_p, $userImageFilePath, 100);
    		}
    		
    		unlink($userImage);
    		
    		return $userImageFileURL;
    	}
    	
    	


    i’m not sure how I could modify this block to handle multiple photos if it’s looking specifically for a file field named ’photo’. this is where I’m lost.

    Around line 601 I see where the values are inserted so I figured if I just add two columns to the user_attributes table and slightly modified this line I could get the locations added correctly....

    $newUserAttr = "INSERT INTO ".$web_user_attributes.
    		" (internalKey, fullname, email, phone, mobilephone, dob, gender, country, state, zip, fax, photo, comment) VALUES".
    		" ('".$key."', '".$fullname."', '".$email."', '".$phone."', '".$mobilephone."', '".$dob."', '".$gender."', '".$country."', '".$state."', '".$zip."', '".$fax."', '".$photo."', '".$comment."')";


    Again if any of what i’m saying fails when reading just let me know please. I’ve been trying to muck around with this but can’t seem to figure out crap! It makes sense to me I just don’t have the programming background to make it work....

    what if we put the photos in an array and looped the function? Another thing is the best way to name the files....

    I was thinking of appending a timestamp to the filename... any thoughts? so if your username was your email (which my site is setup like that) your file(s) would be [email protected], [email protected]

    Any insight, words of encouragments, criticism, jokes....all or any would be much appreciated. Thanks smiley
      I twitch because I care....and drink too much coffee.
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Switch statements only can have 1 value... if you need more complex criteria, you’d have to write an if/elseif/else block.

      Quick question before you hack this... using WebLoginPE, you can have multiple custom attributes. So you could have a user with attributes for alternate image locations (store just the url), yes? What I don’t remember is whether or not the users can upload files to the server or not... but if you could specify a directory for each user, then you could let them upload the files and rely on the extended attributes to handle the extra images.... OR... just have them reference images hosted on another server (that’s lazy, but hey, then you don’t have to hack anything).