0
<?php
public function beforeFilter(\Cake\Event\Event $event)
 {

  $user = $this->request->session()->read('Auth.User');
  $this->set('username', $user['username']);

  $this->set('profile', $user['profile_pic'])
 }
 ?>

In the above code, I can print the username inside Element/folder/nav.ctp

<?php 
    echo $this->Html->image('users/' .stream_get_contents($profile),array('width'=>'100px','height'=>'100px'));
?>

when i print the image, warning messages below.

Warning (2): stream_get_contents() expects parameter 1 to be resource, integer given [APP/Template/Element/folder/nav.ctp, line 19]

3gth
  • 550
  • 5
  • 23
distromob
  • 354
  • 1
  • 10
  • do you store the image data inside the database or just the file name? – arilia Oct 05 '17 at 07:56
  • my image field is in longblob, i stored in database – distromob Oct 05 '17 at 07:58
  • i also have a field name "destination" example /opt/lampp/htdocs/mycakeapps/webroot/img/users/profile2.png – distromob Oct 05 '17 at 08:10
  • does the file profile2.png actually exists? In that case you can pass just the destination to the Html helper. Something like : `$this->Html->image($destination, ...` – arilia Oct 05 '17 at 08:45
  • Storing images in a DB that is not explicitly made for this task is usually considered bad practice. Store it in a storage backend that is made for that purpose and refer the location in the DB. – floriank Oct 05 '17 at 09:48
  • @arilia how can i eliminate " /opt/lampp/htdocs" in the path, i cant still display the image because of that extra path – distromob Oct 05 '17 at 13:54
  • http://php.net/manual/en/function.pathinfo.php – arilia Oct 05 '17 at 14:09
  • @arilia if I echo $path_parts['basename'], "\n"; Only the image filename will output when I debug.. please help me – distromob Oct 05 '17 at 23:18
  • thank you for the suggestion i finaly view my image – distromob Oct 06 '17 at 04:33

1 Answers1

1

Read the error message, it is very clear:

stream_get_contents() expects parameter 1 to be resource, integer given

So you pass an int and not a resource. First thing in this case is to lookup what a method expects. See the php documentation for stream_get_contents(). It obviously wants a stream resource. Look up what a stream and a resource are and then act accordingly.

However, what you try won't work this way because you're not accessing file in the file system. See the documentation on fopen().

You'll have to echo your image as base64 encoded binary data in your markup. See the answer here: Is it possible to put binary image data into html markup and then get the image displayed as usual in any browser? This will increase the size of the image by ~30% due to the base64 encoding.

I wouldn't do this for many large images. Never done it myself with many but I'm pretty sure the browser will get performance issues. Be aware that there might be security issues with this solution as well besides possible performance issues. I could imagine that it is possible to exploit this to inject unwanted things into the pages markup if the data is not properly handled.

Also storing images in a DB that is not explicitly made for this task is usually considered bad practice. Store it in a storage backend that is made for that purpose and refer the location in the DB.

For the future remember to read and lookup error messages if you don't understand them and read documentation if you don't know what they do.

floriank
  • 25,546
  • 9
  • 42
  • 66