无法将图像上载到php codeigniter
在我的情况下,我有图片上传形式与调整大小的方法,但它失败了。 我有下面的代码,但不知道问题出在哪里。你有什么想法吗?在控制器check_base64_image($base64)中有一个函数,其中代码表示存在空值。我正在使用codeingiter的PHP无法将图像上载到php codeigniter,php,image,codeigniter,upload,Php,Image,Codeigniter,Upload,在我的情况下,我有图片上传形式与调整大小的方法,但它失败了。 我有下面的代码,但不知道问题出在哪里。你有什么想法吗?在控制器check_base64_image($base64)中有一个函数,其中代码表示存在空值。我正在使用codeingiter的PHP //----font end php html code with javascript------------// <input id="image1" name="image1" class="file" type="file"
//----font end php html code with javascript------------//
<input id="image1" name="image1" class="file" type="file" accept="image/*">
<button type="submit" class="btn btn-primary btn-tw" onclick="setup(); return false;">Submit</button>
<script>
$("#image1").fileinput({
'showPreview' : true,
'allowedFileExtensions' : ['jpg', 'png','gif'],
'showUpload' : false,
'maxFileCount':1,
'maxFileSize': 8000,
});
$('#image1').on('fileclear', function(event) {
img1 = null;
});
$('#image1').on('fileloaded', function(event, file, previewId, index, reader) {
img1 = file;
$("#uploadImgError").html('');
});
function getResizeImage(pic, callback)
{
var reader = new FileReader();
// Set the image once loaded into file reader
reader.onload = function(e)
{
// Create an image
//-----------------------------Image-------------------------
var img = document.createElement("img");
img.src = e.target.result;
var canvas = document.createElement("canvas");
//var canvas = $("<canvas>", {"id":"testing"})[0];
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var MAX_WIDTH = 800;
var MAX_HEIGHT = 800;
var width = img.width;
var height = img.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
var dataurl = canvas.toDataURL("image/png");
//document.getElementById('image').src = dataurl;
callback(dataurl);
}
reader.readAsDataURL(pic);
}
function isEmptyUploadFile(callback)
{
if(img1 == null && img2 == null && img3 == null && img4 == null && img5 == null)
callback(true);
else
callback(false);
}
function setForm(callback)
{
setImg1(function(data1)
{
if(data1 == true)
{
$('.progress-bar').css('width', 100+'%').attr('aria-valuenow', 100);
callback(true);
}
});
}
function setImg1(callback)
{
if(img1 != null)
{
getResizeImage(img1,function(tempPic)
{
document.getElementById('img1').value = tempPic;
getResizeThumbnailImage(img1,function(tempTPic)
{
document.getElementById('timg1').value = tempTPic;
$('.progress-bar').css('width', 20+'%').attr('aria-valuenow', 20);
callback(true);
});
});
}else{
$('.progress-bar').css('width', 20+'%').attr('aria-valuenow', 20);
callback(true);
}
}
function setup()
{
var myform = document.getElementById("newPost");
//console.log(myform.checkValidity());
if(!myform.checkValidity())
{
document.getElementById("validate").click();
return false;
}
isEmptyUploadFile(function(r)
{
var up1 = document.getElementById('image1').value;
var up2 = document.getElementById('image2').value;
var up3 = document.getElementById('image3').value;
var up4 = document.getElementById('image4').value;
var up5 = document.getElementById('image5').value;
if(r == true || (up1 == "" && up2 == "" && up3 == "" && up4 == "" && up5 == "") )
{
$("#uploadImgError").html('<em><span style="color:red"> <i class="icon-cancel-1 fa"></i> Please Upload at least one image!</span></em>');
//var loc = document.getElementById('uploadImgError'); //Getting Y of target element
//window.scrollTo(0, loc);
location.href = "#uploadImgError"; //Go to the target element.
return false;
}
else if(r == false)
{
$('#pleaseWaitDialog').modal('show');
setForm(function(data)
{
console.log(data);
if(data == true)
{
document.getElementById("newPost").submit();
}
return data;
});
}
});
}
</script>
// ------- controller ---------------------------//
$image1 = $this->input->post('img1');
$timage1 = $this->input->post('timg1');
$imgPath = '';
$timgPath = '';
$date=date_create();
if (isset($image1)) {
$currentTimeStamp = date_timestamp_get($date);
$imgPath = $upload_dir . '/['.$currentTimeStamp.']['.$userName.'].png';
$result = $this->uploadImg($image1, false, $imgPath);
}
if (isset($timage1)) {
$currentTimeStamp = date_timestamp_get($date);
$timgPath = $upload_dir . '/[T]['.$currentTimeStamp.']['.$userName.'].png';
$result = $this->uploadImg($timage1, true, $timgPath);
}
function uploadImg($input, $isThumbnail, $file)
{
if($input == null || $input == "")
{
return false;
}
$stringVal = $input;
$value = str_replace('data:image/png;base64,', '', $stringVal);
if ($this->check_base64_image($value) == false) {
return false;
}
$actualFile = base64_decode($value);
$img = imagecreatefromstring($actualFile);
$imgSize = getimagesize('data://application/octet-stream;base64,'.base64_encode($actualFile));
if ($img == false) {
return false;
}else
{
/*** maximum filesize allowed in bytes ***/
$max_file_length = 100000;
$maxFilesAllowed = 10;
log_message('debug', 'PRE UPLOADING!!!!!!!!');
if (isset($img)){
log_message('debug', 'UPLOADING!!!!!!!!');
// check the file is less than the maximum file size
if($imgSize['0'] > $max_file_length || $imgSize['1'] > $max_file_length)
{
log_message('debug', 'size!!!!!!!!'.print_r($imgSize));
$messages = "File size exceeds $max_file_size limit";
return false;
}else if (file_exists($file)) {
return false;
}else
{
return true;
}
}
function check_base64_image($base64) {
$img = imagecreatefromstring(base64_decode($base64));
// this code said null value.
if (!$img) {
return false;
}
imagepng($img, 'tmp.png');
$info = getimagesize('tmp.png');
unlink('tmp.png');
if ($info[0] > 0 && $info[1] > 0 && $info['mime']) {
return true;
}
return false;
}
//-------------------end --------//
/--font-end php html代码与javascript------------//
提交
$(“#图像1”).fileinput({
“showPreview”:正确,
'allowedFileExtensions':['jpg','png','gif'],
“showUpload”:错误,
“maxFileCount”:1,
“maxFileSize”:8000,
});
$('#image1')。on('fileclear',函数(事件){
img1=null;
});
$('#image1')。在('fileloaded',函数上(事件、文件、预览、索引、读取器){
img1=文件;
$(“#uploadImgError”).html(“”);
});
函数getResizeImage(pic,回调)
{
var reader=new FileReader();
//将图像加载到文件读取器后进行设置
reader.onload=函数(e)
{
//创建一个图像
//-----------------------------形象-------------------------
var img=document.createElement(“img”);
img.src=e.target.result;
var canvas=document.createElement(“canvas”);
//var canvas=$(“”,{“id”:“testing”}[0];
var ctx=canvas.getContext(“2d”);
ctx.drawImage(img,0,0);
var MAX_WIDTH=800;
var最大高度=800;
变量宽度=img.width;
var高度=img高度;
如果(宽度>高度){
如果(宽度>最大宽度){
高度*=最大宽度/宽度;
宽度=最大宽度;
}
}否则{
如果(高度>最大高度){
宽度*=最大高度/高度;
高度=最大高度;
}
}
画布宽度=宽度;
canvas.height=高度;
var ctx=canvas.getContext(“2d”);
ctx.drawImage(img,0,0,宽度,高度);
var dataurl=canvas.toDataURL(“image/png”);
//document.getElementById('image').src=dataurl;
回调(dataurl);
}
reader.readAsDataURL(pic);
}
函数isEmptyUploadFile(回调)
{
如果(img1==null&&img2==null&&img3==null&&img4==null&&img5==null)
回调(true);
其他的
回调(假);
}
函数集合形式(回调)
{
setImg1(功能(数据1)
{
如果(数据1==true)
{
$('.progress bar').css('width',100+'%').attr('aria-valuenow',100);
回调(true);
}
});
}
函数setImg1(回调)
{
如果(img1!=null)
{
getResizeImage(img1,函数(tempPic)
{
document.getElementById('img1')。value=tempPic;
getResizeThumbnailImage(img1,函数(PIC)
{
document.getElementById('timg1')。value=pic;
$('.progress bar').css('width',20+'%').attr('aria-valuenow',20);
回调(true);
});
});
}否则{
$('.progress bar').css('width',20+'%').attr('aria-valuenow',20);
回调(true);
}
}
函数设置()
{
var myform=document.getElementById(“newPost”);
//log(myform.checkValidity());
如果(!myform.checkValidity())
{
document.getElementById(“验证”)。单击();
返回false;
}
isEmptyUploadFile(函数(r)
{
var up1=document.getElementById('image1')。值;
var up2=document.getElementById('image2')。值;
var up3=document.getElementById('image3')。值;
var up4=document.getElementById('image4')。值;
var up5=document.getElementById('image5')。值;
如果(r==true | |(up1==“”&up2==“”&up3==“”&up4==“”&up5==“”)
{
$(“#uploadImgError”).html('请至少上传一张图片!');
//var loc=document.getElementById('uploadImgError');//获取目标元素的Y
//窗口。滚动至(0,位置);
location.href=“#uploadImgError”//转到目标元素。
返回false;
}
else if(r==false)
{
$('plesewaitdialog').modal('show');
setForm(函数(数据)
{
控制台日志(数据);
如果(数据==true)
{
document.getElementById(“newPost”).submit();
}
返回数据;
});
}
});
}
//------控制器---------------------------//
$image1=$this->input->post('img1');
$timage1=$this->input->post('timg1');
$imgPath='';
$timgPath='';
$date=date_create();
如果(isset($image1)){
$currentTimeStamp=date\u timestamp\u get($date);
$imgPath=$upload_dir./['.$currentTimeStamp.]['.$userName.].png';
$result=$this->uploadImg($image1,false,$imgPath);
}
如果(isset($timage1)){
$currentTimeStamp=date\u timestamp\u get($date);
$timgPath=$upload_dir./[T]['.$currentTimeStamp.]['.$userName.].png';
$result=$this->uploadImg($timage1,true,$timgPath);
}
函数uploadImg($input,$isThumbnail,$file)
{
如果($input==null | |$input==“”)
{
返回false;
}
$stringVal=$input;
$value=str_replace('data:image/png;base64',''.$stringVal);
如果($this->check_base64_image($value)=false){
返回false;