troubleshooting Question

get filename from base64 decoded image

Avatar of KoenVDS
KoenVDS asked on
PHP
3 Comments1 Solution9421 ViewsLast Modified:
I am currently working on a couple of functions that process a http posted XML document. In that document, there is a tag "image", which contains a base64 encoded string, holding the image information. i have come to the point where i can decode the base64 string, and display the image on screen.

next i want to build in some security checks, i.e. making sure the base64 string is indeed an image file. Therefor i have written a small function that checks for an imagetype, which returns false when no valid type was found. Because of security reasons, i would prefer to keep the base64 DECODED variable in memory, or at most in a tmpfile();  (temporary file) and not to store it on harddisk before manipulating it further. The final goal of the php routine is to process the xml and maniuplate the sent images into different sizes (thumbnail, medium, large, ...) ,store those to a harddrive location and insert the link (filename) into a database table.

The problem i am currently having is applying the checks for imagetype, because it looks up the fileproperties by use of a filename, which i dont have. i am also anticipating this will cause further problems when trying to resize the image, because it does not exist physically on a harddisk location, only in memory

Is it possible to extract a filename from the base64 decoded image, so i dont have to store it to a location first, and will it be possible to further manipulate the 'image' ?

The following code snippet is what i have at this point. I have to admit i'm only programming in PHP for 2 days now, but i've been programming for 4 years so far, so please don't spare me some technical details.

many thanks in advance for a quick reply.
function processImages($imageItems)
{ // Verifies base64 imagecode and stores in database
	global $authenticationInfo;
	foreach($imageItems->getElementsByTagName('image') as $imageItem) // go through imageItems
	{
		writeToLog(" -- PROCESSING IMAGE INITIATED -- User: (UID ".$authenticationInfo->getElementsByTagName('user')->item(0)->getAttribute('id').")");
		if(!base64_decode($imageItem->nodeValue)){
			writeToLog(" --- PROCESSING IMAGE FAILED -- User: (UID ".$authenticationInfo->getElementsByTagName('user')->item(0)->getAttribute('id').") - Invalid base64 characterset found!");
		}else{
			$decodedImage = base64_decode($imageItem->nodeValue);
			$decodedImageType = $imageItem->getAttribute("type");
			$tmpFile = tmpfile();
			
			file_put_contents('testFile.jpg', $decodedImage);
			echo $tmpFile;
			
			if(false) //!exif_imagetype($decodedImage)
				writeToLog(" --- PROCESSING IMAGE FAILED -- USER: (UID ".$authenticationInfo->getElementsByTagName('user')->item(0)->getAttribute('id').") - Base64 characterset is not a valid image file!");
			else{
				// store image to disk
				//echo "its a done deal, sis'";
			}
		}
		writeToLog(" -- PROCESSING IMAGE TERMINATED -- User: (UID ".$authenticationInfo->getElementsByTagName('user')->item(0)->getAttribute('id').")");
	}
	unset($imageItem); // clear memory resources
}
function exif_imagetype ( $filename ) {
	if ( ( list($width, $height, $type, $attr) = getimagesize( $filename ) ) !== false ) {
		return $type;
	}
	return false;
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros