Question

I'm doing the migration process from Magento 1.9.2.4 to Magento 2.1.6, after migration completed, I move the media folder of M1 to pub/media M2.

Now the problem is some of the images don't generate in the catalog/Cache folder

For example, the below images are going to 404 not found

pub/media/catalog/product/cache/f9c7fbe9b524c081a3ccf800cbd963eb/m/s/msj006c-red_2.jpg
pub/media/catalog/product/cache/75eed2686e01eb22cb4050b2f40ddf97/m/s/msj006c-red_2.jpg
pub/media/catalog/product/cache/f9c7fbe9b524c081a3ccf800cbd963eb/m/s/msj006c-red_2.jpg

I did like simply delete the catalog cache folder and load the page again but still, it goes to a broken image.

My page has 50% of broken images

enter image description here

can share the workaround to fix this issue?

Was it helpful?

Solution

You should try using the image resize command to pre-generate all necessary resizes.

php bin/magento catalog:image:resize

This command gets all the images sizes that have been defined in the theme XML and pregenerates the images in their correct folders.

You can also check the command documentation for more information http://devdocs.magento.com/guides/v2.1/frontend-dev-guide/themes/theme-images.html

OTHER TIPS

Answer on November 20, 2019:

Regenerate image cache by command is not a feasible solution for all because it will take a lot of time for some website which has a lot of products. Also, I faced some issues like If we generate a cache image from CLI, it will work. When we flushed images from admin or delete the cached image manually at that time it won't generate a cache image again on page load so I need to run the regenerate command again and again. As per my point of view, the best solution is to generate image cache on page load.

Default Flow

Default Magento flow is whenever it load image(media), it will always pass through the request to pub/get.php and check whether the image exists or not. If it not exist, it will generate a new cached image. If it exists, it will return that path. So by default image should generate on page load.

We can check this pass-through logic in the below files

pub/media/.htaccess for apache server

RewriteRule .* ../get.php [L]
.............................
.............................

nginx.conf.sample for nginx server

location /media/ {
    try_files $uri $uri/ /get.php$is_args$args;
    .......................................
    .......................................

How to check this logic is working or not?

Put echo "test";exit; in the starting of pub/get.php and load any cached media URL, it should print test. Otherwise something wrong in your server configuration.

For me, whenever I deleted the catalog cache directory (rm -rf pub/media/catalog/product/cache/*) after that when we load the page it will not generate a new cached image and it goes to 404 pages not found and also it never reach get.php. I then noticed that many of the folders were having incorrect permissions different from 755 for folders and 644 for files. After I set the right permission, it works fine.

I hope it gives some idea.

I had this exact same problem but with Magento 2.3.2

For me it was product thumbnail images that had the wrong cache hash path. Product and category images were correct, but thumbs URL was incorrect and showing the std Magento image placeholder.

I was using a custom theme.

When using SSH "php bin/magento catalog:images:resize" - what was happening? The images were being generated using the Luma theme etc/view.xml instead of the custom theme etc/view.xml file.

The problem: When viewing my custom theme in the browser which uses different sized images to Luma theme, Magento could not find the images and shows 404 error.

The fix.

Replace Luma themes etc/view.xml with my custom theme etc/view.xml
Using SSH run "php bin/magento catalog:images:resize

It took me a week to find out how to fix this, but it all works fine now.

I also had this issue and even command line images generation mentioned above did not work. It seems Magento is caching the information that thumbnail is created and even standard Magento cache cleaning (both command line or admin panel) doesn't remove this information from cache.

I removed all cache directories contents manually and it helped:

rm -Rf var/cache/*
rm -Rf var/page_cache/*

.. and so on. Then images thumbnails should generate properly "on demand" while browsing the site.

Please see your config in the theme you are using and make sure that config in your source store is the same with that in your target. You can refer to this:

https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/themes/theme-images.html

Then run:

php bin/magento catalog:images:resize

Let me know if this helps!

if you don't want to run commands than you can also regenerate images programatically

just add below code in top of the file

vendor\magento\module-catalog\view\frontend\templates\product\view\gallary.phtml

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resizeImage = $objectManager->get('\Magento\MediaStorage\Service\ImageResize');
$_product = $block->getProduct();
$galleryImages = $_product->getMediaGalleryImages();
if ($galleryImages) 
{
  foreach ($galleryImages as $image) 
  {
     $resizeImage->resizeFromImageName($image->getFile());
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top