25

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?

1

7 Answers 7

51

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

10
  • 10
    FYI - this command takes absolutely forever to run on a store of any size. We saw upwards of 17hrs on a recent run. On other occasions, it needed to be run over a weekend. See: github.com/magento/magento2/issues/8145
    – Leland
    Dec 3, 2018 at 17:03
  • i had same issue i run this cmd images showing but after flush cache all images broken again and no images in cache folder
    – imtiazau
    Jul 24, 2019 at 9:36
  • 1
    If you use php bin/magento catalog:image:resize then it will take more than 1 days, and any other best method? Aug 14, 2019 at 9:41
  • @Alex Dinca could you please help me on this magento.stackexchange.com/questions/283277/… Aug 22, 2019 at 13:54
  • 2
    Use this command with the -a switch to run asynchronously, this speeds it up dramatically.
    – NetStorm
    May 26, 2022 at 13:52
14

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.

4
  • Any help magento.stackexchange.com/q/296715/57334 thanks @Bilal Usean
    – zus
    Nov 26, 2019 at 9:38
  • 4
    This answer helped me solve my issue. I was completely missing the .htaccess found in the pub/media folder. Once I added it back the images began showing up and being copied to the product/cache/XXXX folder. Thanks a million! Feb 20, 2020 at 19:30
  • 1
    most of the time it's permission issue. Thank you for the reminder :D
    – Dasun
    May 14, 2020 at 11:12
  • Based on this answer by @Bilal Usean, I noticed the file path I was calling included pub. In var/resource_config.json is states "media\/catalog\/product\/cache\/" as an allowed resource which should have matched the url but it didn't. I added a new resource to resource_config.json "pub\/media\/catalog\/product\/cache\/" cleared the caches and reloaded the page. Images appeared. Hope this helps. Jun 4, 2021 at 17:18
3

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());
  }
}
1

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.

0

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.

0

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!

0

In my case, i deleted some themes folder in the source code, you can see this error message if you run php bin/magento catalog:images:resize

Required parameter 'theme_dir' was not passed

i re-upload the theme and then re-run the command then everything go fine

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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