From eb4a2c9755a6f50aef669ff71f9359c9b55aecb3 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 23 Apr 2013 18:48:22 +0200 Subject: [PATCH 1/2] update 0l.de links --- php/campus/cocal.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/campus/cocal.php b/php/campus/cocal.php index a734057..d4a4512 100644 --- a/php/campus/cocal.php +++ b/php/campus/cocal.php @@ -326,7 +326,7 @@ else {
- 0l + 0l

CampusOffice to Google Sync

@@ -349,7 +349,7 @@ else {

From 90593dafce6cf2f6a1225b85d63509a8dad7da76 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 23 Apr 2013 19:06:17 +0200 Subject: [PATCH 2/2] code refactoring --- php/b64img.php | 226 ++++++++++++++++++++------------------------- php/contactimg.php | 57 +++++------- 2 files changed, 124 insertions(+), 159 deletions(-) diff --git a/php/b64img.php b/php/b64img.php index 5875c10..a87e9b1 100644 --- a/php/b64img.php +++ b/php/b64img.php @@ -2,135 +2,109 @@ class ImageCreateException extends Exception { } class DecodeException extends Exception { } -class NoImagesException extends Exception -{ - protected $reasons = array(); - public function __construct ($reasons) - { - $this->reasons = $reasons; - } - public function getReasons() - { - return $this->reasons; - } +class NoImagesException extends Exception { + protected $reasons = array(); + + public function __construct ($reasons) { + $this->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(); - } +if (!empty($_POST['base64'])) { + $contents = array(); + $errors = array(); + $inputs = array(); - $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); + $count = count($_POST['base64']); - 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); - } + 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 -{ - +else { echo ''; ?> @@ -150,7 +124,7 @@ echo '';
- 0l + 0l

Base 64 to Image Converter

@@ -167,7 +141,7 @@ echo ''; diff --git a/php/contactimg.php b/php/contactimg.php index 1e900e1..9b34708 100644 --- a/php/contactimg.php +++ b/php/contactimg.php @@ -4,17 +4,15 @@ function getExtension($filename) { return strtolower(substr(strrchr($filename,"."),1)); } -if (!empty($_FILES["contacts"])) -{ - $count = count($_FILES["contacts"]["tmp_name"]); +if (!empty($_FILES["contacts"])) { + $count = count($_FILES["contacts"]["tmp_name"]); $files = array("zip" => array(), "vcf" => array(), "contact" => array()); - - for($i = 0; $i < $count; $i++) - { + + for($i = 0; $i < $count; $i++) { $extension = getExtension($_FILES["contacts"]["name"][$i]); - + if($extension == "zip") $files["zip"][] = $i; elseif($extension == "contact") @@ -22,28 +20,24 @@ if (!empty($_FILES["contacts"])) elseif($extension == "vcf") $files["vcf"][] = $i; } - + $contents["contact"] = array(); $contents["vcf"] = array(); - - foreach($files["contact"] as $contactfile) - { + + foreach($files["contact"] as $contactfile) { $contents["contact"][] = file_get_contents($_FILES["contacts"]["tmp_name"][$contactfile]); } - foreach($files["vcf"] as $contactfile) - { + foreach($files["vcf"] as $contactfile) { $contents["vcf"][] = file_get_contents($_FILES["contacts"]["tmp_name"][$contactfile]); } - - foreach($files["zip"] as $zipfile) - { + + foreach($files["zip"] as $zipfile) { $zip = new ZipArchive(); $zip->open($_FILES["contacts"]["tmp_name"][$zipfile]); - for ($i = 0; $i < $zip->numFiles; $i++) - { + for ($i = 0; $i < $zip->numFiles; $i++) { $extension = getExtension($zip->getNameIndex($i)); if($extension == "contact") @@ -51,23 +45,21 @@ if (!empty($_FILES["contacts"])) elseif($extension == "vcf") $contents["vcf"][] = $zip->getFromIndex($i); } - + $zip->close(); } - + $images = array(); - - foreach($contents["contact"] as $contact) - { + + foreach($contents["contact"] as $contact) { $image = array(); $image["name"] = preg_replace('=.*?(.*?).*=si',"\\1", $contact); $image["imgb64"] = preg_replace('=.*?(.*?).*=si',"\\1", $contact); - + $images[] = $image; } - foreach($contents["vcf"] as $vcf) - { + foreach($contents["vcf"] as $vcf) { preg_match_all('=FN:(.*?)\n=si', $vcf, $names); $count = preg_match_all('=PHOTO.*?:(.*?)\n[A-Z]=si', $vcf, $imgb64s); @@ -75,7 +67,7 @@ if (!empty($_FILES["contacts"])) $images[] = array("name" => $names[1][$i], "imgb64" => $imgb64s[1][$i]); } } - + ?> @@ -90,8 +82,7 @@ if (!empty($_FILES["contacts"])) If you do not have JavaScript, please click "Convert!" again.

+foreach ($images as $image) { ?> " /> @@ -101,9 +92,9 @@ foreach ($images as $image) - + ';
- 0l + 0l

Windows Contact to Image Converter

@@ -143,7 +134,7 @@ echo '';