【发布时间】:2017-06-07 09:21:59
【问题描述】:
我正在编写一个扩展 HtmlHelper 的自定义帮助程序,并覆盖 \HtmlHelper::image() 方法来计算图像尺寸并将它们添加为 HTML 属性。到目前为止,我所拥有的对于普通图片来说效果很好:
public function image($path, $options = array()) {
if (!array_key_exists('width', $options) && !array_key_exists('height', $options)) {
$stamp = Configure::read('Asset.timestamp');
Configure::write('Asset.timestamp', false);
$path = $this->assetUrl($path, $options + array('pathPrefix' => Configure::read('App.imageBaseUrl')));
list($width, $height) = @getimagesize(rtrim(WWW_ROOT, '\\/') . $path);
if (!is_null($width)) {
$options['width'] = $width;
}
if (!is_null($height)) {
$options['height'] = $height;
}
Configure::write('Asset.timestamp', $stamp);
}
return parent::image($path, $options);
}
……但有以下缺陷:
-
来自插件的图片不能位于磁盘上(它们应该),例如:
echo $this->Html->image('/debug_kit/img/cake.icon.png', array('alt' => 'CakePHP'));... 生成此文件系统路径:
…\src\webroot/debug_kit/img/cake.icon.png...因此
getimagesize()失败,因为实际位置是:…\src\Plugin\DebugKit\webroot\img\cake.icon.png" -
外部图片(应忽略)经过全流程:
echo $this->Html->image('http://placekitten.com/200/300'); …\src\webroothttp://placekitten.com/200/300
我一直在寻找一种内置方法来将 CakePHP 图片 URL(以\HtmlHelper::image() 接受的任何格式转换为文件系统路径(o 类似 null 时不适用)但我不能找到任何。需要磁盘路径的本机功能,例如 \Helper::assetTimestamp() 被包装在大量不可重用的代码中。
有没有优雅的解决方案?
【问题讨论】:
标签: cakephp cakephp-2.9