Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 5
Using Remote Image URLs
 
cssc
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2008-04-24
 

Rather than uploading a product image, is it possible to enter the URL for a remotely stored image and have Magento pull the image from that location (each product would have its’ own image url).

If so, can these URLs be part of the import file?

 
Magento Community Magento Community
Magento Community
Magento Community
 
alkarim
Guru
 
Total Posts:  368
Joined:  2008-04-10
 

this is my problem either… geez

 
Magento Community Magento Community
Magento Community
Magento Community
 
experimentor
Jr. Member
 
Avatar
Total Posts:  5
Joined:  2008-02-26
Loganville, Georgia
 

What I am going to try and do is to make a custom attribute called remote_image_url.  I’m going to create it as a text field, and make it visible on the catalog pages, and then I am going to tweak my template to see if i can use this attribute as an image tag.  That’s about the best thing I can think of other than overrriding the class that handles images - not something I want to do right now.  May be you might want to try what I plan on trying....

 
Magento Community Magento Community
Magento Community
Magento Community
 
cssc
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2008-04-24
 
experimentor - 26 April 2008 03:07 AM

What I am going to try and do is to make a custom attribute called remote_image_url.  I’m going to create it as a text field, and make it visible on the catalog pages, and then I am going to tweak my template to see if i can use this attribute as an image tag.  That’s about the best thing I can think of other than overrriding the class that handles images - not something I want to do right now.  May be you might want to try what I plan on trying....

You are the ”Experimentor”!

This sounds really great.  I hope we can populate this text field using the import feature.  I am not familiar with the coding to make that happen myself.  Please let me know what you find out.  I hope you will share you’re work.  Thanks again.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  182
Joined:  2008-01-15
France
 

+1. That’s too bad external URL for images are not working.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  182
Joined:  2008-01-15
France
 

Héhé, finally, I hacked the code by myself! smile It took me about 5 hours to find out how the whole code works and to find where it was (there are so many files)…

