4

We've migrated over thousands of images from another, older eCommerce platform and are noticing that the products with .bmp product images are causing the following exception:

1 exception(s):
Exception #0 (Exception): Unsupported image format.

Exception #0 (Exception): Unsupported image format.
#0 /srv/public_html/vendor/magento/framework/Image/Adapter/Gd2.php(65): Magento\Framework\Image\Adapter\Gd2->_getCallback('create')
#1 /srv/public_html/vendor/magento/framework/Image.php(55): Magento\Framework\Image\Adapter\Gd2->open('/srv/public_htm...')
#2 /srv/public_html/vendor/magento/framework/Image.php(36): Magento\Framework\Image->open()
#3 /srv/public_html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(111): Magento\Framework\Image->__construct(Object(Magento\Framework\Image\Adapter\Gd2), '/srv/public_htm...')
#4 /srv/public_html/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(66): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Magento\\Framewo...', Array)
#5 /srv/public_html/vendor/magento/framework/ObjectManager/ObjectManager.php(56): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Framewo...', Array)
#6 /srv/public_html/vendor/magento/framework/Image/Factory.php(47): Magento\Framework\ObjectManager\ObjectManager->create('Magento\\Framewo...', Array)
#7 /srv/public_html/vendor/magento/module-catalog/Model/Product/Image.php(534): Magento\Framework\Image\Factory->create('/srv/public_htm...')
#8 /srv/public_html/vendor/magento/module-catalog/Model/Product/Image.php(554): Magento\Catalog\Model\Product\Image->getImageProcessor()
#9 /srv/public_html/vendor/magento/module-catalog/Helper/Image.php(477): Magento\Catalog\Model\Product\Image->resize()
#10 /srv/public_html/vendor/magento/module-catalog/Helper/Image.php(551): Magento\Catalog\Helper\Image->applyScheduledActions()
#11 /srv/public_html/vendor/magento/module-catalog/Block/Product/ImageBuilder.php(132): Magento\Catalog\Helper\Image->getResizedImageInfo()
#12 /srv/public_html/vendor/magento/module-catalog/Block/Product/AbstractProduct.php(516): Magento\Catalog\Block\Product\ImageBuilder->create()
#13 /srv/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Catalog\Block\Product\AbstractProduct->getImage(Object(Magento\Catalog\Model\Product\Interceptor), 'category_page_g...', Array)
#14 /srv/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Catalog\Block\Product\ListProduct\Interceptor->___callParent('getImage', Array)
#15 /srv/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Catalog\Block\Product\ListProduct\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Catalog\Model\Product\Interceptor), 'category_page_g...')
#16 /srv/public_html/generated/code/Magento/Catalog/Block/Product/ListProduct/Interceptor.php(468): Magento\Catalog\Block\Product\ListProduct\Interceptor->___callPlugins('getImage', Array, Array)
#17 /srv/public_html/vendor/magento/module-catalog/view/frontend/templates/product/list.phtml(52): Magento\Catalog\Block\Product\ListProduct\Interceptor->getImage(Object(Magento\Catalog\Model\Product\Interceptor), 'category_page_g...')
#18 /srv/public_html/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/srv/public_htm...')
#19 /srv/public_html/vendor/magento/framework/View/Element/Template.php(270): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catalog\Block\Product\ListProduct\Interceptor), '/srv/public_htm...', Array)
#20 /srv/public_html/generated/code/Magento/Catalog/Block/Product/ListProduct/Interceptor.php(557): Magento\Framework\View\Element\Template->fetchView('/srv/public_htm...')
#21 /srv/public_html/vendor/magento/framework/View/Element/Template.php(300): Magento\Catalog\Block\Product\ListProduct\Interceptor->fetchView('/srv/public_htm...')
#22 /srv/public_html/vendor/magento/framework/View/Element/AbstractBlock.php(667): Magento\Framework\View\Element\Template->_toHtml()
#23 /srv/public_html/generated/code/Magento/Catalog/Block/Product/ListProduct/Interceptor.php(882): Magento\Framework\View\Element\AbstractBlock->toHtml()
#24 /srv/public_html/vendor/magento/framework/View/Layout.php(558): Magento\Catalog\Block\Product\ListProduct\Interceptor->toHtml()
#25 /srv/public_html/vendor/magento/framework/View/Layout.php(534): Magento\Framework\View\Layout->_renderBlock('category.produc...')
#26 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('category.produc...')
#27 /srv/public_html/vendor/magento/framework/View/Layout.php(489): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('category.produc...')
#28 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('category.produc...', true)
#29 /srv/public_html/vendor/magento/framework/View/Element/AbstractBlock.php(507): Magento\Framework\View\Layout\Interceptor->renderElement('category.produc...', true)
#30 /srv/public_html/vendor/magento/module-catalog/Block/Category/View.php(100): Magento\Framework\View\Element\AbstractBlock->getChildHtml('product_list')
#31 /srv/public_html/vendor/magento/module-catalog/view/frontend/templates/category/products.phtml(18): Magento\Catalog\Block\Category\View->getProductListHtml()
#32 /srv/public_html/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/srv/public_htm...')
#33 /srv/public_html/vendor/magento/framework/View/Element/Template.php(270): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catalog\Block\Category\View), '/srv/public_htm...', Array)
#34 /srv/public_html/vendor/magento/framework/View/Element/Template.php(300): Magento\Framework\View\Element\Template->fetchView('/srv/public_htm...')
#35 /srv/public_html/vendor/magento/framework/View/Element/AbstractBlock.php(667): Magento\Framework\View\Element\Template->_toHtml()
#36 /srv/public_html/vendor/magento/framework/View/Layout.php(558): Magento\Framework\View\Element\AbstractBlock->toHtml()
#37 /srv/public_html/vendor/magento/framework/View/Layout.php(534): Magento\Framework\View\Layout->_renderBlock('category.produc...')
#38 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('category.produc...')
#39 /srv/public_html/vendor/magento/framework/View/Layout.php(489): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('category.produc...')
#40 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('category.produc...', true)
#41 /srv/public_html/vendor/magento/framework/View/Layout.php(585): Magento\Framework\View\Layout\Interceptor->renderElement('category.produc...')
#42 /srv/public_html/vendor/magento/framework/View/Layout.php(536): Magento\Framework\View\Layout->_renderContainer('content')
#43 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('content')
#44 /srv/public_html/vendor/magento/framework/View/Layout.php(489): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('content')
#45 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('content', true)
#46 /srv/public_html/vendor/magento/framework/View/Layout.php(585): Magento\Framework\View\Layout\Interceptor->renderElement('content')
#47 /srv/public_html/vendor/magento/framework/View/Layout.php(536): Magento\Framework\View\Layout->_renderContainer('main')
#48 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('main')
#49 /srv/public_html/vendor/magento/framework/View/Layout.php(489): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('main')
#50 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('main', true)
#51 /srv/public_html/vendor/magento/framework/View/Layout.php(585): Magento\Framework\View\Layout\Interceptor->renderElement('main')
#52 /srv/public_html/vendor/magento/framework/View/Layout.php(536): Magento\Framework\View\Layout->_renderContainer('columns')
#53 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('columns')
#54 /srv/public_html/vendor/magento/framework/View/Layout.php(489): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('columns')
#55 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('columns', true)
#56 /srv/public_html/vendor/magento/framework/View/Layout.php(585): Magento\Framework\View\Layout\Interceptor->renderElement('columns')
#57 /srv/public_html/vendor/magento/framework/View/Layout.php(536): Magento\Framework\View\Layout->_renderContainer('main.content')
#58 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('main.content')
#59 /srv/public_html/vendor/magento/framework/View/Layout.php(489): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('main.content')
#60 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('main.content', true)
#61 /srv/public_html/vendor/magento/framework/View/Layout.php(585): Magento\Framework\View\Layout\Interceptor->renderElement('main.content')
#62 /srv/public_html/vendor/magento/framework/View/Layout.php(536): Magento\Framework\View\Layout->_renderContainer('page.wrapper')
#63 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('page.wrapper')
#64 /srv/public_html/vendor/magento/framework/View/Layout.php(489): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('page.wrapper')
#65 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('page.wrapper', true)
#66 /srv/public_html/vendor/magento/framework/View/Layout.php(585): Magento\Framework\View\Layout\Interceptor->renderElement('page.wrapper')
#67 /srv/public_html/vendor/magento/framework/View/Layout.php(536): Magento\Framework\View\Layout->_renderContainer('root')
#68 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('root')
#69 /srv/public_html/vendor/magento/framework/View/Layout.php(489): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('root')
#70 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('root', true)
#71 /srv/public_html/vendor/magento/framework/View/Layout.php(954): Magento\Framework\View\Layout\Interceptor->renderElement('root')
#72 /srv/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\View\Layout->getOutput()
#73 /srv/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\View\Layout\Interceptor->___callParent('getOutput', Array)
#74 /srv/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\View\Layout\Interceptor->Magento\Framework\Interception\{closure}()
#75 /srv/public_html/generated/code/Magento/Framework/View/Layout/Interceptor.php(494): Magento\Framework\View\Layout\Interceptor->___callPlugins('getOutput', Array, Array)
#76 /srv/public_html/vendor/magento/framework/View/Result/Page.php(257): Magento\Framework\View\Layout\Interceptor->getOutput()
#77 /srv/public_html/vendor/magento/framework/View/Result/Layout.php(170): Magento\Framework\View\Result\Page->render(Object(Magento\Framework\App\Response\Http\Interceptor))
#78 /srv/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\View\Result\Layout->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
#79 /srv/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\View\Result\Page\Interceptor->___callParent('renderResult', Array)
#80 /srv/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\View\Result\Page\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Response\Http\Interceptor))
#81 /srv/public_html/generated/code/Magento/Framework/View/Result/Page/Interceptor.php(130): Magento\Framework\View\Result\Page\Interceptor->___callPlugins('renderResult', Array, Array)
#82 /srv/public_html/vendor/magento/framework/App/Http.php(139): Magento\Framework\View\Result\Page\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
#83 /srv/public_html/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch()
#84 /srv/public_html/vendor/magento/framework/App/Bootstrap.php(256): Magento\Framework\App\Http\Interceptor->launch()
#85 /srv/public_html/pub/index.php(40): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#86 {main}

