php保存字符串流图片,PHP-获取base64 img字符串解码并另存为jpg(结果为空白图片)...

嗨,我实际上是通过ajax将base64图像字符串通过ajax发送到php脚本,该脚本仅解码字符串并将内容另存为.jpg文件。

但是结果是空白图像。

这怎么可能?

php脚本:

$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来说可以,因为echo $ photoTemp返回字符串。

我尝试过var_dump(@file_put_contents(

$new_dir.$file.'.jpg',$photoTemp));并且它返回,bool(true)因为保存了图像,但是图像中没有内容:(空图像

对于空图像,我的意思是说,文件已创建并命名,并且它具有与传递给php的内容相同的大小,但是当我尝试打开该图像进行预览时,它说,由于损坏或文件损坏而无法打开文件类型格式

无论如何,这是将照片作为base64并将其发送到php的JS:

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(''+d+'');

}

});

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值