PHP - 获得的base64 IMG串去code和保存为jpg(导致空图片) [英] PHP - get base64 img string decode and save as jpg (resulting empty image )
问题描述
我是实际发送的base64图像串槽阿贾克斯PHP脚本刚刚去codeS字符串,并保存内容.jpg文件。
但结果是一个空的图像。
这怎么可能呢?
PHP脚本:
$ uploadedPhotos =阵列('photo_1','photo_2','photo_3','photo_4');
的foreach($ uploadedPhotos为$文件){
如果($这个 - >输入 - >后期('photo_1')){
$ photoTemp = base64_de code($这个 - >输入 - >后期('photo_1'));
/ *设置形式*图片为节目的名称/
$这个 - >会话级> set_userdata('_上传'$文件,蚁族);
/ *上传*的设定时间/
如果($这个 - >!会话级>用户数据('uploading_on_datetime')){
$这 - >会话而> set_userdata('uploading_on_datetime',时间());
}
$ datetime_upload = $这个 - >会话级>用户数据('uploading_on_datetime',真);
/ *创建临时目录随着时间的推移和用户ID * /
$ NEW_DIR ='温度/用户_'$这个 - 方式>&会话而GT;用户数据('USER_ID',真).'_上_ /''$ datetime_upload。;
@mkdir($ NEW_DIR);
/ *移动上传的文件以新的名称* /
@file_put_contents($ NEW_DIR $文件,$ photoTemp。JPG。);
}
阿贾克斯,这是确定的原因回声$ photoTemp返回字符串。
我tryed 的var_dump(@file_put_contents($ NEW_DIR $文件,$ photoTemp)。JPG。');
并返回布尔(真)
由于图像保存,但有图像:(空图像中没有任何内容
空形象我的意思是,创建并命名的文件,它具有相同的
内容的大小我传递给PHP的,但是当我尝试打开该图片
以preVIEW它说,文件无法打开的原因损坏或损坏的文件类型格式:/
块引用>反正这是需要的照片为base64和发送的JS到PHP:
<脚本类型=文/ JavaScript的>VAR _min_width = 470;
VAR _min_height = 330;
VAR _which;
VAR _fyle_type;
VAR IO;
VAR allowed_types =新的Array('图像/ PNG,图像/ JPG,图像/ JPEG');
如果(typeof运算(的FileReader)==='功能'){
$(输入[类型=文件]')。在('变化',函数(E){
VAR _file_name = $(本).VAL();
$('。'+ _ +这'_持有者)文本(_file_name)。
var文件= e.target.files [0]; 如果(!in_array(file.type,allowed_types)|| file.length === 0){
通知(你必须选择一个有效的图像文件!,假的,假的);
返回;
} 如果(file.size> 3145728 / * * 3MB /){
通知(<?PHP的回声郎('每个-照片1MB');?>中,假的,假的);
返回;
}
notify_destroy(); VAR读卡器=新的FileReader();
reader.onload = fileOnload;
reader.readAsDataURL(文件);
});功能fileOnload(五){
VAR IMG =使用document.createElement('IMG');
img.src = e.target.result; img.addEventListener('负荷',函数(){
如果(img.width< _min_width || img.height< _min_height){
通知(<?PHP的回声郎('每个-照片1MB');?>中,假的,假的);
返回;
}
$阿贾克斯({
类型:'后',
数据类型:脚本,
数据:{photo_1:e.target.result},
网址:_config_base_url +'/上传/ upload_photos',
进度:功能(E){
的console.log(E);
},
成功:函数(D){
$('身体')追加('< IMG SRC ='+ D +'/>');
}
});
});}
}
< / SCRIPT>
解决方案据我所知,你必须使用图像功能imagecreatefromstring,imagejpeg创建图像。
$为imageData = base64_de code($为imageData);
$源= imagecreatefromstring($为imageData);
$旋转= imagerotate($源,$角度,0); //如果要旋转图像
$ imageSave = imagejpeg($旋转,$ imageName,100);
imagedestroy($源);希望这会有所帮助。
图像数据PHP code
$ imageDataEn codeD = base64_en code(的file_get_contents('sample.png'));
$为imageData = base64_de code($ imageDataEn codeD);
$源= imagecreatefromstring($为imageData);
$角= 90;
$旋转= imagerotate($源,$角度,0); //如果要旋转图像
$ imageName =hello1.png;
$ imageSave = imagejpeg($旋转,$ imageName,100);
imagedestroy($源);所以,以下是你的程序的PHP的一部分。
注:
与注释的变化变化是这里
$ uploadedPhotos =阵列('photo_1','photo_2','photo_3','photo_4');
的foreach($ uploadedPhotos为$文件){
如果($这个 - >输入 - >后期($文件)){
$为imageData = base64_de code($这个 - >输入 - >后期($文件)); //< - **唯一不变的是这里的变量名**
$照片= imagecreatefromstring($为imageData); //< - **变化是这里** / *设置形式*图片为节目的名称/
$这个 - >会话级> set_userdata('_上传'$文件,蚁族);
/ *上传*的设定时间/
如果($这个 - >!会话级>用户数据('uploading_on_datetime')){
$这 - >会话而> set_userdata('uploading_on_datetime',时间());
}
$ datetime_upload = $这个 - >会话级>用户数据('uploading_on_datetime',真); / *创建临时目录随着时间的推移和用户ID * /
$ NEW_DIR ='温度/用户_'$这个 - 方式>&会话而GT;用户数据('USER_ID',真).'_上_ /''$ datetime_upload。;
如果(!is_dir($ NEW_DIR)){
@mkdir($ NEW_DIR);
}
/ *移动上传的文件以新的名称* /
// @file_put_contents($ NEW_DIR $文件'JPG。',imagejpeg($照片)。);
imagejpeg($照片,$ NEW_DIR $文件'JPG。',100); //< - **变化是这里** }
}hi i'm actually sending a base64 image string trough ajax to a php script which just decodes string and save content as .jpg file.
But the result is an empty image.
How can this be possible?
php script:
$uploadedPhotos = array('photo_1','photo_2','photo_3','photo_4'); foreach ($uploadedPhotos as $file) { if($this->input->post('photo_1')){ $photoTemp = base64_decode($this->input->post('photo_1')); /*Set name of the photo for show in the form*/ $this->session->set_userdata('upload_'.$file,'ant'); /*set time of the upload*/ if(!$this->session->userdata('uploading_on_datetime')){ $this->session->set_userdata('uploading_on_datetime',time()); } $datetime_upload = $this->session->userdata('uploading_on_datetime',true); /*create temp dir with time and user id*/ $new_dir = 'temp/user_'.$this->session->userdata('user_id',true).'_on_'.$datetime_upload.'/'; @mkdir($new_dir); /*move uploaded file with new name*/ @file_put_contents( $new_dir.$file.'.jpg',$photoTemp); }
Ajax it is ok cause echo $photoTemp returns the string.
i tryed
var_dump(@file_put_contents( $new_dir.$file.'.jpg',$photoTemp));
and it returnsbool(true)
since the image is saved but there is no content in the image :( empty imagefor empty image i mean , file is created and named, and it has same size of the content i pass to php, but when i try to open that image to preview it says file can't be opened cause corrupted or bad file type format :/
anyway this is the JS that takes photo as base64 and send that to php:
<script type="text/javascript"> var _min_width = 470; var _min_height = 330; var _which; var _fyle_type; var io; var allowed_types = new Array('image/png','image/jpg','image/jpeg'); if (typeof(FileReader) === 'function'){ $('input[type="file"]').on('change', function(e) { var _file_name = $(this).val(); $('.'+_which+'_holder').text(_file_name); var file = e.target.files[0]; if (!in_array(file.type,allowed_types) || file.length === 0){ notify("You must select a valid image file!",false,false); return; } if(file.size > 3145728 /*3MB*/){ notify("<?php echo lang('each-photo-1MB'); ?>",false,false); return; } notify_destroy(); var reader = new FileReader(); reader.onload = fileOnload; reader.readAsDataURL(file); }); function fileOnload(e) { var img = document.createElement('img'); img.src = e.target.result; img.addEventListener('load', function() { if(img.width < _min_width || img.height < _min_height ){ notify("<?php echo lang('each-photo-1MB'); ?>",false,false); return; } $.ajax({ type:'post', dataType:'script', data:{photo_1:e.target.result}, url:_config_base_url+'/upload/upload_photos', progress:function(e){ console.log(e); }, success:function(d){ $('body').append('<img src="'+d+'"/>'); } }); }); } } </script>
解决方案AFAIK, You have to use image function imagecreatefromstring, imagejpeg to create the images.
$imageData = base64_decode($imageData); $source = imagecreatefromstring($imageData); $rotate = imagerotate($source, $angle, 0); // if want to rotate the image $imageSave = imagejpeg($rotate,$imageName,100); imagedestroy($source);
Hope this will help.
PHP CODE WITH IMAGE DATA
$imageDataEncoded = base64_encode(file_get_contents('sample.png')); $imageData = base64_decode($imageDataEncoded); $source = imagecreatefromstring($imageData); $angle = 90; $rotate = imagerotate($source, $angle, 0); // if want to rotate the image $imageName = "hello1.png"; $imageSave = imagejpeg($rotate,$imageName,100); imagedestroy($source);
So Following is the php part of your program ..
NOTE
the change with commentChange is here
$uploadedPhotos = array('photo_1','photo_2','photo_3','photo_4'); foreach ($uploadedPhotos as $file) { if($this->input->post($file)){ $imageData = base64_decode($this->input->post($file)); // <-- **Change is here for variable name only** $photo = imagecreatefromstring($imageData); // <-- **Change is here** /* Set name of the photo for show in the form */ $this->session->set_userdata('upload_'.$file,'ant'); /*set time of the upload*/ if(!$this->session->userdata('uploading_on_datetime')){ $this->session->set_userdata('uploading_on_datetime',time()); } $datetime_upload = $this->session->userdata('uploading_on_datetime',true); /* create temp dir with time and user id */ $new_dir = 'temp/user_'.$this->session->userdata('user_id',true).'_on_'.$datetime_upload.'/'; if(!is_dir($new_dir)){ @mkdir($new_dir); } /* move uploaded file with new name */ // @file_put_contents( $new_dir.$file.'.jpg',imagejpeg($photo)); imagejpeg($photo,$new_dir.$file.'.jpg',100); // <-- **Change is here** } }
这篇关于PHP - 获得的base64 IMG串去code和保存为jpg(导致空图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!