Edit: Originally I had asked about bitmap support, and can see now that it isn't possible.

But when we go to replace the problematic product images with jpg's, we run into a "Unsupported image format" error. So I can't even delete the product image and then save without this error firing. Any tips on how to achieve this?

Would I need to delete the image from within the database itself? If so, is there any other data or any references I need to keep in mind?

  • 1
    I've updated my answer. Hope this helps a bit. I guess you find other questions how to change DB entries, if not i can add this later. – sv3n Oct 3 '18 at 13:59
  • 1
    Ladies and gentlemen, introducing your bounty recipient... sv3n! Thanks so much! – brackfost Oct 3 '18 at 14:05
  • FYI image conversion works, but images are bit brighter after. And its slow - so you maybe have to split runs if you have thousands of BMPs. – sv3n Oct 3 '18 at 14:09
2
+50

I guess this is not really related to Magento. As far as i know GD librabray does not support image creation from bitmaps.

Edit: this was for M1, but it should not be much different in M2 (?)

Varien_Image_Adapter_Gd2

private static $_callbacks = array(
    IMAGETYPE_GIF  => array('output' => 'imagegif',  'create' => 'imagecreatefromgif'),
    IMAGETYPE_JPEG => array('output' => 'imagejpeg', 'create' => 'imagecreatefromjpeg'),
    IMAGETYPE_PNG  => array('output' => 'imagepng',  'create' => 'imagecreatefrompng'),
    IMAGETYPE_XBM  => array('output' => 'imagexbm',  'create' => 'imagecreatefromxbm'),
    IMAGETYPE_WBMP => array('output' => 'imagewbmp', 'create' => 'imagecreatefromwbmp'),
);

