简体   繁体   English

如何在cms Magento中缩放图像

[英]How to scale image in cms Magento

When the admin adds an image to a cms page in Magento, and then resize the image: then the image at the frontend retains its original size, but scaled. 当管理员将图像添加到Magento中的cms页面,然后调整图像大小:然后前端的图像保留其原始大小,但缩放。

Is there any solution to save the image in smaller size when the page is saved? 是否有任何解决方案可以在保存页面时以较小的尺寸保存图像?

There's no way this will happen with any default magento. 任何默认的magento都不会发生这种情况。 However, you could put together a module that will observe your cms page saved data. 但是,您可以组合一个模块来观察您的cms页面保存的数据。 Take for instance this event: 比如这个事件:

cms_page_prepare_save

with parameters: 带参数:

array(’page’ ⇒ $model, ‘request’ ⇒ $this→getRequest())

You can create the module starting with only 3 files: 您只需3个文件即可创建模块:

/app/etc/Electricjesus_Cms.xml: /app/etc/Electricjesus_Cms.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Electricjesus_Cms>
            <active>true</active>
            <codePool>local</codePool>
        </Electricjesus_Cms>
    </modules>
</config>

/app/code/local/Electricjesus/Cms/etc/config.xml: /app/code/local/Electricjesus/Cms/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Electricjesus_Cms>
            <version>0.1.0</version>
        </Electricjesus_Cms>
    </modules>
    <global>
        <models>
            <cms>
                <class>Electricjesus_Cms_Model</class>
            </cms>
        </models>
        <events>
            <cms_page_prepare_save>
                <observers>
                    <prepareSave>
                        <class>cms/observer</class>
                        <method>prepareSave</method>
                    </prepareSave>
                </observers>
            </cms_page_prepare_save>
        </events>        
    </global>
</config>

/app/code/local/Electricjesus/Cms/Model/Observer.php: /app/code/local/Electricjesus/Cms/Model/Observer.php:

<?php
class Electricjesus_Cms_Model_Observer {
    function prepareSave ( $observer ) {
       $request = $observer->getRequest();
       $params = $request->getParams();
       // scan the request and params for stuff related with the images.. 
       // find out scaling and pass into a good resizer like TimThumb or
       // Zend_Filter_ImageSize etc.
    }
}

While the above answer is not complete, this is a good place to start. 虽然上述答案尚未完成,但这是一个很好的起点。 I'll try and develop a complete solution whenever I have more time on it. 每当我有更多时间,我会尝试开发一个完整的解决方案。

Alternative method: 替代方法:

Since I mentioned TimThumb above, you can also deploy that at your magento root and start adding your images via HTML editor instead of the usual WYSIWYG editor. 由于我上面提到过TimThumb ,你也可以在magento root上部署它,然后开始通过HTML编辑器而不是通常的WYSIWYG编辑器添加图像。 If installing thumb.php on your website root directory, you can use it like so: 如果在您的网站根目录上安装thumb.php,您可以像这样使用它:

<img src="/thumb.php?src=images/image.jpg&w=100&h=50" alt="Hey" />

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM