Magento Forum

Page 1 of 2
Separating image upload from Magento functionality
 
kullysupply
Member
 
Avatar
Total Posts:  48
Joined:  2008-08-05
 

I made a little change to my Magento installation which is going to make my life much easier. Basically I separated image uploading from the Magento functionality. Now I can upload a bunch of images via ftp to a directory under media/catalog/product and then just enter the correct image text for each product. This works on the individual product pages (see image) and during a batch product import.

The main reason I wanted to do this is because we have a lot of products that share one image, and this is not allowed through Magento’s core functionality. Each duplicate image is renamed, and I could foresee big issues in the future if we ever had to change any of those images.

As you can see in the attachment, I added the image 151118.jpg to a directory I created called images, meaning the whole path is media/catalog/product/images/151118.jpg.

Image Attachments
magento_images.jpg
 
Magento Community Magento Community
Magento Community
Magento Community
 
kullysupply
Member
 
Avatar
Total Posts:  48
Joined:  2008-08-05
 

How I did it

1) Database table eav_attributes
Search for the rows with attribute_code ‘image’, ‘small_image’ and ‘thumbnail’. Change the frontend_input field from ‘media_image’ to ‘text’.

2) app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php
Remove or comment out the following code (around line 570) which will allow image values to be imported:

if (in_array($field$this->_imageFields)) {
                
continue;
            
}

Remove or comment out the following code (around line 640) which will prevent image importing:

