Page 1 of 2
[MOD] - Use "Location" field for External Product Image
Posted: Wed Feb 10, 2010 11:23 pm
by Qphoria
Sometimes you want to use images from another site for your products. Useful if you always want to have the latest image that the other site uses on your site. There are a few way it can be done.
One method would be to use a direct link to the location field and edit the tpl file to have width and height params in the <img tag.
I chose a slightly outside-the-box method where the code grabs the image from the link and saves a copy to your local image folder. I do this so that I can still use the built-in image resize functions of opencart since there are no template level sizing parameters in the default template.
The mod is very simple:
1. EDIT: catalog/controller/product/product.php
2. FIND:
Code: Select all
$this->data['popup'] = image_resize($image, $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
2. BEFORE, ADD:
Code: Select all
//Q: External Image Check using Location field
if (strpos($product_info['location'], 'http') !== false) {
$contents = file_get_contents($product_info['location']);
if ($contents) {
$tmpImage = basename($product_info['location']);
$fp = fopen(DIR_IMAGE . $tmpImage, 'w');
if ($fp) {
fwrite($fp, $contents);
fclose($fp);
$image = $tmpImage;
}
}
}
//
Now if you want to use an external link, simply paste the path to the image in the location field and it will override the normal image with the external one, save that file to your image folder, and resize it in the cache using the normal resize process. It will always grab the image from the external url so long as it exists.
Re: [MOD] - Use "Location" field for External Product Image
Posted: Tue Jun 22, 2010 11:20 pm
by tdweb
Hi
I try to use the direct link method using the location field. But it doesn't work. Its not showing the remote images! Either in the product page or in the list page.
Do I have to change any file to use location as image source?
I try your second method and it works for image on product page. In the list page is not showing any image.
Can you give me some help on this?
Re: [MOD] - Use "Location" field for External Product Image
Posted: Tue Jan 25, 2011 8:57 am
by nickt15
Hi,
I am using 1.4.9.3
I also am able to link to another image on another site per the instructions above. The image shows up on the product page but not the home page when you first add a product.
Is there any help out there on what else I can edit to also show the pic on the home page when first added & also keep on the product page ?
Please reply soon
Thanks in advance
Re: [MOD] - Use "Location" field for External Product Image
Posted: Sun Apr 10, 2011 6:32 am
by muratdekan
hi,
products seems to be 'no image on' the homepage. Do you have an arrangement for it?
Re: [MOD] - Use "Location" field for External Product Image
Posted: Sun Apr 10, 2011 6:55 am
by inactiveaccount9912
That is because the mod aplies to the product page only. If you want it in listing too , you have to repeat the process in the controllers of listings.
Re: [MOD] - Use "Location" field for External Product Image
Posted: Sun Apr 17, 2011 1:58 am
by gsaraiva
I tried as you describe, but does no work...

any tip?
Re: [MOD] - Use "Location" field for External Product Image
Posted: Sun Apr 17, 2011 3:07 am
by inactiveaccount9912
Try this (backup the file first)
In , for example catalog/controller/module/latest.php
Find
Code: Select all
if ($result['image']) {
$image = $result['image'];
} else {
$image = 'no_image.jpg';
}
And replace with
Code: Select all
//Q: External Image Check using Location field
if (strpos($result['location'], 'http') !== false) {
$contents = file_get_contents($result['location']);
if ($contents) {
$tmpImage = basename($result['location']);
$fp = fopen(DIR_IMAGE . $tmpImage, 'w');
if ($fp) {
fwrite($fp, $contents);
fclose($fp);
$image = $tmpImage;
}
}
} else {
if ($result['image']) {
$image = $result['image'];
} else {
$image = 'no_image.jpg';
}
}
//
Again , backup your file
Re: [MOD] - Use "Location" field for External Product Image
Posted: Mon Apr 25, 2011 1:15 am
by gsaraiva
Still no luck.
My example of image is :
http://pan7.fotovista.com/dev/7/1/00015 ... 015417.jpg
But no image appears.
The product link is:
http://www.digilow.com/p-7621-forno-26l ... =1&model=1
If you got any more tip i will apreciate.
Thank you
Re: [MOD] - Use "Location" field for External Product Image
Posted: Mon Apr 25, 2011 2:44 am
by gsaraiva
Re: [MOD] - Use "Location" field for External Product Image
Posted: Mon Apr 25, 2011 4:12 am
by inactiveaccount9912
Well , I tested it before , and it works. For the product page , you use the first one(catalog/controller/product/product.php) and for category listing you use the second on(catalog/controller/product/category.php)
All the respect!
Re: [MOD] - Use "Location" field for External Product Image
Posted: Mon Apr 25, 2011 6:24 am
by gsaraiva
If i give you access to my ftp coulp you check it for me?
It would be a great help, if you have a private email i will send you login and pass.
Thank you!!
Guilherme
Re: [MOD] - Use "Location" field for External Product Image
Posted: Mon Apr 25, 2011 6:40 am
by gsaraiva
Ok thank you , now it´s working, stupid of me i was using the image field not the location.
Thank you again
Re: [MOD] - Use "Location" field for External Product Image
Posted: Wed May 04, 2011 5:18 am
by rcdesign
hey it works great i also adjusted special.php and featured part and the new part.
only when i try do the code on cart.php i get a error:
Code: Select all
Notice: Undefined variable: product_info in /home/rcdesign77/domains/cristael-bvba.be/public_html/catalog/controller/checkout/cart.php on line 135Notice: Undefined variable: product_info in /home/rcdesign77/domains/cristael-bvba.be/public_html/catalog/controller/checkout/cart.php on line 135
someone got a solution cause it's the only place now where i can't see the image i added in location
Re: [MOD] - Use "Location" field for External Product Image
Posted: Sun May 22, 2011 5:01 pm
by gsaraiva
Hello, I´am having trouble in put the code in cart.php to, it does not give me any error , but "no image" appears.
Someone knows hoaw to put this working on cart.php ?
please check
www.digilow.com
Thank you
Re: [MOD] - Use "Location" field for External Product Image
Posted: Thu Jun 16, 2011 7:36 pm
by NTCommerce
Hi has anybody managed to get this working on 1.5.0.3 ? Or any version. I've tried the suggestion above but no joy and need to get this working today
Regards
Re: [MOD] - Use "Location" field for External Product Image
Posted: Sat Jul 16, 2011 8:45 am
by yorkbooksellers
Great code and I really appreciate it. I have impletemented throughout the site. I would like to add code to the catalog/view/theme/default/template/checkout/cart.tpl to complete the implementation. Your help would be greatly appreciated. We have about 700,000 skus and love the software.
Re: [MOD] - Use "Location" field for External Product Image
Posted: Fri Nov 18, 2011 5:53 am
by spires
Here is a mod to the mod above that checks to see if the product has an image already; if not, it takes in the remote image URL in the Location field and saves it, but also updates the Product listing in the database to point to the locally served image. In V1.5.1.3, "uploaded" files are saved in image/data which is the location I chose to save my remote images. You may need to modify this location depending upon version...?
I have added support for validating the file type and extension name as well as to deal with database served images that don't have a file extension at all. It looks up the headers during the call to the remote image for Content-Type validation. This script should fail gracefully if the content is not a valid URL or image type.
Code: Select all
//-----BEGIN REMOTE IMAGE EXTRACTION-----//
//If product does not have a locally stored image...
if($product_info['image'] === '') {
//Let's get an external Image using a URL stored in the Location field
if (strpos($product_info['location'], 'http') !== false) {
//Save the contents of the file to our variable
$contents = file_get_contents($product_info['location']);
//If we got some data from the URL...
if ($contents) {
$imgExt = '';
//Set the filename for the image to be saved locally
$tmpImage = basename($product_info['location']);
//Check to make sure the filename has a valid image extension
preg_match('/[^?]*/', $tmpImage, $matches);
$string = $matches[0];
$pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
//No extension?? It's probably an image served from a DB
if(count($pattern) == 1 || count($pattern) > 2) {
//Let's fetch the image type from the page headers
$imgHeader = get_headers($product_info['location']);
$imgTypes = array('jpg' => '.jpg', 'jpeg' => '.jpg', 'png' => '.png', 'gif' => '.gif');
//Cycle through image headers array to match an image type
foreach ( $imgHeader as $header => $head ) {
foreach ( $imgTypes as $type => $ext ) {
//Store the extension we matched
if(strpos($head, $type)) { $imgExt = $ext; }
}
}
//If we didn't find an appropriate content type then it isn't a valid image. Set ext to NONE as a failsafe
if($imgExt === '') {$imgExt = 'NONE';}
//Otherwise, we probably want to change the filename to a managable length since our URL was probably a long string of characters.
//Let's process the name to unique 8 char filename using a CRC32 hash
else { $tmpImage = hash('crc32', $tmpImage); }
//If the filename did have an extension initially, let's be sure it is a valid one
} else {
if(!(strpos($tmpImage, '.jpeg' )
|| strpos($tmpImage, '.jpg' )
|| strpos($tmpImage, '.gif' )
|| strpos($tmpImage, '.png' )
|| strpos($tmpImage, '.bmp' ))) {
//No valid Extension? Let's fetch the image type from the page headers
$imgHeader = get_headers($product_info['location']);
$imgTypes = array('jpg' => '.jpg', 'jpeg' => '.jpg', 'png' => '.png', 'gif' => '.gif');
//Cycle through image headers array to match an image type
foreach ( $imgHeader as $header => $head ) {
foreach ( $imgTypes as $type => $ext ) {
//Store the extension we matched
if(strpos($head, $type)) { $imgExt = $ext; }
}
}
//If we didn't find an appropriate content type, then it isn't a valid image. Set ext to NONE as a failsafe
if($imgExt === '') {$imgExt = 'NONE';}
}
}
//Now that we have validated the filename and image type
if($imgExt !== 'NONE') {
//Add the file extension to the end of our filename, if necessary
$tmpImage .= $imgExt;
//Save the file to our image folder
file_put_contents(DIR_IMAGE . "data/" . $tmpImage, $contents);
$product_info['image'] = "data/" . $tmpImage;
//Let's update the database to reflect the location of our new file so we only call the local file from now on.
$product_id = $product_info['product_id'];
$this->db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $this->db->escape($product_info['image']) . "' WHERE product_id = '" . (int)$product_id . "'");
}
}
}
} //-----END IMAGE EXTRACTION-----//
I implemented this mod in V1.5.1.3 in controller/product/product.php just after:
Remote extraction happens before any image manipulation is performed on the product page and is only done once during the life-cycle of the product (or until you clear the image from within the catalog admin.)
I believe this script could be better served somewhere during the search function to extract multiple product images at once. Haven't looked to see where I should move it to, yet. I'll let you know when I figure it out.
Any corrections / additions / modifications are welcome.
Re: [MOD] - Use "Location" field for External Product Image
Posted: Sun Jan 29, 2012 6:46 pm
by urusite
Hi,
Let me see if I understand this correctly.
what this code does is
1. save a thumbnail of an external image in its folder and
2. link to external image with something like <img src="photobucket.com/user....">
like this it can save the webspace of those bulk pictures, right? or else there is no point in doing this.
(if this is not doing this, can anyone tell me how else can i use external image as product image?)
if this is working, does it work with 1.513? can anyone tell me what exactly is the latest code supposed to be?
thank you very much!
Urusite from Hong Kong
Re: [MOD] - Use "Location" field for External Product Image
Posted: Sun Mar 03, 2013 1:42 am
by aveirense
hi
why if I apply the modification you said it show me this:
Warning: file_get_contents(
https://webservice.msx-mysite.com/index ... ge&id=1090) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /membri/tantopiacere3/catalog/controller/product/product.php on line 197
?
Re: [MOD] - Use "Location" field for External Product Image
Posted: Mon Mar 04, 2013 9:38 pm
by aveirense
Hi,
I resolved the problem...because it was mine error.
The code works fine with an image lìke this:
www.mysite.com/image.jpg but
don't work with an image like this
https://mysite.com/index.php?action=getimage&id=026yZ
How we can do?
Thanks