Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.08k stars 886 forks source link

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

Open dimer47 opened 1 month ago

dimer47 commented 1 month ago

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 commented 2 weeks ago

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.