Link to home
Start Free TrialLog in
Avatar of KoenVDS
KoenVDS

asked on

get filename from base64 decoded image

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;
}

Open in new window

SOLUTION
Avatar of rohypnol
rohypnol
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You cannot extract the filename from an image's contents, sorry.