私はckeditorを使用してHTMLテキストを作成しています。私はそれに画像を貼り付けると、ckeditorは画像のアップロードを行います。私はpdfライブラリのようにTCPDFとMPDFを使いました。私は2つの異なるエラーを受けました。それぞれのライブラリに1つずつあります。アップロードされた画像がPDFライブラリにエラーを起こします
mPDF error: IMAGE Error (SOURCE-IMAGE): Error parsing temporary file image object created with GD library to parse PNG image
TCPDF ERROR: [Image] Unable to get the size of the image: (SOURCE-IMAGE)
アップロードのイメージのための私のコードは次のようCKEditorバージョンでペーストがあるとき:
<?php
session_start();
class image{
private $save_path = 'uploads/';
private $image_string = '';
private $image_name = '';
private $image;
private $response = array();
public $loaded = false;
public function __construct(){
$this->response = array(
'error' => 1,
'message' => 'unknown error.'
);
$this->image_string = filter_input(INPUT_POST, 'image');
$ext = substr($this->image_string,11,3);
$randomLetters = $rand = substr(md5(microtime()),rand(0,26),6);
$imgnumber = count(scandir($this->save_path));
$this->image_name = "$imgnumber$randomLetters.$ext";
if(!empty($this->image_name) && !empty($this->image_string)){
$this->loaded = true;
}
}
public function save(){
if(!empty($this->image_name) && !empty($this->image_string)){
return $this->progress();
}
else{
$this->response['message'] = 'Error. Not all required infor is given.';
$this->response['error'] = 1;
return $this->response;
}
}
private function progress(){
$imgarr = explode(',', $this->image_string);
if(!isset($imgarr[1])){
$this->response['message'] = 'Error on post data image. String is not the expected string.';
$this->response['error'] = 1;
return $this->response;
}
$this->image = base64_decode($imgarr[1]);
if(!is_null($this->image)){
$file = $this->save_path . $this->image_name;
if(file_exists($file)){
$this->response['message'] = 'Image already exists on server.';
$this->response['error'] = 1;
return $this->response;
}
if(file_put_contents($file, $this->image) !== false){
$this->response['message'] = 'Image saved to server';
$this->response['error'] = 0;
$this->response['source'] = '../plugins/imageuploader/'.$file;
return $this->response;
}
else{
$this->response['error'] = 1;
$this->response['message'] = 'Error writing file to disk';
return $this->response;
}
}
else{
$this->response['message'] = 'Error decoding base64 string.';
return $this->response;
}
}
}
$img = new image();
if($img->loaded){
$result = $img->save();
echo json_encode($result);
}
else{
$result = array(
'error' => 1,
'message' => 'Not all post data given'
);
echo json_encode($result);
}
?>
このエラーが発生することがありますか?
編集: AjaxコードはCKEditorバージョンのコードの一部であり、一部はここにある、画像をbase64でコードのように来て:あなたはいくつかの文字をエンコードするために必要なデータを送信する前に
function h(a, d) {
if (a && "function" === typeof a.getAsFile) {
var b = a.getAsFile(), c = new FileReader;
c.onload = function (a) {
var fd = new FormData();
fd.append('image', a.target.result); //the base64 of image with format equals in src of tag img in html
$.ajax({
type: 'POST',
url: '../plugins/imageuploader/ajaxupload.php',
data: fd,
processData: false,
contentType: false,
dataType: 'json'
}).done(function(data) {
if((data.error == 0) && (typeof data.source != 'undefined')){
//alert(data.source);
var b = d.document.createElement("img", {attributes: {src: data.source}});
setTimeout(function() {
d.insertElement(b)
}, 10)
}else{
alert('Não foi possível carregar a imagem:\nErro - '+data.message); // show the message error if it can't be uploaded.
}
});
};
c.readAsDataURL(b);
}
}
イメージファイルが保存されているディレクトリのアクセス許可を確認(書き込み)しましたか? – Johan
画像がアップロードされ、私は真のような書き込み許可を設定します。これは大丈夫です。画像をアップロードする方法は次のとおりです。画像を貼り付けると、base64エンコードの画像が得られ、base64で復号された文字列@Johan –
こんにちは、Base64でエンコードされた画像の文字列を表示できますか? – mandar