【问题标题】:How to generate an image that is a link?如何生成作为链接的图像?
【发布时间】:2013-07-27 16:17:29
【问题描述】:

现在我的图片链接如下所示:

我需要它们看起来像这样:

我的图片存储在APP/uploads/userid/images/filename.jpg

这是我目前的看法:

  <?php foreach($file as $files){?>

      <?php  echo $this->Html->link($files['Image']['filename'], array('controller' => 'images', 'action' => 'downloadImages', $files['Image']['filename']), array('class' => 'frame'));}?>

它可以正常工作,点击链接可以正确显示相关图像。

控制器sn-p供参考:

    public function downloadImages($filename) {


    $download = !empty($_GET['download']);
    $idUser = $this->Auth->user('idUser');
    $folder_url = APP.'uploads/'.$idUser.'/'.'images'.'/'.$filename;

    $this->response->file($folder_url, array('download' => $download, 'name' =>   $filename));

    return $this->response;
}

我需要做什么才能使图像显示为链接而不是文件名?

【问题讨论】:

  • 我想显示图像而不是链接...希望您理解...现在写在线文件名在查看页面上回显而不是图像...
  • @AD7six 很抱歉写得不好。我已经更新了我的问题
  • 好吧,谢谢你帮助我先生......真的很感谢你..如果我做错了什么,我深表歉意
  • 抱歉@AD7six 它可以工作...好吧我没有看到您更新的答案...谢谢你我试过了..它工作..中间的代码..
  • k 这令人困惑 - 答案未删除,并且已删除投票。

标签: cakephp cakephp-2.0 cakephp-2.1 image-uploading


【解决方案1】:

如何生成图片链接

在问题中有这样一行(为了清楚起见,转述):

$downloadUrl = array('controller' => 'images', 'action' => 'downloadImages', $files['Image']['filename'], '?' => array('download' => true));
$imageUrl = array('controller' => 'images', 'action' => 'downloadImages', $files['Image']['filename']);

echo $this->Html->link(
    $files['Image']['filename'], 
    $downloadUrl,
    array('class' => 'frame')
);

而不是链接到文件名 - 链接到图像:

echo $this->Html->link(
    $this->Html->image($imageUrl),
    $downloadUrl,
    array('class' => 'frame', 'escape' => false)
);

或者直接使用图片功能,因为the image function supports that:

echo $this->Html->image(
    $imageUrl,
    array(
        'url' => $downloadUrl
    )
);

【讨论】:

    【解决方案2】:

    这里是 y GemsImageComponent 的副本,我用来将图像从磁盘推送到浏览器。它处理缓存,并使用文件时间戳来查看是否应该再次发送图像,或者浏览器的当前缓存版本是否是最新的。

    如果你觉得这有用,请投票。

    <?php
    
    /**
     * $settings['cache'] string (optional) How long the browser should cache
     * images.
     * $settings['expires'] string (optional) If all images should expire after a
     * given period from the browsers cache.
     */
    class GemsImageComponent extends Component
    {
    
        /**
         *
         * @var array The default settings for the component.
         */
        protected $defaults = array(
                'cache' => '+7 days',
                'expires' => false
        );
    
        /**
         *
         * @var Controller The controller using this component.
         */
        protected $controller;
    
        /**
         *
         * @see Component::__construct()
         */
        public function __construct(ComponentCollection $collection,
                $settings = array())
        {
            parent::__construct($collection,
                    Hash::merge($this->defaults, $settings));
        }
    
        /**
         *
         * @see Component::startup()
         */
        public function startup(Controller $controller)
        {
            $this->controller = $controller;
        }
    
        /**
         * Sends an image from disk to the browser.
         *
         * @param string $file The full path to the image file.
         * @return mixed The value that the View should return.
         */
        public function send($file)
        {
            $response = $this->controller->response;
            $settings = $this->settings;
    
            // the file has to exist
            if(! file_exists($file))
            {
                throw new NotFoundException();
            }
    
            // set browser cache options
            if($settings['cache'] !== false)
            {
                $response->cache('-1 minute', $settings['cache']);
            }
            if($settings['expires'] !== false)
            {
                $response->expires($settings['expires']);
            }
    
            // mark this image with a timestamp of when it last changed.
            $timestamp = filemtime($file);
            $response->modified($timestamp);
    
            // generate a unique ID that browser cache engines can use to track this
            // resource.
            // TODO: Add GEMS version number to the hash tag.
            $unique = sha1(sprintf('%s-%d', $file, $timestamp));
            $response->etag($unique);
    
            // check if we can abort sending this resource
            if(! $response->checkNotModified($this->controller->request))
            {
                $response->file($file, array(
                        'download' => false
                ));
            }
    
            return $response;
        }
    }
    
    ?>
    

    【讨论】:

    • 我不确定这个问题到底是什么 - 但我认为这不能回答它。
    • @AD7six 我以为他想在不触发save as.. 提示的情况下将图像文件推送到浏览器?但这个问题很难理解。
    • @hellosheikh 哦,使用 $this->Html->image 代替 $this->Html->link
    猜你喜欢
    • 1970-01-01
    • 2012-08-18
    • 2017-12-31
    • 2022-09-29
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多