【问题标题】:How to resize images at upload in Magento (for real)?如何在 Magento 上传时调整图像大小(真实)?
【发布时间】:2014-02-07 14:19:41
【问题描述】:

谁能告诉我 Magento 中的哪个类/方法应该被覆盖(在插件中)以便在上传时自动调整图像大小?

目的是在 Magento 设置中设置最大宽度/高度,然后在此类/方法中使用这些设置。

这个问题至少被问了 2 或 3 次,但出于多种原因,我不认为“Magento 自动制作不同的版本”是可以接受的答案:

  • 后端用户有时会上传 10Mpixels 左右的高清图片
  • 10Mpixels 的图像在一个简单的网上商店中是绝对必要的(至少在几年内是这样)
  • 它占用硬盘、备份磁盘并且可以很快填满磁盘配额,锁定Magento
  • 当问题只是“如何”时,讨论“为什么”是没有意义的

谢谢你:)

【问题讨论】:

    标签: php magento image-uploading


    【解决方案1】:

    我们创建了一个简单的 Magento 插件,可以在上传时调整图像大小。您可以配置最大宽度和高度或将其禁用:https://github.com/ircf/magento-resize-at-upload

    这个插件将很快添加到magento connect中。

    任何人都可以选择使用或不使用,这是另一个问题。

    【讨论】:

      【解决方案2】:

      我们在没有 Magento 但直接 PHP 的情况下做了类似的事情,也许你也可以这样做,我不知道你的里程是多少。

      在 Linux 上安装 ImageMagick 并在此处阅读该站点上的命令行工具:Magick commands

      了解所需的命令语法后,请执行以下步骤:

      • 将二进制文件上传到具有唯一名称的临时目录(使用 PHP 的 uniqueid());
      • 使用 PHP 的 exec() 语句触发转换;
      • 将文件移动到最终目标文件夹(使用 PHP 的 rename() 语句);
      • 取消链接(删除)临时文件(使用 PHP 的 unlink() 语句)。

      这是很多工作,但一旦完成,它现在可以在我们拥有的许多系统上运行良好。

      【讨论】:

      • 感谢您的回答,我计划实现类似的东西,除了我想使用 magento 和 varien API 来上传和调整图像大小,所以这个功能可以打包在 Magento 插件中轻松启用/禁用
      【解决方案3】:

      Magento 的产品图像大小调整功能已经非常强大,在任何情况下您都不应考虑更换它。没有意义。

      任何上传 10Mega 像素图像且未经优化的后端用户都不应是网站管理员。任何产品摄影都应始终针对网络进行优化,甚至更优化以适合您的商店模板。

      让我们从不同的角度看看如何 - 您将 1200 像素 X 1200 像素的图像上传到 magento 管理员(优化后应小于 150kb),而在前端,您的响应式模板显示的最大产品图像约为 600 像素,视网膜版本仍然是1200px。

      使用图片元素或 webkits 接受的 srcset,您可以从单个高分辨率图像快速定义不同屏幕宽度和密度的新分辨率,因为这会动态创建这些尺寸并存储在缓存中。

      如果存储有问题,您可以随时刷新缓存以节省存储空间并清理旧的产品图像,但是如果存储有问题,我建议升级您的主机,因为在系统中寻找黑客以适应较差的硬件是没有意义的基础设施。

      考虑使用 CDN 和其他基于云的系统作为备份手段,而忽略您自己本地备份中的媒体文件夹。

      【讨论】:

      • 后端用户不是网站管理员,而是一个简单的商家,所以是的,它确实有意义。目的不是要取代 magnto 调整大小的方法,而只是确保将原始图片限制为合理的格式。 Magento 旨在让简单用户无需准备图像即可访问。这与糟糕的基础设施无关,而是保持合理(负担得起的)磁盘空间。此外,我不想在这里讨论“为什么”,只是“如何”,更准确地说是要覆盖什么方法。不知道就不要回答了。
      • 傲慢不会让任何人在任何地方,我的回答不是我不知道如何做,而只是解释做某事的更好方法。如果你想拆分 Magento,首先查看 Varien_Image,然后使用 GD 库进行扩展。
      【解决方案4】:

      我已经编写了代码,用于根据您想要的宽度和高度在 Magento 2 中调整任何产品图像的大小。我将描述如何在 Magento2 中调整图像大小,在 Magento2 中更新产品图像,在 Magento 2 中删除白框,在 Magento 2 中更改产品图像尺寸,例如需要调整自定义图像的大小并将其显示在网站上特殊大小的区域。

      对于 Magento 2,您可以使用以下代码在 Magento 2 中调整图像大小

      第一步:你需要在 Vender\Module\Helper\Image.php 和下面的代码中创建帮助类文件 Image.php。

      < ?php
      namespace Vender\Module\Helper;
      use Magento\Framework\Filesystem;
      use Magento\Framework\Url;
      use Magento\Framework\App\Helper\AbstractHelper;
      use Magento\Framework\App\Filesystem\DirectoryList;
      class Image extends \Magento\Framework\App\Helper\AbstractHelper {
          protected $scopeConfig;
          protected $storeManager;
          protected $messageManager;
          protected $_response;
          protected $_resourceConfig;
          protected $_responseFactory;
          protected $_url;
          protected $_filesystem;
          protected $_directory;
          protected $_imageFactory;
          public function __construct(
              \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
              \Magento\Store\Model\StoreManagerInterface $storeManager,
              \Magento\Framework\Message\ManagerInterface $messageManager,
              \Magento\Framework\App\ResponseInterface $response,
              \Magento\Framework\App\Config\Storage\WriterInterface $resourceConfig,
              \Magento\Framework\App\ResponseFactory $responseFactory,
              \Magento\Framework\UrlInterface $url,
              \Magento\Framework\Filesystem $filesystem,
              \Magento\Framework\Image\AdapterFactory $imageFactory
          )
          {
              $this->scopeConfig = $scopeConfig;
              $this->_storeManager=$storeManager;
              $this->messageManager=$messageManager;
              $this->_response=$response;
              $this->_resourceConfig=$resourceConfig;
               $this->_responseFactory = $responseFactory;
              $this->_url = $url;
              $this->_filesystem = $filesystem;
              $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
              $this->_imageFactory = $imageFactory;
          }
          public function imageResize(
          $src,
          $width=35,
          $height=35,
          $dir='resized/'
          ){
              $absPath = $this->_filesystem
              ->getDirectoryRead(DirectoryList::MEDIA)
              ->getAbsolutePath().$src;
              $imageResized = $this->_filesystem
              ->getDirectoryRead(DirectoryList::MEDIA)
              ->getAbsolutePath($dir).
              $this->getNewDirectoryImage($src);
              $imageResize = $this->_imageFactory->create(); 
              $imageResize->open($absPath);
              $imageResize->backgroundColor([255, 255, 255]);
              $imageResize->constrainOnly(TRUE);
              $imageResize->keepTransparency(TRUE);
              $imageResize->keepFrame(true);
              $imageResize->keepAspectRatio(true);
              $imageResize->resize($width,$height);
              $dest = $imageResized ;
              $imageResize->save($dest);
              $resizedURL= $this->_storeManager->getStore()
              ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).
              $dir.$this->getNewDirectoryImage($src);
              return $resizedURL;
          }
          public function getNewDirectoryImage($src){
              $segments = array_reverse(explode('/',$src));
              $first_dir = substr($segments[0],0,1);
              $second_dir = substr($segments[0],1,1);
              return 'cache/'.$first_dir.'/'.$second_dir.'/'.$segments[0];
          }
      }
      

      第 2 步:使用下面的代码,您可以从任何类、块或模板调用上述 imageResize() 方法。

      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $imgpath = $objectManager->create('Vender\Module\Helper\Image')->imageResize('IMAGE_PATH','50','50','YOUR_DIR_NAME/');
      

      现在我将解释我使用的方法 1.getDirectoryRead() 2.getAbsolutePath() 3. 背景颜色() 4. 约束() 5.保持透明() 6. 保持帧() 7.keepAspectRatio()

      Magento 2 – 从帧中正确删除白色图像

      < ?php foreach ($this->getGalleryImages() as $_image): ?>
      
      •  
                   helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(56); ?>" width="56" height="56" alt="< ?php echo $this->htmlEscape($_image->getLabel()) ?>" />
      
          < ?php endforeach; ?>
      

      如果您想在 magento 2 中调整图像大小并且想了解更多信息,请阅读我们的博客:HOW TO RESIZE IMAGES IN MAGENTO 2?

      【讨论】:

        猜你喜欢
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-19
        • 1970-01-01
        • 2015-10-10
        • 1970-01-01
        相关资源
        最近更新 更多