2017-01-29 1 views
5

HTML 텍스트를 처리하는 데 ckeditor를 사용하고 있습니다. 이미지를 붙여 넣으면 ckeditor가 이미지 업로드를 수행합니다. 나는 pdf 라이브러리 인 TCPDF와 MPDF를 사용했다. 두 개의 별개의 오류가있다. 하나는 각 라이브러리에있다.업로드 된 이미지가 PDF 라이브러리에 오류를 발생합니다.

<?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); 
} 
?> 

이게 무슨 오류가 발생할 수 있습니다 : 다음과 같이 업로드 이미지

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에서 붙여 넣기 때?

편집 : 아약스 코드는 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); 
     } 
    } 
+0

이미지 파일이 저장된 디렉토리에 대한 사용 권한을 확인 (쓰기) 했습니까? – Johan

+0

이미지가 업로드됩니다, 내가 좋아, 이미지를 업로드 할 수있는 방법은 : 이미지를 붙여 넣을 때, 나는 base64 인코딩 이미지를 얻은 다음 base64 디코딩 된 문자열 @ 조한 –

+0

안녕하세요, 이미지의 base64 인코딩 된 문자열을 표시 할 수 있습니까? – mandar

답변

1

이미지가 서버에 성공적으로 업로드되고 pdf 라이브러리에서 오류가 발생하는 경우

여기서 가능한 이유는 pdf 라이브러리가 이미지 파일을 읽을 수 없다는 것입니다. 이미지 파일의 경로가 올바른지 확인하십시오.

PDF로 라이브러리 변환으로 HTML이 상대 경로를 기대해야 PDF로 예 : -

<img src="http://yourdomain/image_path"> 

나는 TCPDF 라이브러리를 테스트 라이브러리를 찾을 수 없습니다 경우 같은 문제와 같은 오류를 발견했다 이미지.

4

base64. 예를 들어 +와 =는 코드가 다르게 동작하게합니다. 또는 base64 인 코드의 16 진수 값을 사용하십시오.

편집 : 양식에 따라 다릅니다.

function escapeChars(){ 
    var value = document.getElementById("myTextarea").value; 
    value = value.replace("+", "%2b"); 
    value = value.replace("/", "%2f"); 
    value = value.replace("=", "%3d"); 
    document.getElementById("myTextarea").value = value; 
} 

또는 당신이 아약스를 사용하는 경우 바로 전송하기 전에 함수를 사용하지만이 기능을 사용하여 텍스트 영역을 변환하는 버튼을 설정할 수 있습니다. PHP에서

, 되돌릴 변경 : PHP에서 대신 $this->image_string = filter_input(INPUT_POST, 'image');

EDIT2 의

$this->image_string = str_replace(array("%2b", "%2f", "%3d"), array("+", "/", "="), $_POST['image']); 

: 자바 스크립트에서

<?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 = str_replace(array("%2b", "%2f", "%3d"), array("+", "/", "="), $_POST['image']); 

     $imgarr = explode(',', $this->image_string); 
     if(!isset($imgarr[1])){ 
      return; 
     } 
     $this->image_string = $imgarr[1]; 

     $namearray = explode('.', $imgarr[0]); 
     $ext = end($namearray); 
     if(!in_array($ext, array('jpg','png'))) 
      return false; 

     $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(){ 

     $this->image = base64_decode($this->image); 
     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); 
} 
?> 

전송 아약스 전에 :

function escapeChars(value){ 
    value = value.replace("+", "%2b"); 
    value = value.replace("/", "%2f"); 
    value = value.replace("=", "%3d"); 
    return value; 
} 

그런 다음 값에 escapeChars를 사용할 수 있습니다.

+0

설명은 확장 토론이 아닙니다. 이 대화는 [채팅으로 이동되었습니다] (http://chat.stackoverflow.com/rooms/135063/discussion-on-answer-by-sadlyblue-image-uploaded-causes-error-in-pdf-library). –

관련 문제