$imageData = array();
        foreach (
$this->_imageFields as $field{
            
if (!empty($importData[$field]) && $importData[$field] != 'no_selection'{
                
if (!isset($imageData[$importData[$field]])) {
                    $imageData[$importData[$field]] 
= array();
                
}
                $imageData[$importData[$field]][] 
$field;
            
}
        }

        
foreach ($imageData as $file => $fields{
            
try {
                $product
->addImageToMediaGallery(Mage::getBaseDir('media') . DS 'import' $file$fields);
            
}
            
catch (Exception $e{}
        }

Finally, add this code above $product->setData($field, $setValue); (around line 620) which will set the image values to “no_selection” if empty:

if (in_array($field$this->_imageFields) && empty($setValue)) {
                $setValue 
"no_selection";
            
}

Now, in my csv import file under ‘image’,’small_image’ and ‘thumbnail’, I can just add ‘images/151118.jpg’ and that value will be added to the corresponding product as seen in the attachment above.

3) lib/Varien/File/Uploader.php
The following may not be necessary since I now can manually enter image names, but I disabled the file renaming and path dispersion functionality anyway.

disable file renaming:

static public function getNewFileName($destFile)
    
{
        $fileInfo 
pathinfo($destFile);
        
        
/* Return filename before it's renamed */
        
return $fileInfo['basename'];
        
        if( 
file_exists($destFile) ) {
            $index 
1;
            
$baseName $fileInfo['filename''.' $fileInfo['extension'];
            while( 
file_exists($fileInfo['dirname'DIRECTORY_SEPARATOR $baseName) ) {
                $baseName 
$fileInfo['filename']'_' $index '.' $fileInfo['extension'];
                
$index ++;
            
}
            $destFileName 
$baseName;
        
else {
            
return $fileInfo['basename'];
        
}

        
return $destFileName;
    
}

disable path dispersion:

static public function getDispretionPath($fileName)
    
{
        $char 
0;
        
$dispretionPath '';
        
        
/* Return empty path */
        
return $dispretionPath;
        
        while( (
$char 2) && ($char strlen($fileName)) ) {
            
if (empty($dispretionPath)) {
                $dispretionPath 
DIRECTORY_SEPARATOR.('.' == $fileName[$char] '_' $fileName[$char]);
            
}
            
else {
                $dispretionPath 
self::_addDirSeparator($dispretionPath) . ('.' == $fileName[$char] '_' $fileName[$char]);
            
}
            $char 
++;
        
}
        
return $dispretionPath;
    
}

Even though I had to edit some database fields directly and edit some core code, the changes are pretty simple. Like I said before, separating image uploading from product uploading and not having to deal with image renaming is going to make my life much easier.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Josh Mormann
Jr. Member
 
Total Posts:  13
Joined:  2008-09-24
 

GREAT STUFF!

This is exactly what I was looking for a while ago. Thanks for posting this.

However, now Category images don’t work… any suggestions?

 
Magento Community Magento Community
Magento Community
Magento Community
 
kullysupply
Member
 
Avatar
Total Posts:  48
Joined:  2008-08-05
 

I’m glad to hear this helps you out!

I just tried uploading a category image and it worked fine. I’m using version 1.1.6.

So you browse for your image, select it then save the category and nothing happens?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Josh Mormann
Jr. Member
 
Total Posts:  13
Joined:  2008-09-24
 

Morning kullysupply,

Thanks for looking into it for me. Somehow the edits made as per your directions got replaced by a “Modern Theme” reinstall… I’m pretty embarrassed.

red face

 
Magento Community Magento Community
Magento Community
Magento Community
 
kullysupply
Member
 
Avatar
Total Posts:  48
Joined:  2008-08-05
 

So the category images work for you now?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Josh Mormann
Jr. Member
 
Total Posts:  13
Joined:  2008-09-24
 

Well, I’m assuming they would. I just started from scratch again, and now I can’t seem to get the changes to take at all. Only the changes to the DB are taking.

Am I supposed to be flushing a cache of some kind to make these changes?

I think this might be why it took me so long to find out why the changes broke yesterday. I must have had a cashed version of your changes still in play, and so I couldn’t tell why some of the items weren’t working.

Thanks for your help in this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Josh Mormann
Jr. Member
 
Total Posts:  13
Joined:  2008-09-24
 

Hi Again,

I think I may have edited the wrong image attribute causing the category image to be affected by the changes, because now, after running a cron.php with an added line of code to flush the cache, I was able to get it all working again, and as advertised!

You’re awesome.

Thanks again kullysupply.

- Josh

 
Magento Community Magento Community
Magento Community
Magento Community
 
joyously
Guru
 
Total Posts:  447
Joined:  2008-08-21
 

This sounds a lot like I expected it to work when I first found Magento.

Can you still add more than one image to a product?

And what does it do with the caching of the images that way? (it saves the different sizes of generated thumbnails in the cache)

One more: Does this fix the problem of duplicating the image when it’s imported more than once?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Josh Mormann
Jr. Member
 
Total Posts:  13
Joined:  2008-09-24
 
joyously - 23 October 2008 09:27 AM

This sounds a lot like I expected it to work when I first found Magento.

Can you still add more than one image to a product?

And what does it do with the caching of the images that way? (it saves the different sizes of generated thumbnails in the cache)

One more: Does this fix the problem of duplicating the image when it’s imported more than once?

More than one photo, I’m not sure. Actually the way this works now, I’m pretty sure the answer is no, but as for duplicating images via import, you no longer import through the Admin page, you upload them via FTP to one folder, and call the image locations manually, (see original attachment).

This mod works great for me. It will at least carry me through to a release of Magento that has features that will take better care of multiple image uploads, and the ability to manage them in a spreadsheet. Why Magento was designed to take images only via catalog management, and why it puts images in alphabetical order two directories deep is beyond me, but they must have had their reasons. I just can’t seem to figure it out what they are confused

 
Magento Community Magento Community
Magento Community
Magento Community
 
joyously
Guru
 
Total Posts:  447
Joined:  2008-08-21
 

In case you don’t want to change code in /lib files in order to disable the file renaming and dispersion, you can comment it out in app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Attribute/Backend/Image.php around line 16 and 17
and in app/code/core/Mage/Adminhtml/controllers/Catalog/Product/GalleryController.php around line 22 and 23. 
Make the change in a local version of course…

//           $uploader->setAllowRenameFiles(true);
 //           $uploader->setFilesDispersion(true);
 
Magento Community Magento Community
Magento Community
Magento Community
 
kullysupply
Member
 
Avatar
Total Posts:  48
Joined:  2008-08-05
 

Yes, you can do that as well, although I searched through all the source code within app/code/core/Mage folder and found four instances of the code $uploader->setAllowRenameFiles(true);

In my case, since I don’t want any renaming to occur, whether through the import process or otherwise, it seems to make more sense to disable renaming from within the method itself rather than altering all four method calls.

 
Magento Community Magento Community
Magento Community
Magento Community
 
kullysupply
Member
 
Avatar
Total Posts:  48
Joined:  2008-08-05
 

By the way, I found that changing the image attribute frontend inputs from media_image to text in the database as described above will disable the ability to upload watermarks and placeholders. Not sure how to fix that problem, although for me it’s not really an issue.

 
Magento Community Magento Community
Magento Community
Magento Community
 
gfxguru
Sr. Member
 
Total Posts:  186
Joined:  2008-11-20
 

so there is no true way to import images, if you want to use watermarking?. I’ve been struggling with this one.

 
Magento Community Magento Community
Magento Community
Magento Community
 
smoothie
Jr. Member
 
Total Posts:  24
Joined:  2008-04-08
 

does anyone know if this works on the latest version 1.2.0.2?

 
Magento Community Magento Community
Magento Community
Magento Community
 
theallgreenstore
Member
 
Total Posts:  33
Joined:  2009-01-08
 

I need to do this same thing, but instead of having the images on my site, they are hosted externally by my merchants.  How would I modify the above to use that instead of a local path?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2