问题
I am attempting to write to an image file from a blob.
if($_POST['logoFilename'] != 'undefined'){
$logoFile = fopen($_POST['logoFilename'], 'w') or die ("Cannot create ".$_POST['logoFilename']);
fwrite($logoFile, $_POST['logoImage']);
fclose($logoFile);
}
In the previous code snippet, $_POST['logoImage']
is a BLOB. The file is correctly written to the root directory, however the file cannot be opened. In ubuntu 11.04 I receive the following error:
Error interpreting JPEG image file (Not a JPEG file: starts with 0x64 0x61).
The BLOB does correctly display if I create an img and set its src=blob
Included below is the first snippet of the BLOB:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDAAgGBgcGBQgHBwcJCQ
回答1:
Your "Blob" is really a Data URI:
data:[<MIME-type>][;charset=<encoding>][;base64],<data>
Since you only want the decoded data part, you have to do
file_put_contents(
'image.jpg',
base64_decode(
str_replace('data:image/jpeg;base64,', '', $blob)
)
);
But since PHP natively supports data:// streams, you can also do (thanks @NikiC)
file_put_contents('image.jpg', file_get_contents($blob));
If the above doesnt work, you can try with GDlib:
imagejpg(
imagecreatefromstring(
base64_decode(
str_replace('data:image/jpeg;base64,', '', $blob)
)
),
'image.jpg'
);
回答2:
If it's really a blob, you might want to trying using mode "wb" as the second parameter to your fopen() call.
EDIT: You might also consider just using file_put_contents(), which is binary-safe.
回答3:
If it's a file upload control, $_POST
won't contain the information. You're looking for handling file uploads with $_FILES. (And more specifically, move_uploaded_file)
Given the new update, try the following:
//
// Export a image blob to a file using either the specific image name
// @blob : The actual image blob
// @fileName : Can be the explicit name (with an extension) or this can be
// just a generic file name and the extension (based on data
// type) will be appended automatically. This can also include
// path information.
// Exmaples:
// storeBlob('data:image/png;base64,...', 'myimage'); :: saves as myimage.png
// storeBlob('data:image/jpg;base64,...', 'img/new.jpg'); :: saves as img/new.jpg
function storeBlob($blob, $fileName)
{
$blobRE = '/^data:((\w+)\/(\w+));base64,(.*)$/';
if (preg_match($blobRE, $blob, $m))
{
$imageName = (preg_match('/\.\w{2,4}$/', $fileName) ? $fileName : sprintf('%s.%s', $fileName, $m[3]));
return file_put_contents($imageName,base64_decode($m[4]));
}
return false; // error
}
回答4:
This function will save data uri to file:
function saveDataUri($blob, $filename = null)
{
// generate unique name basing on content
if (empty($filename)) {
$filename = md5($blob);
}
// parse data URI
$semiPos = strpos($blob, ';', 5);
$comaPos = strpos($blob, ',', 5);
$mime = substr($blob, 5, $semiPos - 5);
$data = substr($blob, $comaPos + 1);
$isEncoded = strpos(substr($blob, $semiPos, $comaPos), 'base64');
if ($isEncoded) {
$data = base64_decode($data);
}
// save image data to file
switch ($mime) {
case 'image/png':
$ext = 'png';
break;
case 'image/gif':
$ext = 'gif';
break;
case 'image/jpg':
case 'image/jpeg':
default:
$ext = 'jpg';
break;
}
$outFile = $filename . '.' . $ext;
$funcName = 'image' . $ext;
$result = $funcName(imagecreatefromstring($data), $outFile);
if ($result) {
return $outFile;
}
return $result;
}
Usage in your case:
// some_validation($_POST);
$filename = saveDataUri($_POST['logoImage']);
echo '<img src="' . $filename . '" alt="' . $filename . '" />';
来源:https://stackoverflow.com/questions/6710309/error-in-writing-to-image-file-from-php