2017-01-29 9 views
5

मैं HTML पाठ करने के लिए ckeditor का उपयोग कर रहा हूं। जब मैं इसमें एक छवि पेस्ट करता हूं, तो 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); 
} 
?> 

क्या इस त्रुटि पैदा कर सकता है: इस प्रकार

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 में पेस्ट है?

संपादित करें: 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); 
     } 
    } 
+0

क्या आपने निर्देशिका फ़ाइल को संग्रहीत किया है जहां छवि फ़ाइल संग्रहीत की जाती है? – Johan

+0

छवि अपलोड की गई है, मैंने लिखने की अनुमतियों को सत्य की तरह सेट किया है, यह ठीक है, छवियों को अपलोड करने का तरीका यह है: जब पेस्ट छवि, मुझे छवि का बेस 64 एन्कोड मिलता है, तो मैं बेस 64 डीकोडेड स्ट्रिंग @Johan फ़ाइल में लिखता हूं –

+0

हाय, क्या आप छवि की बेस 64 एन्कोडेड स्ट्रिंग दिखा सकते हैं? – mandar

उत्तर

1

छवि सफलतापूर्वक सर्वर पर अपलोड किया जाता है और पीडीएफ पुस्तकालय त्रुटि फेंक रहा है।

संभव यहाँ कारण हो सकता है कि पीडीएफ पुस्तकालय, छवि फ़ाइल को पढ़ने के लिए सुनिश्चित छवि फ़ाइल के लिए पथ सही है करने में असमर्थ है।

पीडीएफ पुस्तकालय धर्मान्तरित के रूप में एचटीएमएल PDF में यह एक रिश्तेदार पथ की उम्मीद की जानी चाहिए जैसे: -

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

मैं tcpdf पुस्तकालय का परीक्षण किया और एक ही मुद्दा है और एक ही त्रुटि पाई है, तो पुस्तकालय खोजने में असमर्थ था है छवि।

4

आप में से कुछ वर्ण एन्कोड करने के लिए की जरूरत है बेस 64। उदाहरण के लिए + और = कोड अलग-अलग व्यवहार करने का कारण बन जाएगा। या बेस 64 एन्कोड के हेक्स मान पूर्णांक का उपयोग करें।

संपादित करें: प्रपत्र पर निर्भर करता है। लेकिन आप इस समारोह का उपयोग कर पाठ क्षेत्र में परिवर्तित करने के लिए एक बटन सेट कर सकते हैं:

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

या यदि आप ajax का उपयोग सिर्फ भेजने से पहले समारोह का उपयोग करें।

php में, वापस लौटने के परिवर्तन:

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

EDIT2 के बजाय

php में:

<?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); 
} 
?> 
जावास्क्रिप्ट में

ajax भेजने से पहले:

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)। –

संबंधित मुद्दे