Cakephp 如何生成作为链接的图像?
现在,我的图像链接如下所示: 我需要它们看起来像这样: 我的图像存储在Cakephp 如何生成作为链接的图像?,cakephp,cakephp-2.0,cakephp-2.1,image-uploading,Cakephp,Cakephp 2.0,Cakephp 2.1,Image Uploading,现在,我的图像链接如下所示: 我需要它们看起来像这样: 我的图像存储在APP/uploads/userid/Images/filename.jpg 这是我目前的看法: <?php foreach($file as $files){?> <?php echo $this->Html->link($files['Image']['filename'], array('controller' => 'images', 'action' =&g
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'));}?>
我需要做什么才能使图像显示为链接而不是文件名?这是一份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;
}
}
?>
如何生成图像链接
问题中有这一行(为了清楚起见进行了解释):
而不是链接到文件名-链接到图像:
echo $this->Html->link(
$this->Html->image($imageUrl),
$downloadUrl,
array('class' => 'frame', 'escape' => false)
);
或者直接使用图像功能,因为:
我想显示一个图像而不是一个链接。。。希望你能理解。。。“立即联机写入”文件名在“查看”页面上回响,而不是图像…@AD7six抱歉写入错误。我已经更新了我的问题,谢谢你的帮助,先生。。。真的非常感谢你…如果我做错了什么,我向你道歉抱歉@AD7six它起作用了。。。我没有看到你更新的答案。。。谢谢,我试过了。。它起作用了。。中间的代码..k让人困惑-然后答案被取消删除,向下投票被删除。我不确定问题的确切内容-但我不认为这能回答问题。@AD7six我以为他想在不触发
另存为..
提示的情况下将图像文件推送到浏览器?但是这个问题很难理解。@hellosheikh哦,用$this->Html->image代替$this->Html->link@hellosheikh@MathewFoscarini我也尝试过这个。。但是现在imgsrc中的url变成了localhost/cakephp/img/picture1.jpg,这实际上是错误的。
$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)
);
echo $this->Html->image(
$imageUrl,
array(
'url' => $downloadUrl
)
);