3

i have uploaded image in the admin to a product. Now I want the product image to be proper SEO name, basically like productname-sometags.png

How can I do this? I have 1000 products on my site, it is not possible to change names of all one by one.

Please help me, is there anyway by which it can be done programmatically.

1

Images:-

Uploading images is extremely straight forward. Browse to find the file you want then select upload. Once you have done this select which image you want as your Base Image, Small Image and Thumbnail. You can also select sort order if you have multiple images.

Image Name:- Search engines develop an understanding of how product images are related due to it's file name, alt tag and the context in which it is placed. It is good practice to name your file after the product, as this assists search engines understanding what is in the image.

Label:-

This is the only other thing you have to consider in Magento Images for SEO. This Label also applies as it’s Alt Tag. The importance of Alt Tags is reducing with time, but it is still best practice to name the Alt Tag (and therefore the label) after the product.

Here is a definition of Alt Tags “The alt attribute is used in HTML and XHTML documents to specify alternative text (alt text) that is to be rendered when the element to which it is applied cannot be rendered. It is also used by "screen reader" software so that a person who is listening to the content of a webpage (for instance, a person who is blind) can interact with this element.” enter image description here

  • this is good info, but the question was how to bulk change existing image labels for 1000 products quickly. – paj Jul 16 '15 at 9:18
  • ok frinds i try another one – Ashvin Monpara Jul 16 '15 at 9:19
  • hey i am asking about product name to be seo friendly not label – user1799722 Jul 17 '15 at 7:41
  • @AshvinMonpara : How can i bulk csv upload of Alt label – Gem Nov 28 '16 at 4:49
1

You can programmatically set the image alt label and title in

app/design/frontend/theme/default/template/catalog/product/view/media.phtml

For example if your product image labels are empty, and you want any empty image labels to have the product name you can do this

$_imageLabel=$this->escapeHtml($this->getImageLabel());
if (empty($_imageLabel)) { $_imageLabel=$this->escapeHtml($_product->getName()); }

And replace $this->escapeHtml($this->getImageLabel() with $_imageLabel

You would also need to do this for the gallery images.

$_imageLabel=$this->escapeHtml($_image->getLabel());
if (empty($_imageLabel)) { $_imageLabel=$this->escapeHtml($_product->getName()); }  
  • 1
    hey i am asking about product name to be seo friendly not label – user1799722 Jul 17 '15 at 7:41
  • you mean you want to rename all the filenames of the images you already have uploaded and assigned to products? – paj Jul 17 '15 at 15:26
0

Please make sure the following things before running the script

  1. This script location is at root-folder/script
  2. System -> Configuration -> Catalog -> Catalog -> Frontend -> Use Flat Catalog Product should be No. If it is Yes then set it No first then execute the script and again you can set it Yes
  3. After execute the script reindex at System -> Index Management -> Product Flat Data

.

  require_once('../app/Mage.php');

    Mage::app();

    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*'); // select all attributes
    $newPaths = array();
    $normalizeChars = array(
        'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
        'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
        'Ï'=>'I', 'Ñ'=>'N', 'Ń'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
        'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
        'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
        'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
        'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f',
        'ă'=>'a', 'î'=>'i', 'â'=>'a', 'ș'=>'s', 'ț'=>'t', 'Ă'=>'A', 'Î'=>'I', 'Â'=>'A', 'Ș'=>'S', 'Ț'=>'T',
        ' '=>'-'
    );

    foreach ($collection as $productCollection) {
        $product = mage::getModel('catalog/product')->load($productCollection->getId());
        $images = $product->getMediaGallery('images');
        foreach ($images as $image) {
            $name = trim(strtolower($product->getName()));
            $formattedName = preg_replace("/[^A-Za-z0-9\_\-\.]/", '', strtr($name, $normalizeChars));
            $newPaths[$product->getSku()]['old'] = $image['file'];

            $newPaths[$product->getSku()]['name'] = $name;
            $newPaths[$product->getSku()]['formated'] = $formattedName;
            $newPaths[$product->getSku()]['new'] = '/'.$formattedName[0].'/'.$formattedName[1].'/'.$newPaths[$product->getSku()]['formated'].'-'.$product->getSku().'.'.pathinfo($image['file'], PATHINFO_EXTENSION);

        }
    }

    try {
        $write = Mage::getSingleton('core/resource')->getConnection('core_write');

        foreach ($newPaths as $newPath) {
            copy('../media/catalog/product' . $newPath['old'], '../media/catalog/product' . $newPath['new']);

            $query = 'UPDATE catalog_product_entity_media_gallery SET value = "' . $newPath['new'] . '" WHERE value ="' . $newPath['old'].'"';
            $write->query($query);

            $query = 'UPDATE catalog_product_entity_varchar SET value = "' . $newPath['new'] . '" WHERE value ="' . $newPath['old'].'"';
            $write->query($query);
        }
    }catch (Exception $e){
        var_dump($e);
    }
    var_dump($newPaths);

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.