[英]Error in Writing to Image file from PHP
我正在嘗試從 blob 寫入圖像文件。
if($_POST['logoFilename'] != 'undefined'){
$logoFile = fopen($_POST['logoFilename'], 'w') or die ("Cannot create ".$_POST['logoFilename']);
fwrite($logoFile, $_POST['logoImage']);
fclose($logoFile);
}
在前面的代碼片段中, $_POST['logoImage']
是一個 BLOB。 該文件已正確寫入根目錄,但無法打開該文件。 在 ubuntu 11.04 中,我收到以下錯誤:
Error interpreting JPEG image file (Not a JPEG file: starts with 0x64 0x61).
如果我創建一個 img 並設置它的 src=blob,BLOB 會正確顯示
下面是 BLOB 的第一個片段:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDAAgGBgcGBQgHBwcJCQ
data:[<MIME-type>][;charset=<encoding>][;base64],<data>
由於您只想要解碼的數據部分,因此您必須這樣做
file_put_contents(
'image.jpg',
base64_decode(
str_replace('data:image/jpeg;base64,', '', $blob)
)
);
但是由於 PHP 本身支持 data:// 流,您也可以這樣做(感謝@NikiC)
file_put_contents('image.jpg', file_get_contents($blob));
如果上述方法不起作用,您可以嘗試使用 GDlib:
imagejpg(
imagecreatefromstring(
base64_decode(
str_replace('data:image/jpeg;base64,', '', $blob)
)
),
'image.jpg'
);
如果是文件上傳控件,
$_POST
將不包含該信息。
您正在尋找使用
$_FILES
處理文件上傳。
(更具體地說,
move_uploaded_file )
鑒於新的更新,請嘗試以下操作:
//
// 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
}
如果它真的是一個 blob,您可能想嘗試使用模式“wb”作為 fopen() 調用的第二個參數。
編輯:您也可以考慮只使用file_put_contents() ,這是二進制安全的。
這個 function 會將數據 uri 保存到文件中:
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;
}
在您的情況下使用:
// some_validation($_POST);
$filename = saveDataUri($_POST['logoImage']);
echo '<img src="' . $filename . '" alt="' . $filename . '" />';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.