Update:

I found a script that converts BMPs to JPGs ...

<?php

function ConvertBMP2GD($src, $dest = false)
{
    if (!($src_f = fopen($src, "rb"))) {
        return false;
    }
    if (!($dest_f = fopen($dest, "wb"))) {
        return false;
    }
    $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,14));
    $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant",fread($src_f, 40));

    extract($info);
    extract($header);

    if ($type != 0x4D42) { // signature "BM"
        return false;
    }

    $palette_size = $offset - 54;
    $ncolor = $palette_size / 4;
    $gd_header = "";
// true-color vs. palette
    $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
    $gd_header .= pack("n2", $width, $height);
    $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
    if ($palette_size) {
        $gd_header .= pack("n", $ncolor);
    }
// no transparency
    $gd_header .= "\xFF\xFF\xFF\xFF";

    fwrite($dest_f, $gd_header);

    if ($palette_size) {
        $palette = fread($src_f, $palette_size);
        $gd_palette = "";
        $j = 0;
        while ($j < $palette_size) {
            $b = $palette{$j++};
            $g = $palette{$j++};
            $r = $palette{$j++};
            $a = $palette{$j++};
            $gd_palette .= "$r$g$b$a";
        }
        $gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
        fwrite($dest_f, $gd_palette);
    }

    $scan_line_size = (($bits * $width) + 7) >> 3;
    $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size &
            0x03) : 0;

    for ($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
// BMP stores scan lines starting from bottom
        fseek($src_f, $offset + (($scan_line_size + $scan_line_align) *
                $l));
        $scan_line = fread($src_f, $scan_line_size);
        if ($bits == 24) {
            $gd_scan_line = "";
            $j = 0;
            while ($j < $scan_line_size) {
                $b = $scan_line{$j++};
                $g = $scan_line{$j++};
                $r = $scan_line{$j++};
                $gd_scan_line .= "\x00$r$g$b";
            }
        } elseif ($bits == 8) {
            $gd_scan_line = $scan_line;
        } elseif ($bits == 4) {
            $gd_scan_line = "";
            $j = 0;
            while ($j < $scan_line_size) {
                $byte = ord($scan_line{$j++});
                $p1 = chr($byte >> 4);
                $p2 = chr($byte & 0x0F);
                $gd_scan_line .= "$p1$p2";
            } $gd_scan_line = substr($gd_scan_line, 0, $width);
        } elseif ($bits == 1) {
            $gd_scan_line = "";
            $j = 0;
            while ($j < $scan_line_size) {
                $byte = ord($scan_line{$j++});
                $p1 = chr((int) (($byte & 0x80) != 0));
                $p2 = chr((int) (($byte & 0x40) != 0));
                $p3 = chr((int) (($byte & 0x20) != 0));
                $p4 = chr((int) (($byte & 0x10) != 0));
                $p5 = chr((int) (($byte & 0x08) != 0));
                $p6 = chr((int) (($byte & 0x04) != 0));
                $p7 = chr((int) (($byte & 0x02) != 0));
                $p8 = chr((int) (($byte & 0x01) != 0));
                $gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
            } $gd_scan_line = substr($gd_scan_line, 0, $width);
        }

        fwrite($dest_f, $gd_scan_line);
    }
    fclose($src_f);
    fclose($dest_f);
    return true;
}

