I'm having a problem with writing a new image file from image-data (dataurl). Images lower than 1 mb is no problem with file_put_contents
. But if the file is bigger than 1 mb. The function does not work.
Procedure
- A DataUrl was sent to
print.php
via ajax with 1436190 in length. - print.php is get the var
$_POST['image']
and$_POST['filename']
print.php
save file to hdd withfile_put_contents($photo, $data);
Here's the print.php
script:
<?php
header('Content-Type: image/jpeg');
ini_set('memory_limit', '128M');
$data=$_POST['image'];
if(!$_POST['filename']){
exit("error");
}
list($dir,$folder,$file)=explode("/",$_POST['filename']);
$ext=strtolower(pathinfo($_POST['filename'], PATHINFO_EXTENSION));
$file=basename($file,".".$ext);
$data = base64_decode($data);
$imgRes = imagecreatefromstring($data);
if($imgRes !== false && imagejpeg($imgRes, $file,"100") === true)
if(!is_dir($dir.'/'.$folder.'/new/')){
mkdir($dir.'/'.$folder.'/new/',0777);
}
$photo=$dir.'/'.$folder.'/new/'.$file.'-'.time().".".$ext;//full url
$saved=file_put_contents($photo, $data);
if($saved){
echo "ok";
}else{
echo "error";
}
imagedestroy($imgRes);
?>
I've tried to figure this out. But it's not working. Nothing shows in console. Or is there any smarter way to achieve this than file_put_contents
?