How do I include images in a Magento module?

2019-09-15 02:30发布

问题:

I want the images to be displayed on the webpage, but included in the Magento module.

  1. Which folder do I put them in?
  2. How do I reference to them? (I'll need a variable for the module path.)

The documentation, various external tutorials and a Google search provides no help.

All I see under design/frontend/default/default folder is etc layout locale template and nowhere to put images.

Do I put them in the skin/frontend/default folder? If so, where?

回答1:

Assuming your module applies to Magento 1.4+, your images belong under ./skin/frontend/base/default/images/, ideally in a folder indicating your module's namespace and name, e.g. namespace_module. They can then be referenced in the following ways:

  • A module CSS file at base/default/css/namespace_module/file.css: url(../../images/namespace_module/image.jpg)

  • A template (or block): $this->getSkinUrl('images/namespace_module/image.jpg')

  • Anywhere: Mage::getDesign()->getSkinUrl('images/namespace_module/image.jpg');

FYI, nothing under the ./app/ directory is accessed (should be accessed) directly by the browser.



回答2:

An alternative to shipping your image as part of your extension, is to host it on your webserver (or someone else's) and then include it by URL in your extension.

If that's the case, and you want the image to show up on your basic admin pages, then you just put html directly into your system.xml (escape it first). Otherwise, if you're including it in your templates, just put it in with an img html tag.

Couple of benefits to this approach:

1) You can look at your Apache logs each month and see how many times your image got viewed (i.e how many people are installing your extension) and also, from the referer HTTP header, you can see which sites are using it too.

2) When your wildly successful internet company gets acquired, you can easily swap in a different logo.jpg on your webserver, without having to roll out a new version of your extension.

So it's not the right way, but it's a simple way with a couple of benefits.



标签: magento