Post by Qphoria » Wed Feb 10, 2010 11:23 pm

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.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by tdweb » Tue Jun 22, 2010 11:20 pm

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?

New member

Posts

Joined
Fri Jun 04, 2010 10:35 pm

Post by nickt15 » Tue Jan 25, 2011 8:57 am

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

New member

Posts

Joined
Tue Jan 25, 2011 8:50 am

Post by muratdekan » Sun Apr 10, 2011 6:32 am

hi,

products seems to be 'no image on' the homepage. Do you have an arrangement for it?

Newbie

Posts

Joined
Mon Apr 04, 2011 3:16 am

Post by inactiveaccount9912 » Sun Apr 10, 2011 6:55 am

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.

Expert Member

Posts

Joined
Fri May 14, 2010 2:36 am

Post by gsaraiva » Sun Apr 17, 2011 1:58 am

I tried as you describe, but does no work... :( any tip?

Newbie

Posts

Joined
Sun Jan 30, 2011 5:16 am

Post by inactiveaccount9912 » Sun Apr 17, 2011 3:07 am

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

Expert Member

Posts

Joined
Fri May 14, 2010 2:36 am

Post by gsaraiva » Mon Apr 25, 2011 1:15 am

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

Newbie

Posts

Joined
Sun Jan 30, 2011 5:16 am

Post by gsaraiva » Mon Apr 25, 2011 2:44 am

Sorry the limk is wrong, here is the new one:

http://www.digilow.com/p-7621-blister-1 ... egory_id=0

Newbie

Posts

Joined
Sun Jan 30, 2011 5:16 am

Post by inactiveaccount9912 » Mon Apr 25, 2011 4:12 am

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!

Expert Member

Posts

Joined
Fri May 14, 2010 2:36 am

Post by gsaraiva » Mon Apr 25, 2011 6:24 am

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

Newbie

Posts

Joined
Sun Jan 30, 2011 5:16 am

Post by gsaraiva » Mon Apr 25, 2011 6:40 am

Ok thank you , now it´s working, stupid of me i was using the image field not the location.

Thank you again

Newbie

Posts

Joined
Sun Jan 30, 2011 5:16 am

Post by rcdesign » Wed May 04, 2011 5:18 am

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

Newbie

Posts

Joined
Wed May 04, 2011 1:45 am

Post by gsaraiva » Sun May 22, 2011 5:01 pm

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

Newbie

Posts

Joined
Sun Jan 30, 2011 5:16 am

Post by NTCommerce » Thu Jun 16, 2011 7:36 pm

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

New member

Posts

Joined
Tue Jun 14, 2011 7:51 pm

Post by yorkbooksellers » Sat Jul 16, 2011 8:45 am

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.


Posts

Joined
Fri May 27, 2011 10:36 am

Post by spires » Fri Nov 18, 2011 5:53 am

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:

Code: Select all

$this->load->model('tool/image');
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.

Newbie

Posts

Joined
Tue Oct 25, 2011 8:57 pm

Post by urusite » Sun Jan 29, 2012 6:46 pm

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

Newbie

Posts

Joined
Sun Jan 29, 2012 5:13 pm

Post by aveirense » Sun Mar 03, 2013 1:42 am

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

?

Newbie

Posts

Joined
Sun Mar 03, 2013 1:36 am

Post by aveirense » Mon Mar 04, 2013 9:38 pm

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

Newbie

Posts

Joined
Sun Mar 03, 2013 1:36 am
Who is online

Users browsing this forum: No registered users and 97 guests