Import images base on URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clevercode
    Junior Member
    • Jun 2017
    • 6

    Import images base on URL

    Hello,

    It will be nice having this feature in core application. I have made this approach for anyone who wants to import images (tested for version 4.7.1):
    Modifications in : /application/Espo/Services/Import.php
    Added the following function:

    PHP Code:
    public function parseImageUrl( $url ) {
            $contents = '';
    
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
            $ret_val = curl_exec($curl);
            $contents =  chunk_split(base64_encode($ret_val));
            curl_close($curl);
    
            $exif = exif_read_data($url, 0, true);
    
            $contents = base64_decode($contents);
    
            $attachment = $this->getEntityManager()->getEntity('Attachment');
            $attachment->set("name", $exif["FILE"]["FileName"]);
            $attachment->set("type", $exif["FILE"]["MimeType"]);
            $attachment->set("size", $exif["FILE"]["FileSize"]);
            $attachment->set("role", "Attachment");
            $attachment->set("related_type", "Product");
            $this->getEntityManager()->saveEntity($attachment);
            $this->getFileStorageManager()->putContents($attachment, $contents);
    
            return $attachment->id;
        } 
    
    and modification in parseValue() function. I have added the following code:
    PHP Code:
    if ($field == 'imageId') {
            $value = $this->parseImageUrl($value);
    } 
    
    Note that is working for fieldname "image" and for Jpeg files only. Waiting for better solutions if you have. Thank you.
    Last edited by clevercode; 06-13-2017, 03:55 PM.
  • Patrick
    Member
    • Sep 2016
    • 46

    #2
    any reason why you encode $content ,and then decode it again?

    Comment

    • Patrick
      Member
      • Sep 2016
      • 46

      #3
      I tried not to encode/decode, it doesn't work.

      I tried to save $ret_val and $contents to file, it showed no difference... in diff

      It's really interesting. can somebody explain why?

      Comment

      • scotch_k
        Junior Member
        • Apr 2018
        • 14

        #4
        Originally posted by clevercode
        Hello,

        It will be nice having this feature in core application. I have made this approach for anyone who wants to import images (tested for version 4.7.1):
        Modifications in : /application/Espo/Services/Import.php
        Added the following function:

        PHP Code:
        public function parseImageUrl( $url ) {
        $contents = '';
        
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
        $ret_val = curl_exec($curl);
        $contents = chunk_split(base64_encode($ret_val));
        curl_close($curl);
        
        $exif = exif_read_data($url, 0, true);
        
        $contents = base64_decode($contents);
        
        $attachment = $this->getEntityManager()->getEntity('Attachment');
        $attachment->set("name", $exif["FILE"]["FileName"]);
        $attachment->set("type", $exif["FILE"]["MimeType"]);
        $attachment->set("size", $exif["FILE"]["FileSize"]);
        $attachment->set("role", "Attachment");
        $attachment->set("related_type", "Product");
        $this->getEntityManager()->saveEntity($attachment);
        $this->getFileStorageManager()->putContents($attachment, $contents);
        
        return $attachment->id;
        } 
        
        and modification in parseValue() function. I have added the following code:
        PHP Code:
        if ($field == 'imageId') {
        $value = $this->parseImageUrl($value);
        } 
        
        Note that is working for fieldname "image" and for Jpeg files only. Waiting for better solutions if you have. Thank you.
        Hi, does this add the image to the product module? If so, did you create a new entity field for it or use one of the exiting ones eg url - with a link to the image?
        Any help would be most appreciated. Thanks

        Comment

        • nunos
          Member
          • Mar 2018
          • 94

          #5
          Hi everyone,

          Any new / updated solution for this?

          Is the above code still working on version 8.1+ ?

          Thanks,

          Nunos

          Comment

          Working...