PHP - get base64 img string decode and save as jpg (resulting empty image )(PHP - 获取base64 img字符串解码并保存为jpg(产生空图像))
问题描述
我实际上是通过 ajax 将 base64 图像字符串发送到 php 脚本,该脚本只是解码字符串并将内容保存为 .jpg 文件.
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.
但结果是一个空图像.
这怎么可能?
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 返回字符串.
For ajax it is ok because, echo $photoTemp returns the string.
我试过 var_dump(@file_put_contents( $new_dir.$file.'.jpg',$photoTemp));
并且它返回 bool(true)
因为图像已保存但图像中没有内容:(空图像
i tried var_dump(@file_put_contents( $new_dir.$file.'.jpg',$photoTemp));
and it returns bool(true)
since the image is saved but there is no content in the image :( empty image
对于空图像,我的意思是,文件被创建并命名,它具有相同的我传递给 php 的内容的大小,但是当我尝试打开该图像时预览它说,文件无法打开,因为文件类型格式损坏或错误
for empty image i mean , file is created and named, and it has the 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 because corrupted or bad file type format
无论如何这是将照片作为base64并将其发送到php的JS:
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,你必须使用图像函数 imagecreatefromstring, imagejpeg 来创建图像.
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);
希望这会有所帮助.
带有图像数据的 PHP 代码
$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);
所以下面是你程序的 php 部分.. NOTE
带有注释的更改 Change is here
So Following is the php part of your program .. NOTE
the change with comment Change 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字符串解码并保存为jpg(产生空图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP - 获取base64 img字符串解码并保存为jpg(产生空图像)
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01