I am attempting to write to an image file from a blob.
if($_POST[\'logoFilename\'] != \'undefined\'){
$logoFile = fopen($_POST[\'logoFilename\'], \'w\') o
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 '';