function imagecreatefrombmp($filename)
{
    $tmp_name = tempnam("/tmp", "GD");
    if (ConvertBMP2GD($filename, $tmp_name)) {
        $img = imagecreatefromgd($tmp_name);
        unlink($tmp_name);
        return $img;
    } return false;
}

Source: https://forums.codewalkers.com/php-coding-7/how-to-convert-bmp-to-jpg-879135.html

You can try this:

  1. run this script over your image directory to convert images

    $root = './path/to/your/images';
    $iterator  = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
        RecursiveIteratorIterator::SELF_FIRST,
        RecursiveIteratorIterator::CATCH_GET_CHILD
    );
    
    foreach ($iterator as $path => $item) {
        if ($item->isFile() && strtolower($item->getExtension()) === 'bmp') {
            $img = imagecreatefrombmp($item->getPathname());
            imagejpeg($img, str_replace(
                '.' . $item->getExtension(),
                '.jpg',
                $item->getPathname()
            ));
        }
    }
    
  2. update your database entries and replace file extensions

    #later, if required...
    
1

You should use JPG,PNG,GIF instead of BMP for images. BMP files are usually very large, which cause speed, performance issue and also can cause space issue. (disk space will be used up very quickly and page load time will be slow as it downloads those images.)

So i would suggest convert all these images in one of above formates and import separately.

  • Well sure, I get that. This doesn't actually address my question, though. Also, 99% of the product images are JPG's, but there are a few random (and small) BMP's here and there. – brackfost Sep 18 '18 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.