
diff --git a/php_scripts/b64img.php b/php_scripts/b64img.php new file mode 100644 index 0000000..5875c10 --- /dev/null +++ b/php_scripts/b64img.php @@ -0,0 +1,179 @@ +reasons = $reasons; + } + public function getReasons() + { + return $this->reasons; + } +} + +if (!empty($_POST['base64'])) +{ + $contents = array(); + $errors = array(); + $inputs = array(); + + $count = count($_POST['base64']); + + for ($i = 0; $i < $count; $i++) + { + $inputs[$i] = array("base64" => $_POST['base64'][$i], "name" => $_POST['names'][$i]); + } + + try + { + foreach($inputs as $input) + { + try + { + $img_decoded = base64_decode($input['base64']); + + if(empty($img_decoded)) + { + throw new DecodeException("Could not get base64 string"); + } + + if (!@imagecreatefromstring($img_decoded)) + { + throw new ImageCreateException(); + } + + $contents[] = array("decoded" => $img_decoded, "name" => $input["name"]); + } + catch (Exception $e) + { + $errors[] = $e; + } + } + + if(!empty($contents)) + { + $count = count($contents); + + if($count == 1) + { + $im = imagecreatefromstring($contents[0]["decoded"]); + + header("Content-Type: image/png"); + header("Cache-Control: no-cache, must-revalidate"); + header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); + imagepng($im); + + } + else + { + $file = tempnam("tmp", "zip"); + + $zip = new ZipArchive(); + $zip->open($file, ZipArchive::OVERWRITE); + + for($i = 0; $i < $count; $i++) + { + $filename = empty($contents[$i]["name"]) ? "image_{$i}" : $contents[$i]["name"]; + $zip->addFromString($filename . ".png", $contents[$i]["decoded"]); + } + + if(!empty($errors)) + { + $errorstring = print_r($errors, true); + + $info = "In total, " . count($errors) . " errors occurred:\r\n\r\n"; + + $info .= "- " . substr_count($errorstring, "DecodeException") . " times, decoding the string didnt work\r\n"; + $info .= "- " . substr_count($errorstring, "ImageCreateException") . " times, creating the image didnt work"; + + $zip->addFromString("errors.txt", $info); + } + + $zip->close(); + + header("Cache-Control: no-cache, must-revalidate"); + header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); + header("Content-Type: application/zip"); + header("Content-Length: " . filesize($file)); + header("Content-Disposition: attachment; filename=\"images.zip\""); + readfile($file); + + unlink($file); + } + } + else + { + throw new NoImagesException($errors); + } + } + catch (DecodeException $e) + { + echo "Could not decode the given string."; + } + catch (ImageCreateException $e) + { + echo "Could not create an image from the decoded string. The base64 string is invalid or does not contain an image."; + } + catch (NoImagesException $e) + { + echo "No images found. The following errors occurred:"; + echo "
" . print_r($e->getReasons(), true) . ""; + } + catch (Exception $e) + { + print_r($e); + } +} +else +{ + + +echo ''; +?> + + + + +
tested successfully with version 1.4.1.1
+ ++ +'; } -?> \ No newline at end of file +?> + ++