1

I am making a laravel application to serve as a backend to my website. I have an image (images) stored in a postgresql database. The format is bytea. I now want to display this image but it is showing me an error message.

Facade\Ignition\Exceptions\ViewException htmlspecialchars() expects parameter 1 to be string, resource given (View: C:\wamp64\www\WebTech\resources\views\shop\index-temp.blade.php)

I can't figure out whats wrong. Here is the code that is producing this error.

    @foreach($productsList as $product)
    <div class="col text-center">
       <a href="/products/{{$product->id}}">
          <img class="img-fluid" srcset="{{ $product->image_200 }} 200w, {{ $product->image_300 }} 300w"
                                 sizes="(max-width: 480px) 200px, 300px" 
                                 src="{{ $product->image_300 }}" alt="{{ $product->alt_text }}">
          <p class="mb-0 text-center text-white">{{ $product->name }}<br>{{ $product->cost }} eur</p>
          <p>{{$product->image_200}}</p>
      </a>
   </div>
   @endforeach

For clarification, the columns image_200, image_300 and image_500 are in bytea format in postgresql database. I have tried using the pg_unescape_bytea({{ $product->image_300 }}) function, but it doesn't help. What is the correct way to display an image in this way?

4
  • does this help? stackoverflow.com/questions/11329323/… Commented Nov 13, 2019 at 23:23
  • also it would be {{ pg_unescape_bytea($product->image_300) }} if you were trying to do what you had tried Commented Nov 13, 2019 at 23:33
  • The link doesn't help really, i tried it with the result of my page loading, but the image being broken. As to the second comment now i get this error: > pg_unescape_bytea() expects parameter 1 to be string, resource given Commented Nov 13, 2019 at 23:37
  • yes because it is still a resource that hasn't changed, i was just pointing out you were not doing what you thought you were doing ... you still need to get the contents from that resource before you can use it Commented Nov 13, 2019 at 23:39

1 Answer 1

1

As per https://stackoverflow.com/a/33499238/2109233 :

Try to get the stream contents, then unescape the bytea:

{{ pg_unescape_bytea(stream_get_contents($product->image_300)) }}

I am not sure what format that will end up in, if it can't go through htmlspeciachars replace {{ }} with {!! !!}.

You could move this into an accessor so you can just call $product->image_300 and have it converted for you.

You probably want to be using a data URI scheme for the image src attribute.

Update:

Apparently you may not need the pg_unescape_bytea function but you do need to use a Data URI scheme for the image source.

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried doing this, but as i said, the result is that the website shows (without errors), but the image is broken.
then look at the html source to see the value of the src attribute of the image tag so then you can figure out what is going wrong.
The output using this code $temp = stream_get_contents($product->image_200); $temp1 = pg_unescape_bytea($temp); $product->image_200 = "data:image/jpeg;base64," . base64_encode($temp1); is: data:image/jpeg;base64,/9j/4A== but it should be more like: data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4gJASUNDX1... I got the last one using this code: ob_start(); fpassthru($product->image_200); $contents = ob_get_contents(); ob_end_clean(); $product->image_200 = "data:image/png;base64," . base64_encode($contents);
Just wanted to update that I solved it using information above. The resulting code is: $temp1 = stream_get_contents($product->image_200); $product->image_200 = "data:image/png;base64," . base64_encode($temp1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.