2017-01-29 40 views
5

HTML metnini yapmak için ckeditor kullanıyorum. Görüntüyü içine yapıştırdığımda, ckeditor görüntünün yüklenmesini yapar. TCPDF ve MPDF pdf kütüphanesi gibi kullandım, her kütüphanede iki ayrı hata var.Resim yüklendi PDF kitaplığında hata neden oluyor

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

Ne bu hataya neden olabilir: şöyle yükleme görüntü için

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)

Kodum CKEditor içinde macun olduğunda?

Düzenleme: ajax kodu, ckeditor kod parçası, parçası burada olduğu görüntü base64 kodu gibi gelir: bazı karakterleri kodlamak için gereken verileri göndermeden önce

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

Resim dosyasının saklandığı dizinde (yazma) izinlerini kontrol ettiniz mi? – Johan

+0

görüntü yüklenir, ben gerçek gibi yazma izinlerini, bu Tamam, resim yüklemek için yoludur ayarlayın: yapıştırma görüntü i görüntünün base64 encode olsun, sonra ben base64 dize deşifre Bir dosyada yazma @Johan –

+0

Merhaba, görüntünün base64 kodlu dizesini gösterebilir misin? – mandar

cevap

1

. Burada

olası nedeni pdf kütüphane, resim dosyası okumak görüntü dosyasının yolunu doğru olduğundan emin olmak için edemeyeceğini olabilir.

pdf kütüphane dönüştürür gibi html o göreli bir yol bekliyor olmalı pdf örn: -

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

Ben TCPDF kütüphane test edilmiş ve kütüphane bulamadı eğer aynı sorunu ve aynı hatayı bulduk görüntü.

4

base64. Örneğin + ve = kodun farklı davranmasına neden olur. Veya base64 kodlamasının hex değerlerini kullanın.

DÜZENLEME: forma bağlı.

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

veya ajax kullanmak eğer sadece göndermeden önce işlevini kullanın: Ama bu fonksiyonu kullanarak textarea dönüştürmek için bir düğmeye ayarlayabilirsiniz. php olarak

, geri değişiklikleri: php olarak yerine $this->image_string = filter_input(INPUT_POST, 'image');

EDIT2 ait

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

: JavaScript

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

gönderme ajax önce:

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

Ardından, escapeChars değerini kullanabilirsiniz. resim başarıyla sunucuya yüklenir ve pdf kütüphane hata atma ise

+0

Yorumlar uzun tartışmalar için değil; Bu sohbet [sohbet etmek için taşındı] (http://chat.stackoverflow.com/rooms/135063/discussion-on-answer-by-sadlyblue-image-uploaded-causes-error-in-pdf-library). –

İlgili konular