2

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

链接显示为文本

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

链接显示为图像

我的图像存储在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'));}?>

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

控制器片段供参考:

    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;
}

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

4

2 回答 2

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)
);

或者直接使用 image 函数,因为image 函数支持

echo $this->Html->image(
    $imageUrl,
    array(
        'url' => $downloadUrl
    )
);
于 2013-07-27T16:51:46.967 回答
0

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

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

<?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;
    }
}

?>
于 2013-07-27T16:24:50.263 回答