Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Image column or field does not support absolute image URLs #5600

Open
dimer47 opened this issue Aug 5, 2024 · 1 comment
Open

[Bug] Image column or field does not support absolute image URLs #5600

dimer47 opened this issue Aug 5, 2024 · 1 comment

Comments

@dimer47
Copy link
Contributor

dimer47 commented Aug 5, 2024

Bug report

What I did

I used the image field and column in a backpack crud.

What I expected to happen

I expected absolute image URLs to be supported and displayed as uploaded images.

What happened

Image URLs are not displayed in the resolved image with the specified disk. Currently, the column or field detects if it's a base64 image or a relative image that is uploaded.

What I've already tried to fix it

I added a condition to check if it is an absolute URL.

vendor/backpack/crud/src/resources/views/crud/columns/image.blade.php

     ...
    if($column['value'] instanceof \Closure) {
      $column['value'] = $column['value']($entry);
    }

    if (is_array($column['value'])) {
      $column['value'] = json_encode($column['value']);
    }

    if (preg_match('/^data\:image\//', $column['value'])) { // base64_image
      $href = $src = $column['value'];
+    } elseif (preg_match('/^https?\:\/\/.*$/i', $column['value'])) { // base64_image
+      $href = $src = $column['value'];
    } elseif (isset($column['disk'])) { // image from a different disk (like s3 bucket)

      if (!empty($column['temporary'])) {
          $href = $src = Storage::disk($column['disk'])->temporaryUrl($column['prefix'].$column['value'], now()->addMinutes((int) $column['expiration']));
      } else {
          $href = $src = Storage::disk($column['disk'])->url($column['prefix'].$column['value']);
      }

     ...

vendor/backpack/pro/resources/views/fields/image.blade.php

    ...
    if (! function_exists('maximumServerUploadSizeInBytes')) {
        function maximumServerUploadSizeInBytes() {

            $val = trim(ini_get('upload_max_filesize'));
            $last = strtolower($val[strlen($val)-1]);

            switch($last) {
                // The 'G' modifier is available since PHP 5.1.0
                case 'g':
                    $val = (int)$val * 1073741824;
                    break;
                case 'm':
                    $val = (int)$val * 1048576;
                    break;
                case 'k':
                    $val = (int)$val * 1024;
                    break;
            }

            return $val;
        }
    }

    // if value isn't a base 64 image, generate URL
-    if($value && !preg_match('/^data\:image\//', $value)) {
+    if ($value){
+        if(preg_match('/^https?\:\/\/.*$/i', $value)) { // base64_image
+            $imageUrl = $value;
+        } else if(!preg_match('/^data\:image\//', $value)) {
            // make sure to prepend the prefix once to value
            $imageUrl = Str::start($value, Str::finish($field['prefix'], '/'));

            // generate URL
            $imageUrl = getDiskUrl($field, $imageUrl);
        }
    }
    ...

Is it a bug in the latest version of Backpack?

Yes, this bug is present in the latest version of Laravel Backpack.

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
8.3.9

### PHP EXTENSIONS:
Core, date, libxml, openssl, pcre, zlib, filter, hash, json, pcntl, random, Reflection, SPL, session, standard, sodium, mysqlnd, PDO, xml, bcmath, calendar, ctype, curl, dom, mbstring, FFI, fileinfo, ftp, gd, gettext, iconv, igbinary, imagick, imap, intl, ldap, exif, msgpack, mysqli, pcov, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, readline, redis, shmop, SimpleXML, soap, sockets, sqlite3, sysvmsg, sysvsem, sysvshm, tokenizer, xmlreader, xmlwriter, xsl, zip, memcached, swoole, Zend OPcache, xdebug

### LARAVEL VERSION:
10.48.18.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.3.5
backpack/crud: 6.7.24
backpack/devtools: 2.0.3
backpack/editable-columns: 3.0.10
backpack/generators: v4.0.5
backpack/language-switcher: 2.0.0
backpack/logmanager: v5.0.2
backpack/pro: 2.2.11
backpack/theme-coreuiv2: 1.2.5
backpack/theme-coreuiv4: 1.1.2
backpack/theme-tabler: 1.2.11

Additional Note

I cannot create a PR because the image field is present in the Backpack Pro package.

@jcastroa87
Copy link
Member

Hello @dimer47 i dont think this is a bug, but is a new feature.

Let me assign this to @pxpm, he can check if this change is ok to be added on this column.

Thanks.

Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

3 participants