Lets say I have this data url from Apple:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6%2BR8AAAAlElEQVR42oWQQRHEIAxF10ElVAISVgo3bCABCUhYCZWAhEpAQpoD6bzJhNnDO0DyyA8fEXkppXyVCpLViDUfyqVIQN9JFMY637hrlCySFauL21E7KVWbAIGx56rnSLqc5KPXSLo3kySalPhTygFhRDtFC09EIsMeZJSGBj7Qveh3OJW89syImiH%2BIO2BOJX0XwA2%2BQEL4pAhSX4aBQAAAABJRU5ErkJggg%3D%3D
That I want to save as a png file.How do I do That?
Rightclicking and 'Save image as' is not working(Why?)
So I thought I should use a PHP script with the 'base64_decode' function:
$img = "iVBORw0KGgoAAAANSUhEUgAAA9QAAAAkCAMAAABfcIIyAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAHVQTFRFAAAA%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F5YtmQAAAACd0Uk5TAAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmZiD6WAAAAdlJREFUeF7t3btuFEEQheFzunotGYkIERA4spn3fyeHiAgEEtsXB82s2eEJavR%2F0Wjyo75XWfHpw4MkSXNqCkA2lr2%2B%2Fvz63n15qpI0x5yTSAM52XaxJLVXf%2FkoaY6xQk2sgXy8Ql2KJf3wS5HG6HMMQg3kZNkuxVGKNLxJo48%2BxhxTpBrIx7JcXEqUKJI3zd77CjWLaiAle4U6Iixv6q33MfocTL%2BBlCy7OEqJqCFvs%2FV2m3%2BTaiAd6zb7rlHtrbc1VA%2Bm30BSa%2B%2B7RNSo4a21thbVTL%2BBnCy7rCV1rdXbtbXW%2B%2Bh%2Ft78BZLM2v6NE1Fov%2FrpC3Zl%2BA2l5H6lrrRe%2FXNu%2BqCbUQE57qGvsoW5t9D4H028gJculOKLUWuvFz%2B16O9NipAZSsm8nWpfq53ZlTQ3k9s%2BamlADZ0CogZMh1MDJEGrgZAg1cDL%2Fh5ojLSC1%2ByMtLp8A2R0un3BNFEjv%2FpooDzqA9O4fdPD0Esju8PSSIglAdsciCZQzArI7lDOat30yCg8CGe2FB1eovZcI7oMSwUBSq0TwWlQHxfyBEzgW86ftDpDase0ODfKA7A4N8mhlC2R318r2px%2BeQqLpPJDae9P5%2FmrVz491%2FWekBlKy7PXVfn9rb9xXsMD2wdOcAAAAAElFTkSuQmCC";
header("Content-type: image/png");
echo base64_decode($img);
That is giving me back the broken image icon :
How should I solve this problem and save a data url image to a file?
Your code is fine, but your base64 is urlencoded. I urldecoded it for you: iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6+R8AAAAlElEQVR42oWQQRHEIAxF10ElVAISVgo3bCABCUhYCZWAhEpAQpoD6bzJhNnDO0DyyA8fEXkppXyVCpLViDUfyqVIQN9JFMY637hrlCySFauL21E7KVWbAIGx56rnSLqc5KPXSLo3kySalPhTygFhRDtFC09EIsMeZJSGBj7Qveh3OJW89syImiH+IO2BOJX0XwA2+QEL4pAhSX4aBQAAAABJRU5ErkJggg==
or do it yourself:
base64_decode(urldecode($img));
Related
I'm trying to echo an image content from an uploaded file with this code:
<?php
$imgContent = addslashes(file_get_contents($_FILES['image']['tmp_name']));
header("Content-type: image/png");
echo $imgContent;
?>
But it just shows an small empty square. I must upload and fetch a BLOB field from mysql to show on the browser, but it's not saving correctly.
As commented by Lars Stegelitz and RiggsFolly, I corrupted the file by adding addslashes on the file content. By removing it I could see the image being returned back.
I wroted that simple script just to test if I getting the image content correctly. In the real code I'm checking the file type.
I want to display image from blob mysql data, but image not display, here the code :
$data = $this->Api_m->getData("calon", 'id_calon', $id)->result()[0]->gambar_calon;
header('Content-Type: image/png');
imagepng($data);
image type is png, and when i download from phpmyadmin is work. and because I want to show image in android so i cant use base64 image like this way:
src='data:image/jpeg;base64
Browser screenshot :
Hi i write some code for images to be put side by side.I use Imagick library for that purpose.This is my code.
$im = new Imagick();
// session contain image path like upload/my.jpg
$im->readImage("http://localhost/wordpress3.5/".$_SESSION['imgname']);
$im->readImage("http://localhost/wordpress3.5/".$_SESSION['preimgurl']);
$im->resetIterator();
$combined = $im->appendImages(false);
/* Output the image */
$combined->setImageFormat("png");
header("Content-Type: image/png");
echo $combined;exit();
But the output is not what i suppose to be.this is output.I write this code under the plugin/ files.Also i want to save that image to directory like "localhost/wordpress/uplaod_pic/".
You should use an image tag with the src tags containing the direct or base64 version of the image.
First example would be direct:
echo '<img src="http://localhost/wordpress3.5/'.$_SESSION['imgname'].'">';
Second would be using base64 with the data URI scheme, PHP Example:
echo '<img src="data:image/png;base64,'.base64_encode($combined).'">';
I would recommend the first method, simply because it looks like a product display, the images are most likely public and hotlinking can be busted if you choose.
In both examples, since the image is inline, you wouldn't need to set the content-type to an image.
You should also place the correct mime type within the second version, either image/png, image/jpeg or others depending on the image.
Edit: To extend this:
$combined contains the image, so you want to save this, simple use:
file_put_contents('upload_pic/'.$time().'.png', $combined);
I have a question about displaying images in PHP. I need to have a PHP file display as JUST an image, as opposed to an image embedded in a web page, as if you had browsed to the JPEG image directly. The reason that I need it to be a PHP page as opposed to actually browsing to the image is that I need to resize the image before it is delivered. It would be easiest to use an image directly because that way I can display the image in a desktop application more easily. Is there any way to do this? Thanks!
<?php
header('Content-Type: image/jpeg');
readfile('path/to/image.jpeg');
You need to set the header Content-Type property to be the correct image type. See this link for the some examples.
http://www.electrictoolbox.com/image-headers-php/
Choose the correct header :
header("Content-type: image/png");
And use php gd to change the image size : http://php.net/manual/fr/book.image.php
I am trying to make a PHP page load an image and display, as if it were an image file. Here's what I tried to do:
header("Content-type: image/png");
echo base64_encode(file_get_contents($ad->location));
But this doesn't seems to work. How should this be done?
Thank you.
You don't need to base64 encode image data when outputting it as a file.
Just pass it through.