Missing files in /media/ - help on how to write a script that will regenerate based on .htaccess call?

magento.stackexchange https://magento.stackexchange.com/questions/3012

Pergunta

I performed a complete cache clean and discovered that this obliterates everything under /media/ (as noted by me below and later correctly observed by an answer, it is indeed just the cache that is being wiped under the /media/ folder). Unfortunately there are some email distributions from the past that rely on these images and I am curious as to whether there is a way to regenerate all of the thumbnails / cache entries under the /media/ folder?

So, for example, in this situation I am seeing missing files such as:

[Mon Apr 15 11:46:01 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/T/E/TE0213.jpg
[Mon Apr 15 11:46:01 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/F/X/FX0413_1.jpg
[Mon Apr 15 11:46:01 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/F/B/FB0413.jpg
[Mon Apr 15 11:46:01 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/F/K/FK0413.jpg
[Mon Apr 15 11:46:02 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/E/V/EV0413.jpg
[Mon Apr 15 11:46:02 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/F/P/FP0413.jpg
[Mon Apr 15 11:46:02 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/F/E/FEA0413.jpg
[Mon Apr 15 11:46:03 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/F/Z/FZ0413.jpg
[Mon Apr 15 11:46:03 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/G/B/GB0413.jpg
[Mon Apr 15 11:46:03 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/F/S/FS0413.jpg
[Mon Apr 15 11:46:04 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/0/image/265x/4df79eab33525d08d6e5fb8d27136e95/F/U/FU0413.jpg

When I do look for the images, for example, I do see the originals present in: media/catalog/product/F/U/

The goal, therefore, is to regenerate all of these appropriately.

Moving further along in this, I can see that I need to be able to execute a Magento script upon failure of finding an image (particularly since many of these images are not being found due to being called by external references such as emails or other sites). I expect to use something like this from Apache to call this script that I'm hoping to get help on here:

# Inside of .htaccess:
######################
# if the file exists, just send it
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^cache/.*$ - [L]

# if it doesn't exists, rewrite to PHP script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cache/.*$ /path/to/script.php?url=$0 [L]
Foi útil?

Solução

File does not exist: /home/username/public_html/domain.tld/media/catalog/product/cache/

It cleared the image cache.

media/catalog/product/F/U/

Your original images are still in place, so the clear image cache function obviously did not do as you first said, obliterates everything under /media/

Your major problem then is that your templates are not doing proper media helper calls to regenerate the image. Where exactly are you having your missing image problem?

A typical helper call asks for the url, if no URL exists, the image is added to the cache on the fly with the size chosen and then the URL is returned to the html block.

$this->helper('catalog/image')->init($_product, 'small_image')->resize(135)

Product View Page image

<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />

Product Listing

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />

Note that the init function for the helper takes the following inputs:

public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)

and processes $imageFile with the following:

if ($imageFile) {
    $this->setImageFile($imageFile);
} else {
    // add for work original size
$this->_getModel()->setBaseFile($this->getProduct()->getData($this->_getModel()->getDestinationSubdir()));
}

POC code that feeds out resized image url when queried with image string. For Store 1 in this sample, yours according to the given URLs is Store 0.

<?php

/*
 * imgurl.php allows you to return image url for imgquery.
 * http://www.example.com/imgurl.php?imgurl=/l/p/lp_5009.jpg
 */

ini_set('memory_limit','256M');

require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore('1');
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);


$product  = "0";
$imgurl   = "";

if (isset($_GET["imgquery"])) {  

    $string = filter_input(INPUT_GET, 'imgquery', FILTER_SANITIZE_URL);

    $imgquery = trim($string);

    $imgurl = Mage::helper('catalog/image')->init($product, 'image', $imgquery)->resize(250, 250);

}

if ($imgurl) {

    echo '<img src="' . $imgurl . '"/>';
}

?>

Feed it a get query:

http://www.example.com/imgurl.php?imgquery=/l/p/lp_5009.jpg

Which kicks out:

<img src="http://www.example.com/media/catalog/product/cache/1/image/250x250/9df78eab33525d08d6e5fb8d27136e95/l/p/lp_5009.jpg"/>

Outras dicas

This error in my case with magento 1.7.0.2 is a bad code of zoom image module.

Disable it to get the work.

The files are generated, when needed. You need a call to Mage::helper('catalog/image)->... to create these images. You can do it either manually or you might write a script to redirect all requests to /media/ to this script do generate the images just in time

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top