6
votes

I am getting stuck with images not displaying with their products. When I create a product and insert an image with that product manually through (manage products) the image shows which is great. However we have over 1000 product images which need to be uploaded, (all the product information is already up I just need to add the images) I have exported the products CSV file and the working image address is /n/g/image1.jpg I have then copied that but change the image to image2.jpg, image3.jpg etc. I then uploaded the new images into that folder directory on our server thinking that's enough. I then upload the CSV file blow the cache and reindex the data however no new images show and worse the working image doesn't display. I have filled in image, small_image and thumbnail details in the CSV. All images are correctly sized etc.

Further more can allow tell me where I can just have 1 image folder in the directory where all my images are stored? Thanks

I'm using Magento version 1.7.0.2

4
Addiction details I don't have media/import which I can see many people do have in their directory.user1035650

4 Answers

14
votes

First of all your image should store in media>import directory

then in your csv file just write in image column /imagename.jpg

it will find same imagename in import directory, if image exist then it will upload the image

EDIT

if you don't have import directory then simply create it

3
votes

I'm sorry to say, but there is more going on behind the scenes with magento images that it simply putting a filename in the database and linking to your image. The cache generation alone is pretty complex. I believe you are going to have a hard time doing it the way you are attempting.

That being said, I do have a suggestion. Since your images are already on the server, I suggest you write a simple php script to tell magento to attach them to the product image. This could be automated, but i'll give you a small example below...

To attach an image, you would simply navigate to the url like this http://yoursite.com/imageattacher.php?sku=YOURSKU&image=yourimagefilename.jpg

The script would be like this... create a file in your magento ROOT and call it imageattacher.php. Upload your images to the magento media import directory. I have not tested this but it should work.

<?php
    // Initialize magento for use outside of core
    umask(0);
    require_once 'app/Mage.php';
    Mage::app('admin');

    // Get the variables from the URL
    $sku = $_GET["sku"];
    $imageName = $_GET["image"];

    // get the image from the import dir
    $import = Mage::getBaseDir('media') . DS . 'import/' . $imageName;

    // Load the product by sku
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);

    // if the product exists, attempt to add the image to it for all three items
    if ($product->getId() > 0)
    {
        // Add the images and set them for their respective usage (the radio button in admin)
        $product->addImageToMediaGallery($import,array('image', 'small_image', 'thumbnail'),false,false);

        // Make the changes stick
        $product->save();
    }

?>

See this http://www.danneh.org/2012/05/creating-products-programmatically-magento/

0
votes

The issue in the end was that there wasn't an import folder under the media directory. The images were uploaded to this folder. The image column in the CSV was changed to /image1.jpg which allowed them to show on the website.

0
votes

I had to import a bunch of Magento product images recently which were all named by SKU (eg 123456.jpg). This is the script I used to import them, parts of which are based on CarComp's answer. It will only work for numeric SKUs (eg 123456) but could be fairly easily modified to cater for alphanumeric ones.

<?php
require_once(__DIR__ . "/app/Mage.php");
Mage::app('admin');

class Sku_ImageLoader {
    private $_uploadDir;
    private $_imagePaths;

    public function setUploadDirectory($dir = null) {
        if (empty($dir)) {
            if (isset($this->_uploadDir)) return $this;
            $dir = 'upload';            
            }
        $this->_uploadDir = Mage::getBaseDir('media') . DS . $dir; 
        // mkdir($this->_uploadDir . DS . "/loaded", 0770);
        return $this;
    }

    public function load() {
        $this->setUploadDirectory();

        // Match product images like 123456.jpg
        $pattern = '[0-9][0-9][0-9][0-9][0-9][0-9].{jpg,gif,png}';

        chdir($this->_uploadDir);
        $this->_imagePaths = glob($pattern, GLOB_BRACE);
        return $this;
    }

    public function showFiles() {
        $this->load();
        echo "\r\n\r\nSorry, I wasn't able to upload the following image files in " . $this->_uploadDir . "\r\n\r\n<pre>\r\n";
        print_r($this->_imagePaths);
        return $this;
    }

    private function _addImage($path) {
        $sku = (string)intval($path);
        try {
           echo "Loading SKU: {$sku} ... ";  
           $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
           // if the product exists, attempt to add the image to it for all three items
           if (strpos(get_class($product), 'Catalog_Model_Product') !== false && $product->getId() > 0) {
               $product->addImageToMediaGallery($path,array('image', 'small_image', 'thumbnail'),false,false);
               $product->save();
               echo " ok \r\n";
               return true;
           }
        } catch (Exception $e) {
            echo "Exception thrown for {$sku}: " . $e->getMessage() . "\r\n";
        }
        echo " (FAILED) \r\n";
       return false;
    }

    private function _moveImage($path) {
        // rename($path, 'loaded' . DS . $path);
        unlink($path);
    }

    public function import() {
        echo "<pre>\r\n";
        if (!isset($this->_imagePaths)) $this->load();
        foreach ($this->_imagePaths as $path) {
            if ($this->_addImage($path)) {
                $this->_moveImage($path);
            }
        }
        return $this;
    }

}

$u = new Sku_ImageLoader();

$u->import()->showFiles();
?>