在自定义模块中通过所见即所得编辑器上传的图像的前端获取断开的链接


Getting Broken link on frontend for image uploaded via Wysiwyg editor in a custom module !

M 得到这个:

<img alt="" }}="" es_3.jpg="" wysiwyg="" src="{{media url=">

在我的表单中,我添加了此代码

$wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config')->getConfig(
     array('tab_id' => 'form_section')
);
$wysiwygConfig["files_browser_window_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg_images/index');
$wysiwygConfig["directives_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive');
$wysiwygConfig["directives_url_quoted"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive');
$wysiwygConfig["widget_window_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/widget/index');
$wysiwygConfig["files_browser_window_width"] = (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_width');
$wysiwygConfig["files_browser_window_height"] = (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_height');
$plugins = $wysiwygConfig->getData("plugins");
$plugins[0]["options"]["url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/system_variable/wysiwygPlugin');
$plugins[0]["options"]["onclick"]["subject"] = "MagentovariablePlugin.loadChooser('".Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/system_variable/wysiwygPlugin')."', '{{html_id}}');";
$plugins = $wysiwygConfig->setData("plugins",$plugins);

  $fieldset->addField('longdescription', 'editor', array(
      'name'      => 'longdescription',
      'label'     => Mage::helper('press')->__('Description'),
      'title'     => Mage::helper('press')->__('Description'),
      'style'     => 'width:500px; height:300px;',
      'config'    => $wysiwygConfig,

    ));

我仍然不清楚上面的代码,但我从某个地方复制了它,但我知道它可以浏览图像文件而不是编写自定义 url。

之后,我只是像这样在前端调用它:

<?php echo $item["longdescription"]; ?>

正在获取文本,但不是图像,对于图像,我得到了顶部提到的断开链接。

我错过了什么吗? 如果是,那又怎样?

添加以下代码:

<?php $_cmsHelper = Mage::helper('cms');?>
<?php $_process = $_cmsHelper->getBlockTemplateProcessor();?>
<?php echo $_process->filter($item["longdescription"]); ?>

看起来您可能在某处打破了引号,您如何传入图像的图像/URL?如果你看这里:alt=" }}=" es_3.jpg="",你可以看到你在某处传递了右括号,并且完全跳过了SRC。试着VAR_EXPORTing$item,告诉我你有什么。

我今天遇到了同样的问题。事实证明,Mage_Adminhtml_Cms_WysiwygController::directiveAction()尝试根据 url 创建图像。此函数调用Varien_Image_Adapter_Gd2::open(),而又会尝试打开文件。这是出错的地方:

图像

适配器尝试获取有关图像的信息,例如图像大小和 MIME 类型。但。。。当您的网站位于本地主机或流浪者盒子或类似的东西上时,服务器会尝试getimagesize('http://www.domain.com/image.jpg')而不是getimagesize('/Users/john/sites/domain.com/image.jpg')(例如(。

解决此问题的方法是覆盖您自己的模块中的directiveAction()以在引发异常之前添加另一个捕获:

public function directiveAction()
{
    $directive = $this->getRequest()->getParam('___directive');
    $directive = Mage::helper('core')->urlDecode($directive);
    $url = Mage::getModel('core/email_template_filter')->filter($directive);
    try {
        $image = Varien_Image_Adapter::factory('GD2');
        $image->open($url);
        $image->display();
    } catch (Exception $e) {
        // Try to get an absolute path:
        $path = Mage::getBaseDir().'/'.preg_replace('/http:'/'/(.*)'//Ui', '', $url);
        $image = Varien_Image_Adapter::factory('GD2');
        $image->open($path);
        $image->display();
    } catch (Exception $e) {
        $image = Varien_Image_Adapter::factory('GD2');
        $image->open(Mage::getSingleton('cms/wysiwyg_config')->getSkinImagePlaceholderUrl());
        $image->display();
    }
}

一个不错的小奖励:您的管理员中断开的链接现在也消失了! ;-(