Merge branch 'master' of github.com:stv0g/snippets
This commit is contained in:
commit
7ec78ea9af
3 changed files with 126 additions and 161 deletions
|
@ -2,94 +2,76 @@
|
|||
|
||||
class ImageCreateException extends Exception { }
|
||||
class DecodeException extends Exception { }
|
||||
class NoImagesException extends Exception
|
||||
{
|
||||
class NoImagesException extends Exception {
|
||||
protected $reasons = array();
|
||||
public function __construct ($reasons)
|
||||
{
|
||||
|
||||
public function __construct ($reasons) {
|
||||
$this->reasons = $reasons;
|
||||
}
|
||||
public function getReasons()
|
||||
{
|
||||
|
||||
public function getReasons() {
|
||||
return $this->reasons;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($_POST['base64']))
|
||||
{
|
||||
if (!empty($_POST['base64'])) {
|
||||
$contents = array();
|
||||
$errors = array();
|
||||
$inputs = array();
|
||||
|
||||
$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]);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
foreach($inputs as $input)
|
||||
{
|
||||
try
|
||||
{
|
||||
try {
|
||||
foreach($inputs as $input) {
|
||||
try {
|
||||
$img_decoded = base64_decode($input['base64']);
|
||||
|
||||
if(empty($img_decoded))
|
||||
{
|
||||
if(empty($img_decoded)) {
|
||||
throw new DecodeException("Could not get base64 string");
|
||||
}
|
||||
|
||||
if (!@imagecreatefromstring($img_decoded))
|
||||
{
|
||||
if (!@imagecreatefromstring($img_decoded)) {
|
||||
throw new ImageCreateException();
|
||||
}
|
||||
|
||||
$contents[] = array("decoded" => $img_decoded, "name" => $input["name"]);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
catch (Exception $e) {
|
||||
$errors[] = $e;
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($contents))
|
||||
{
|
||||
if(!empty($contents)) {
|
||||
$count = count($contents);
|
||||
|
||||
if($count == 1)
|
||||
{
|
||||
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
|
||||
{
|
||||
else {
|
||||
$file = tempnam("tmp", "zip");
|
||||
|
||||
$zip = new ZipArchive();
|
||||
$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"];
|
||||
$zip->addFromString($filename . ".png", $contents[$i]["decoded"]);
|
||||
}
|
||||
|
||||
if(!empty($errors))
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -101,36 +83,28 @@ if (!empty($_POST['base64']))
|
|||
header("Content-Length: " . filesize($file));
|
||||
header("Content-Disposition: attachment; filename=\"images.zip\"");
|
||||
readfile($file);
|
||||
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
throw new NoImagesException($errors);
|
||||
}
|
||||
}
|
||||
catch (DecodeException $e)
|
||||
{
|
||||
catch (DecodeException $e) {
|
||||
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.";
|
||||
}
|
||||
catch (NoImagesException $e)
|
||||
{
|
||||
catch (NoImagesException $e) {
|
||||
echo "No images found. The following errors occurred:";
|
||||
echo "<pre>" . print_r($e->getReasons(), true) . "</pre>";
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
catch (Exception $e) {
|
||||
print_r($e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
else {
|
||||
|
||||
echo '<?xml version="1.0" ?>';
|
||||
?>
|
||||
|
@ -150,7 +124,7 @@ echo '<?xml version="1.0" ?>';
|
|||
<div id="content">
|
||||
|
||||
<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>
|
||||
</header>
|
||||
|
||||
|
@ -167,7 +141,7 @@ echo '<?xml version="1.0" ?>';
|
|||
</form>
|
||||
|
||||
<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>
|
||||
|
||||
|
||||
|
|
|
@ -326,7 +326,7 @@ else {
|
|||
|
||||
<div id="content">
|
||||
<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>
|
||||
</header>
|
||||
|
||||
|
@ -349,7 +349,7 @@ else {
|
|||
</p>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -4,15 +4,13 @@ function getExtension($filename) {
|
|||
return strtolower(substr(strrchr($filename,"."),1));
|
||||
}
|
||||
|
||||
if (!empty($_FILES["contacts"]))
|
||||
{
|
||||
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")
|
||||
|
@ -26,24 +24,20 @@ if (!empty($_FILES["contacts"]))
|
|||
$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")
|
||||
|
@ -57,8 +51,7 @@ if (!empty($_FILES["contacts"]))
|
|||
|
||||
$images = array();
|
||||
|
||||
foreach($contents["contact"] as $contact)
|
||||
{
|
||||
foreach($contents["contact"] as $contact) {
|
||||
$image = array();
|
||||
$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);
|
||||
|
@ -66,8 +59,7 @@ if (!empty($_FILES["contacts"]))
|
|||
$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);
|
||||
|
||||
|
@ -90,8 +82,7 @@ if (!empty($_FILES["contacts"]))
|
|||
If you do not have JavaScript, please click "Convert!" again.</p>
|
||||
|
||||
<?php
|
||||
foreach ($images as $image)
|
||||
{ ?>
|
||||
foreach ($images as $image) { ?>
|
||||
<textarea name="base64[]" style="display:none"><?php echo $image["imgb64"]; ?></textarea>
|
||||
<input name="names[]" type="text" style="display:none" value="<?php echo $image["name"]; ?>" />
|
||||
<?php } ?>
|
||||
|
@ -125,7 +116,7 @@ echo '<?xml version="1.0" ?>';
|
|||
<body><div id="content">
|
||||
|
||||
<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>
|
||||
</header>
|
||||
|
||||
|
@ -143,7 +134,7 @@ echo '<?xml version="1.0" ?>';
|
|||
<iframe name="ifr" id="ifr"></iframe>
|
||||
|
||||
<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>
|
||||
<div id="iPhoneExport" style="display:none">
|
||||
|
|
Loading…
Add table
Reference in a new issue