Using an external PHP lib in EspoCRM

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Firyo
    Senior Member
    • Jun 2022
    • 134

    Using an external PHP lib in EspoCRM

    Hi there,

    I'm currently trying to implement a external PHP lib (https://packagist.org/packages/intervention/image) into my EspoCRM.
    I intend to use the lib in an entrypoint to resize images on demand.

    I already tried multiple things without success
    - using composer with "composer require intervention/image" which broke my EspoCRM before I rolled back
    - Adding the source code of the lib in a specific folder "Vendor" in my custom extension. When I tried to `use` the `ImageManager`, an error were threw because it couldn't find the class.

    Thanks in advance for you help.

    Regards,
    Firyo.
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    If you want to use it inside your extension i suggest you follow these steps, https://github.com/espocrm/ext-templ...r-in-extension
    Rabii
    Web Dev

    Comment

    • Firyo
      Senior Member
      • Jun 2022
      • 134

      #3
      Originally posted by rabii
      If you want to use it inside your extension i suggest you follow these steps, https://github.com/espocrm/ext-templ...r-in-extension
      Thanks for your answer, I'll check this out,

      Comment

      • yuri
        Member
        • Mar 2014
        • 8440

        #4
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment

        • Firyo
          Senior Member
          • Jun 2022
          • 134

          #5
          Thanks for you answer, am I supposed to edit all the namespaces of all the php files in the lib I'm adding ?

          Comment

          • yuri
            Member
            • Mar 2014
            • 8440

            #6
            1. Install composer somewhere in your extension repositor
            2. When building extension, copy the vendor folder to your extension src folder (e.g. src/custom/Espo/Modules/YourModule/vendor).
            3. Create the autoload.json with autoloading mapping for your extension (src/custom/Espo/Modules/YourModule/Resources/autoload.json). This autoloading mapping you need to copy from the composer.json, but paths should be fixed to correspond the actual path to vendor when the extension is installed.
            Last edited by yuri; 06-01-2023, 03:19 PM.
            If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

            Comment

            • emillod
              Active Community Member
              • Apr 2017
              • 1405

              #7
              Here you have even video tutorial
              Auf YouTube findest du die angesagtesten Videos und Tracks. Außerdem kannst du eigene Inhalte hochladen und mit Freunden oder gleich der ganzen Welt teilen.

              Comment

              • esforim
                Active Community Member
                • Jan 2020
                • 2204

                #8
                Hope you share your success, I want to do be able to do something similar. I want trim down some of my upload size so database and EspoCRM don't become a giant.

                Right now I'm hesitating to upload PDF and just keep it on PC.

                Comment

                • Firyo
                  Senior Member
                  • Jun 2022
                  • 134

                  #9
                  Originally posted by espcrm
                  Hope you share your success, I want to do be able to do something similar. I want trim down some of my upload size so database and EspoCRM don't become a giant.

                  Right now I'm hesitating to upload PDF and just keep it on PC.
                  My current goal isn't to optimize my server or DB, just to reduce the loading time of the images I'm using on the public website linked to my EspoCRM.
                  Currently with the images at their original sizes the enduser have to download ~ 250Mb in ~ 35s (in WIFI) to have all the images loaded.

                  But yeah I'll share my results when I'll be done ?

                  Comment

                  • Firyo
                    Senior Member
                    • Jun 2022
                    • 134

                    #10
                    Ok, so after adding the lib into my composer.json (https://packagist.org/packages/intervention/image#2.7.2) a fatal error appear.

                    It seems that therequired in the lib in going into a conflict with the same lib used in Espo.

                    Am I stuck here or is there a solution ?


                    Click image for larger version

Name:	image.png
Views:	370
Size:	226.5 KB
ID:	93338

                    Comment

                    • yuri
                      Member
                      • Mar 2014
                      • 8440

                      #11
                      You can ignore a specific dependency in your composer. https://stackoverflow.com/questions/...omposer-config

                      Code:
                      "replace": {
                          "vendor/name": "*"
                      }​
                      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

                      Comment

                      • Firyo
                        Senior Member
                        • Jun 2022
                        • 134

                        #12
                        I've made a different change after posting my answer.
                        I've changed the version of the lib. I'm now in 3.0 (3.0.0-alpha.5).

                        The PHP code in my custom entrypoint is like this now.
                        Click image for larger version

Name:	image.png
Views:	341
Size:	45.9 KB
ID:	93345

                        Comment

                        • Firyo
                          Senior Member
                          • Jun 2022
                          • 134

                          #13
                          After testing the thing, I seems that some problems are coming.

                          I get this particular error when loading a lot of times the entrypoint (with differents ids) in my testing server.

                          Code:
                          [2023-06-06 19:39:19] ERROR: Uncaught Exception PDOException: "SQLSTATE[HY000] [1203] User {sql_username} already has more than 'max_user_connections' active connections" at /home/user/espocrmFolder/application/Espo/ORM/PDO/MysqlPDOFactory.php line 73 {"exception":"[object] (PDOException(code: 1203): SQLSTATE[HY000] [1203] User {sql_username} already has more than 'max_user_connections' active connections at /home/user/espocrmFolder/application/Espo/ORM/PDO/MysqlPDOFactory.php:73)"}
                          ​
                          It's slower to load on my local server but I dont get this error even after loading multiples times the page that load the entrypoints.

                          Did someone ever encounter this problem with an entrypoint or a custom route ?


                          EDIT :
                          When going back to the default behavior (clone of Entrypoints/Attachment) the errror is gone but the images take much longer time to load.
                          Last edited by Firyo; 06-06-2023, 08:21 PM.

                          Comment

                          • esforim
                            Active Community Member
                            • Jan 2020
                            • 2204

                            #14
                            I get this error whenever I use CLI hard rebuild option, or anything major really. But this error got nothing related to your issue I believe.

                            Comment

                            • Firyo
                              Senior Member
                              • Jun 2022
                              • 134

                              #15
                              I found a workarround for my case. I'll make the process of scaling down the image at the beforeSave of the "Document" entity.
                              This way the code that was throwing the error is executed in upsteam of the catalog viewing.

                              Comment

                              Working...