dongluojiao6322 2017-10-05 07:24
浏览 32
已采纳

如何在元素视图中显示配置文件图像

<?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]

  • 写回答

1条回答 默认 最新

  • duanjiani6826 2017-10-05 09:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错