Announcement

Collapse
No announcement yet.

Import images base on URL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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($curlCURLOPT_RETURNTRANSFERtrue );
            
    $ret_val curl_exec($curl);
            
    $contents =  chunk_split(base64_encode($ret_val));
            
    curl_close($curl);

            
    $exif exif_read_data($url0true);

            
    $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.

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

    Comment


    • #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


      • #4
        Originally posted by clevercode View Post
        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($curlCURLOPT_RETURNTRANSFERtrue );
        $ret_val curl_exec($curl);
        $contents chunk_split(base64_encode($ret_val));
        curl_close($curl);

        $exif exif_read_data($url0true);

        $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

        Working...
        X