I managed to modify the minimum so that you can do it in 1 minute....
Features:
- You put an external URL inside your inventory (like http://www.google.fr/intl/fr_fr/images/logo.gif)
- The hack detects that it starts by http:// and overrides the default prohibition to have such a file.
- It adopts a different behaviour for the image output if it detects that it starts by http://.
I had to replace the : by {{{2dots}}] because the setData function didn’t like them. So I do a preg_replace in the toString function.

Here are the steps:
1. Open /app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php. At the beginning of the function addImage, line 168, replace:

$file realpath($file);

        if (!
$file{
            Mage
::throwException(Mage::helper('catalog')->__('Image not exists'));
        
}

        $pathinfo 
pathinfo($file);

        if (!isset(
$pathinfo['extension']) || !in_array($pathinfo['extension'], array('jpg','jpeg','gif','png'))) {
            Mage
::throwException(Mage::helper('catalog')->__('Invalid image file type'));
        
}

        $fileName       
Varien_File_Uploader::getCorrectFileName($pathinfo['basename']);
        
$dispretionPath Varien_File_Uploader::getDispretionPath($fileName);
        
$fileName       $dispretionPath DS $fileName;
        
$fileName $dispretionPath DS
                  
Varien_File_Uploader::getNewFileName($this->_getConfig()->getTmpMediaPath($fileName));
        
        
$ioAdapter = new Varien_Io_File();
        
$ioAdapter->setAllowCreateFolders(true);
        
$distanationDirectory dirname($this->_getConfig()->getTmpMediaPath($fileName));
        try 
{
            $ioAdapter
->open(array(
                
'path'=>$distanationDirectory
            
));

            if (
$move{
                $ioAdapter
->mv($file$this->_getConfig()->getTmpMediaPath($fileName));
            
else {
                $ioAdapter
->cp($file$this->_getConfig()->getTmpMediaPath($fileName));
                
$ioAdapter->chmod($this->_getConfig()->getTmpMediaPath($fileName), 0777);
            
}
        }
        
catch (Exception $e{
            Mage
::throwException(Mage::helper('catalog')->__('Failed to move file: %s'$e->getMessage()));
        
}

with:

$pattern '/^http:\/\//'//begin custom
        
if (preg_match($pattern$file) == 0{
            $file 
realpath($file);

            if (!
$file{
                Mage
::throwException(Mage::helper('catalog')->__('Image not exists'));
            
}

            $pathinfo 
pathinfo($file);

            if (!isset(
$pathinfo['extension']) || !in_array($pathinfo['extension'], array('jpg','jpeg','gif','png'))) {
                Mage
::throwException(Mage::helper('catalog')->__('Invalid image file type'));
            
}

            $fileName       
Varien_File_Uploader::getCorrectFileName($pathinfo['basename']);
            
$dispretionPath Varien_File_Uploader::getDispretionPath($fileName);
            
$fileName       $dispretionPath DS $fileName;
            
$fileName $dispretionPath DS
                      
Varien_File_Uploader::getNewFileName($this->_getConfig()->getTmpMediaPath($fileName));
            
            
$ioAdapter = new Varien_Io_File();
            
$ioAdapter->setAllowCreateFolders(true);
            
$distanationDirectory dirname($this->_getConfig()->getTmpMediaPath($fileName));
            try 
{
                $ioAdapter
->open(array(
                    
'path'=>$distanationDirectory
                
));

                if (
$move{
                    $ioAdapter
->mv($file$this->_getConfig()->getTmpMediaPath($fileName));
                
else {
                    $ioAdapter
->cp($file$this->_getConfig()->getTmpMediaPath($fileName));
                    
$ioAdapter->chmod($this->_getConfig()->getTmpMediaPath($fileName), 0777);
                
}
            }
            
catch (Exception $e{
                Mage
::throwException(Mage::helper('catalog')->__('Failed to move file: %s'$e->getMessage()));
            
}
        }
        
else {
            $fileName 
str_replace(":"'{{{2dots}}}'$file);
        
//end custom

2. Open /app/code/core/Mage/Catalog/Helper/Image.php. At the beginning of the __toString() method (that controls the output of the object), on line 111, add the following piece of code:

$pattern '/^http{{{2dots}}}\/\//'//begin custom
            
$customUrl $this->getProduct()->getData($this->_getModel()->getDestinationSubdir());
            if (
preg_match($pattern$customUrl) > 0{
                $url 
preg_replace($pattern'http://'$customUrl);
                return 
$url;
            
//end custom

3. Open /app/code/core/Mage/Catalog/Model/Product/Attribute/Frontend/. Just before the function starts, at line 34, add:

$pattern '/^http{{{2dots}}}\/\//'//begin custom
        
$customUrl $object->getData($this->getAttribute()->getAttributeCode());
        if (
preg_match($pattern$customUrl) > 0{
            $url 
preg_replace($pattern'http://'$customUrl);
            return 
$url;
        
//end custom

Since this is a hack, you will have to reapply it each time you update Magento. But if someone is ready to make a module based on this, I will appreciate.

I hope it helps! If you feel you are lucky, don’t hesitate to thank the Good here.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Shpigford
Sr. Member
 
Avatar
Total Posts:  171
Joined:  2007-09-03
Denver, CO
 

mayerwin: I tried your hack, but had no luck. It still does the same as before and doesn’t import external images. No errors, it just doesn’t do it.

I’m running Magento 1.0.19870.1

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  182
Joined:  2008-01-15
France
 

Well Shpigford, it is not supposed to import external images, but only to authorise you to ask Magento to display images hosted externally.
However it should not be that hard to add some PHP lines that copy the remote file into the local server…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@mayerwin: did you try just adding remote_image_url text attribute and use it in templates like this:

<img src="<?php echo $_product->getRemoteImageUrl() ?>" />
 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  182
Joined:  2008-01-15
France
 

That’s really a good idea Moshe! I would love to master Magento as you do.... But I’m confident such great solutions will become even more obvious with practice and reading of the documentation grin.

I’ll let you know soon if this works (although there is no reason it wouldn’t), and works better than my hack. What is interesting is that it will be surely better for upgrades (you won’t need to hack the core files again, only the template files).

 
Magento Community Magento Community
Magento Community
Magento Community
 
JayKay8875
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2008-04-22
LA
 
Moshe - 09 June 2008 09:37 PM

@mayerwin: did you try just adding remote_image_url text attribute and use it in templates like this:

<img src="<?php echo $_product->getRemoteImageUrl() ?>" />

Could you please explain how to do this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sebastian G
Guru
 
Total Posts:  336
Joined:  2007-09-15
 

@ JayKay8875

1. Go to Admin > Catalog > Attributes > Manage Attributes

2. Add New Attribute, set following attribute configuration, leave the default values unaltered for the fields not mentioned below, and Save:

Attribute Code: remote_image_url
Scope: Store View
Catalog Input Type for Store Owner: Text Field

Manage Labels / Options > Admin: Remote Image URL

3. Go to Admin > Catalog > Attributes > Manage Attribute Sets

4. Edit your attribute sets, drag’n’drop remote_image_url attribute to your sets (suggested - Images group), and Save.

5. Edit your products and set Remote Image URL = http://remote-host.com/remote/image.gif (replace with your image url)

6. Edit your custom theme files and replace internal images url with external as needed, examples:

CHANGE TEMPLATES FOR PRODUCT DETAILS VIEW:

FILE: app/design/frontend/[interface]/[theme]/template/catalog/product/view/media.phtml

FIND: (approx line 29)

<?php echo $this->htmlEscape($_product->getName()) ?>

REPLACE WITH:

<?php echo $this->htmlEscape($_product->getName()) ?>

FIND: (approx line 46)

<?php echo $this->htmlEscape($_product->getName()) ?>

REPLACE WITH:

<?php echo $this->htmlEscape($_product->getName()) ?>

CHANGE LAYOUTS AND TEMPLATES FOR CATEGORY PRODUCT LIST:

FILE: app/design/frontend/[interface]/[theme]/layout/catalog.xml

FIND: (approx lines 73 and 103)

APPEND RIGHT AFTER:

remote_image_url

FILE: app/design/frontend/[interface]/[theme]/template/catalog/product/list.phtml

FIND: (approx line 45)

<?php echo $this->htmlEscape($_product->getName()) ?>

REPLACE WITH:

<?php echo $this->htmlEscape($_product->getName()) ?>

FIND: (approx line 93)

<?php echo $this->htmlEscape($_product->getName()) ?>

REPLACE WITH:

<?php echo $this->htmlEscape($_product->getName()) ?>

7. You can create few attributes in the same way for small, medium and large images.

I hope these more specific instructions are helpful.

 
Magento Community Magento Community
Magento Community
Magento Community
 
JayKay8875
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2008-04-22
LA
 

Thank you.

I will give it a shot.

 
Magento Community Magento Community
Magento Community
Magento Community
 
JayKay8875
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2008-04-22
LA
 

Ok

I have followed your steps up until changing the files

I dont understand why I find <?php echo $this->htmlEscape($_product->getName()) ?> and replace it with the same thing. <?php echo $this->htmlEscape($_product->getName()) ?>

Quote:

FILE: app/design/frontend/[interface]/[theme]/template/catalog/product/view/media.phtml

FIND: (approx line 29)

<?php echo $this->htmlEscape($_product->getName()) ?>

REPLACE WITH:

<?php echo $this->htmlEscape($_product->getName()) ?>

FIND: (approx line 46)

<?php echo $this->htmlEscape($_product->getName()) ?>

REPLACE WITH:

<?php echo $this->htmlEscape($_product->getName()) ?>

End Quote:

Could you please explain a little more?

Many thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sebastian G
Guru
 
Total Posts:  336
Joined:  2007-09-15
 

@JayKay8875: Sorry i had an issue with my text editor oh oh
Here is how it should go:

1. Go to Admin > Catalog > Attributes > Manage Attributes

2. Add New Attribute, set following attribute configuration, leave the default values unaltered for the fields not mentioned below, and Save:

Attribute Code: remote_image_url
Scope: Store View
Catalog Input Type for Store Owner: Text Field

Manage Labels / Options > Admin: Remote Image URL

3. Go to Admin > Catalog > Attributes > Manage Attribute Sets

4. Edit your attribute sets, drag’n’drop remote_image_url attribute to your sets (suggested - Images group), and Save.

5. Edit your products and set Remote Image URL = http://remote-host.com/remote/image.gif (replace with your image url)

6. Edit your custom theme files and replace internal images url with external as needed, examples:

CHANGE TEMPLATES FOR PRODUCT DETAILS VIEW:

FILE: app/design/frontend/[interface]/[theme]/template/catalog/product/view/media.phtml

FIND: (approx line 29)

<img id="image" src="<?php echo $this->helper('catalog/image')->init($_product, 'image'); ?>” alt="<?php echo $this->htmlEscape($_product->getName()) ?>” />

REPLACE WITH:

<img id="image" src="<?php echo $_product->getData('remote_image_url'); ?>” alt="<?php echo $this->htmlEscape($_product->getName()) ?>” />

FIND: (approx line 46)

<img id="image" src="<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(265); ?>” alt="<?php echo $this->htmlEscape($_product->getName()) ?>” />

REPLACE WITH:

<img id="image" src="<?php echo $_product->getData('remote_image_url'); ?>” alt="<?php echo $this->htmlEscape($_product->getName()) ?>” />

CHANGE LAYOUTS AND TEMPLATES FOR CATEGORY PRODUCT LIST:

FILE: app/design/frontend/[interface]/[theme]/layout/catalog.xml

FIND: (approx lines 73 and 103)

<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">

APPEND RIGHT AFTER:

<action method="addAttribute"><code>remote_image_url</code></action>

FILE: app/design/frontend/[interface]/[theme]/template/catalog/product/list.phtml

FIND: (approx line 45)

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

REPLACE WITH:

<img src="<?php echo $_product->getData('remote_image_url'); ?>” width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>” />

FIND: (approx line 93)

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

REPLACE WITH:

<img src="<?php echo $_product->getData('remote_image_url'); ?>” width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>” />

7. You can create few attributes in the same way for small, medium and large images.

Let me know if that worked out for you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
JayKay8875
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2008-04-22
LA
 

Thank you for the fast reply, I will let you know if it works.

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