Merge branch 'master' of github.com:stv0g/snippets

This commit is contained in:
Steffen Vogel 2013-05-10 02:59:16 +02:00
commit 7ec78ea9af
3 changed files with 126 additions and 161 deletions

View file

@ -2,94 +2,76 @@
class ImageCreateException extends Exception { } class ImageCreateException extends Exception { }
class DecodeException extends Exception { } class DecodeException extends Exception { }
class NoImagesException extends Exception class NoImagesException extends Exception {
{
protected $reasons = array(); protected $reasons = array();
public function __construct ($reasons)
{ public function __construct ($reasons) {
$this->reasons = $reasons; $this->reasons = $reasons;
} }
public function getReasons()
{ public function getReasons() {
return $this->reasons; return $this->reasons;
} }
} }
if (!empty($_POST['base64'])) if (!empty($_POST['base64'])) {
{
$contents = array(); $contents = array();
$errors = array(); $errors = array();
$inputs = array(); $inputs = array();
$count = count($_POST['base64']); $count = count($_POST['base64']);
for ($i = 0; $i < $count; $i++) for ($i = 0; $i < $count; $i++) {
{
$inputs[$i] = array("base64" => $_POST['base64'][$i], "name" => $_POST['names'][$i]); $inputs[$i] = array("base64" => $_POST['base64'][$i], "name" => $_POST['names'][$i]);
} }
try try {
{ foreach($inputs as $input) {
foreach($inputs as $input) try {
{
try
{
$img_decoded = base64_decode($input['base64']); $img_decoded = base64_decode($input['base64']);
if(empty($img_decoded)) if(empty($img_decoded)) {
{
throw new DecodeException("Could not get base64 string"); throw new DecodeException("Could not get base64 string");
} }
if (!@imagecreatefromstring($img_decoded)) if (!@imagecreatefromstring($img_decoded)) {
{
throw new ImageCreateException(); throw new ImageCreateException();
} }
$contents[] = array("decoded" => $img_decoded, "name" => $input["name"]); $contents[] = array("decoded" => $img_decoded, "name" => $input["name"]);
} }
catch (Exception $e) catch (Exception $e) {
{
$errors[] = $e; $errors[] = $e;
} }
} }
if(!empty($contents)) if(!empty($contents)) {
{
$count = count($contents); $count = count($contents);
if($count == 1) if($count == 1) {
{
$im = imagecreatefromstring($contents[0]["decoded"]); $im = imagecreatefromstring($contents[0]["decoded"]);
header("Content-Type: image/png"); header("Content-Type: image/png");
header("Cache-Control: no-cache, must-revalidate"); header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
imagepng($im); imagepng($im);
} }
else else {
{
$file = tempnam("tmp", "zip"); $file = tempnam("tmp", "zip");
$zip = new ZipArchive(); $zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE); $zip->open($file, ZipArchive::OVERWRITE);
for($i = 0; $i < $count; $i++) for($i = 0; $i < $count; $i++) {
{
$filename = empty($contents[$i]["name"]) ? "image_{$i}" : $contents[$i]["name"]; $filename = empty($contents[$i]["name"]) ? "image_{$i}" : $contents[$i]["name"];
$zip->addFromString($filename . ".png", $contents[$i]["decoded"]); $zip->addFromString($filename . ".png", $contents[$i]["decoded"]);
} }
if(!empty($errors)) if(!empty($errors)) {
{
$errorstring = print_r($errors, true); $errorstring = print_r($errors, true);
$info = "In total, " . count($errors) . " errors occurred:\r\n\r\n"; $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, "DecodeException") . " times, decoding the string didnt work\r\n";
$info .= "- " . substr_count($errorstring, "ImageCreateException") . " times, creating the image didnt work"; $info .= "- " . substr_count($errorstring, "ImageCreateException") . " times, creating the image didnt work";
$zip->addFromString("errors.txt", $info); $zip->addFromString("errors.txt", $info);
} }
@ -101,36 +83,28 @@ if (!empty($_POST['base64']))
header("Content-Length: " . filesize($file)); header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=\"images.zip\""); header("Content-Disposition: attachment; filename=\"images.zip\"");
readfile($file); readfile($file);
unlink($file); unlink($file);
} }
} }
else else {
{
throw new NoImagesException($errors); throw new NoImagesException($errors);
} }
} }
catch (DecodeException $e) catch (DecodeException $e) {
{
echo "Could not decode the given string."; echo "Could not decode the given string.";
} }
catch (ImageCreateException $e) catch (ImageCreateException $e) {
{
echo "Could not create an image from the decoded string. The base64 string is invalid or does not contain an image."; echo "Could not create an image from the decoded string. The base64 string is invalid or does not contain an image.";
} }
catch (NoImagesException $e) catch (NoImagesException $e) {
{
echo "No images found. The following errors occurred:"; echo "No images found. The following errors occurred:";
echo "<pre>" . print_r($e->getReasons(), true) . "</pre>"; echo "<pre>" . print_r($e->getReasons(), true) . "</pre>";
} }
catch (Exception $e) catch (Exception $e) {
{
print_r($e); print_r($e);
} }
} }
else else {
{
echo '<?xml version="1.0" ?>'; echo '<?xml version="1.0" ?>';
?> ?>
@ -150,7 +124,7 @@ echo '<?xml version="1.0" ?>';
<div id="content"> <div id="content">
<header> <header>
<a href="http://0l.de"><img src="http://0l.de/_media/nulll_small.png" alt="0l" /></a> <a href="http://dev.0l.de"><img src="http://dev.0l.de/_media/nulll_small.png" alt="0l" /></a>
<h1>Base 64 to Image Converter</h1> <h1>Base 64 to Image Converter</h1>
</header> </header>
@ -167,7 +141,7 @@ echo '<?xml version="1.0" ?>';
</form> </form>
<footer> <footer>
<p>by <a href="http://www.michaschwab.de">Micha Schwab</a> - <a href="http://0l.de/tools/base64img">help</a></p> <p>by <a href="http://www.michaschwab.de">Micha Schwab</a> - <a href="http://dev.0l.de/tools/base64img">help</a></p>
</footer> </footer>

View file

@ -326,7 +326,7 @@ else {
<div id="content"> <div id="content">
<header> <header>
<a href="http://0l.de"><img src="http://0l.de/_media/nulll_small.png" alt="0l" /></a> <a href="http://dev.0l.de"><img src="http://dev.0l.de/_media/nulll_small.png" alt="0l" /></a>
<h1>CampusOffice to Google Sync</h1> <h1>CampusOffice to Google Sync</h1>
</header> </header>
@ -349,7 +349,7 @@ else {
</p> </p>
<footer> <footer>
<p>by <a href="http://www.steffenvogel.de">Steffen Vogel</a> - <a href="http://0l.de/tools/campus">help</a></p> <p>by <a href="http://www.steffenvogel.de">Steffen Vogel</a> - <a href="http://dev.0l.de/tools/campus">help</a></p>
</footer> </footer>
</div> </div>
</body> </body>

View file

@ -4,15 +4,13 @@ function getExtension($filename) {
return strtolower(substr(strrchr($filename,"."),1)); return strtolower(substr(strrchr($filename,"."),1));
} }
if (!empty($_FILES["contacts"])) if (!empty($_FILES["contacts"])) {
{
$count = count($_FILES["contacts"]["tmp_name"]); $count = count($_FILES["contacts"]["tmp_name"]);
$files = array("zip" => array(), $files = array("zip" => array(),
"vcf" => array(), "vcf" => array(),
"contact" => array()); "contact" => array());
for($i = 0; $i < $count; $i++) for($i = 0; $i < $count; $i++) {
{
$extension = getExtension($_FILES["contacts"]["name"][$i]); $extension = getExtension($_FILES["contacts"]["name"][$i]);
if($extension == "zip") if($extension == "zip")
@ -26,24 +24,20 @@ if (!empty($_FILES["contacts"]))
$contents["contact"] = array(); $contents["contact"] = array();
$contents["vcf"] = array(); $contents["vcf"] = array();
foreach($files["contact"] as $contactfile) foreach($files["contact"] as $contactfile) {
{
$contents["contact"][] = file_get_contents($_FILES["contacts"]["tmp_name"][$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]); $contents["vcf"][] = file_get_contents($_FILES["contacts"]["tmp_name"][$contactfile]);
} }
foreach($files["zip"] as $zipfile) foreach($files["zip"] as $zipfile) {
{
$zip = new ZipArchive(); $zip = new ZipArchive();
$zip->open($_FILES["contacts"]["tmp_name"][$zipfile]); $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)); $extension = getExtension($zip->getNameIndex($i));
if($extension == "contact") if($extension == "contact")
@ -57,8 +51,7 @@ if (!empty($_FILES["contacts"]))
$images = array(); $images = array();
foreach($contents["contact"] as $contact) foreach($contents["contact"] as $contact) {
{
$image = array(); $image = array();
$image["name"] = preg_replace('=.*?<c:FormattedName>(.*?)</c:FormattedName>.*=si',"\\1", $contact); $image["name"] = preg_replace('=.*?<c:FormattedName>(.*?)</c:FormattedName>.*=si',"\\1", $contact);
$image["imgb64"] = preg_replace('=.*?<c:Value c:ContentType\="binary".*?>(.*?)</c:Value>.*=si',"\\1", $contact); $image["imgb64"] = preg_replace('=.*?<c:Value c:ContentType\="binary".*?>(.*?)</c:Value>.*=si',"\\1", $contact);
@ -66,8 +59,7 @@ if (!empty($_FILES["contacts"]))
$images[] = $image; $images[] = $image;
} }
foreach($contents["vcf"] as $vcf) foreach($contents["vcf"] as $vcf) {
{
preg_match_all('=FN:(.*?)\n=si', $vcf, $names); preg_match_all('=FN:(.*?)\n=si', $vcf, $names);
$count = preg_match_all('=PHOTO.*?:(.*?)\n[A-Z]=si', $vcf, $imgb64s); $count = preg_match_all('=PHOTO.*?:(.*?)\n[A-Z]=si', $vcf, $imgb64s);
@ -90,8 +82,7 @@ if (!empty($_FILES["contacts"]))
If you do not have JavaScript, please click "Convert!" again.</p> If you do not have JavaScript, please click "Convert!" again.</p>
<?php <?php
foreach ($images as $image) foreach ($images as $image) { ?>
{ ?>
<textarea name="base64[]" style="display:none"><?php echo $image["imgb64"]; ?></textarea> <textarea name="base64[]" style="display:none"><?php echo $image["imgb64"]; ?></textarea>
<input name="names[]" type="text" style="display:none" value="<?php echo $image["name"]; ?>" /> <input name="names[]" type="text" style="display:none" value="<?php echo $image["name"]; ?>" />
<?php } ?> <?php } ?>
@ -125,7 +116,7 @@ echo '<?xml version="1.0" ?>';
<body><div id="content"> <body><div id="content">
<header> <header>
<a href="http://0l.de"><img src="http://0l.de/_media/nulll_small.png" alt="0l" /></a> <a href="http://dev.0l.de"><img src="http://dev.0l.de/_media/nulll_small.png" alt="0l" /></a>
<h1>Windows Contact to Image Converter</h1> <h1>Windows Contact to Image Converter</h1>
</header> </header>
@ -143,7 +134,7 @@ echo '<?xml version="1.0" ?>';
<iframe name="ifr" id="ifr"></iframe> <iframe name="ifr" id="ifr"></iframe>
<footer> <footer>
<p>by <a href="http://www.michaschwab.de">Micha Schwab</a> - <a href="http://0l.de/tools/contactimg">help</a></p> <p>by <a href="http://www.michaschwab.de">Micha Schwab</a> - <a href="http://dev.0l.de/tools/contactimg">help</a></p>
<a href="#" onclick="document.getElementById('iPhoneExport').style.display='block'">You can also use this tool to get the contact photos you made with your iPhone</a> <a href="#" onclick="document.getElementById('iPhoneExport').style.display='block'">You can also use this tool to get the contact photos you made with your iPhone</a>
<div id="iPhoneExport" style="display:none"> <div id="iPhoneExport" style="display:none">