You are here:  » No image


No image

Submitted by chrisst1 on Fri, 2006-09-08 15:55 in

David
Is there any way to code "if no product image use a default image"

Chris

Submitted by support on Fri, 2006-09-08 17:07

Hi Chris,

No problem - you'll need to make changes to html/searchresults.php and html/product.php.

In html/searchresults.php, find the following code (starting at line 9):

          <?php if ($product["image_url"]): ?>
          <a href='<?php print $product["productHREF"]; ?>'><img border='0' width='80' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' /></a>
          <?php endif; ?>

...and change as follows:

          <?php if ($product["image_url"]): ?>
          <a href='<?php print $product["productHREF"]; ?>'><img border='0' width='80' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' /></a>
          <?php else: ?>
          <img width='80' src='/path/to/default.gif' />
          <?php endif; ?>

And in html/product.php, find the following code, starting at line 6:

        <?php if ($mainProduct["image_url"]): ?>
          <img width='180' src='<?php print $mainProduct["image_url"]; ?>' alt='<?php print $mainProduct["name"]; ?>' />
        <?php endif; ?>

...and change as follows:

        <?php if ($mainProduct["image_url"]): ?>
          <img width='180' src='<?php print $mainProduct["image_url"]; ?>' alt='<?php print $mainProduct["name"]; ?>' />
        <?php else: ?>
          <img width='200' src='/path/to/default.gif' />
        <?php endif; ?>

You might actually want to use a different default image for the small (search results) and large (product page) images; and also note that i've used an absolute path to the image - this is important so that the file can be found whatever page of your site it is on.

Cheers,
David.

Submitted by chrisst1 on Sat, 2006-09-09 10:37

Thanks Dave super job, should put an end to those red crosses.

Chris
villagefetes

Submitted by pikibou on Sat, 2007-08-18 11:29

Don't know what's wrong but for me it doesn't work:

Here's my code for product.php

<?php if ($mainProduct["image_url"]): ?>
          <img width='170' onload='JavaScript:if(this.width > 200) this.width=200;' src='<?php print $mainProduct["image_url"]; ?>' alt='<?php print $mainProduct["name"]; ?>' />
        <?php else: ?>
          <img width='170' src='/images/noimage.gif' />
        <?php endif; ?>

I have tried almost everything:

- full path to image
- remove onload'javascript ...
- looking at my image on my server
- trying withjpg / gif / ...
- removing url rewrite

In fact, it doesn't seem to recognize those lines:

        <?php else: ?>
          <img width='170' src='/images/noimage.gif' />
        <?php endif; ?>

The same for searchresults.php

Anyone has the solution?

Thanks

Piki

Submitted by pikibou on Sat, 2007-08-18 12:11

OK, I know why it doesn't work for me / and certainly for you also:

When there's a 404 for the image it shows nothing.

Here's the code I've found on the blog of someone using pricetapestry: targetweb

<?php
$image_to_check = $mainProduct["image_url"];
$html = @getimagesize($image_to_check);
if (!$html){
$productimage = "/images/noimages.gif";
}
else {
$productimage = $mainProduct["image_url"];
}
?>
<p><a href='<?php print $product["productHREF"]; ?>'><img border='0' width='80' height='80' src='<?php print $productimage?>' alt='<?php print $product["name"]; ?>' class='image' /></a>

For instance, it checks the size of the image. If no size, then it shows the noimage.gif

--> very nice but I still don't know how to adapt it to product.php

Submitted by support on Sat, 2007-08-18 16:12

Hi Piki,

The product image is displayed on the product page by the following code on line 7 of html/product.php:

<img width='180' src='<?php print $mainProduct["image_url"]; ?>' alt='<?php print $mainProduct["name"]; ?>' />

You should be able to replace this with similar code to the above mod as follows:

<?php
$image_to_check = $mainProduct["image_url"];
$html = @getimagesize($image_to_check);
if (!$html){
$productimage = "/images/noimages.gif";
}
else {
$productimage = $mainProduct["image_url"];
}
?>
<img width='180' src='<?php print $productimage?>' alt='<?php print $mainProduct["name"]; ?>' />

Hope this helps,
Cheers,
David.

Submitted by pikibou on Sun, 2007-08-19 10:22

Thanks for the tip!

It works!

Submitted by babyuniverse on Sun, 2007-09-16 10:53

Hi All,

<?php
$image_to_check 
$mainProduct["image_url"];
$html = @getimagesize($image_to_check);
if (!
$html){
$productimage "/images/noimages.gif";
}
else {
$productimage $mainProduct["image_url"];
}
?>


Works well for product.php - what about searchreuslts.php - still cant get this to work

Submitted by support on Sun, 2007-09-16 11:35

Hi,

In searchresults.php, you will need to use:

$product["image_url"]

instead of:

$mainProduct["image_url"]

If you're not sure where to use this in searchresults.php, have a look at the code in the first reply in this thread...

Cheers,
David.

Submitted by babyuniverse on Sun, 2007-09-16 12:23

this is my code in searchresults.php

<?php
$image_to_check = $product["image_url"];
$html = @getimagesize($image_to_check);
if (!$html){
$productimage = "http://www.domain.com/shopping/images/noimage.gif";
}
else {
$productimage = $product["image_url"];
}
?>
<a href='<?php print $product["productHREF"]; ?>'><img border='0' width='80' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' /></a>

Submitted by support on Sun, 2007-09-16 12:26

Hi,

That looks almost correct - I think the only thing missing is you need to replace the image URL in the HTML with your new $productimage variable; like this:

<?php
$image_to_check = $product["image_url"];
$html = @getimagesize($image_to_check);
if (!$html){
$productimage = "http://www.domain.com/shopping/images/noimage.gif";
}
else {
$productimage = $product["image_url"];
}
?>
<a href='<?php print $product["productHREF"]; ?>'><img border='0' width='80' src='<?php print $productimage?>' alt='<?php print $product["name"]; ?>' /></a>

Hope this helps,
Cheers,
David.

Submitted by babyuniverse on Sun, 2007-09-16 12:32

thanks again.

This is now working great

Submitted by clare on Wed, 2008-02-27 11:51

I also find this all works well..except that today my affiliate window feeds, where the image url is from AW server such as
http://images.productserve.com/preview/1525/9956352.jpg

These are all showing as no image when passed through the @getimagesize filter in the code, and so I wonder is it possible that the check image size code, is somehow a problem for some servers? As it only appears to be happening on the AW feeds, where I know the images are there, but are showing in PT as no image using the above code that gets the image size.

Submitted by clare on Wed, 2008-02-27 12:33

Actually, I have just removed the check image size code and put your original code only, as it is not so often that the image urls are incorrect and return a 404 necessitating that extra getimagesize code. And I found that adding the check image size was also really slowing things down alot.

Submitted by support on Wed, 2008-02-27 12:37

Hi Clare,

I was going to recommend a similar course of action - the function getimagesize is effectively making a request to fetch all images from the remote server before the page is displayed, so under normal circumstances it's certainly not ideal...!

Cheers,
David.

Submitted by clare on Thu, 2008-02-28 11:58

Just for future info on this thread, I stumbled across a little dreamweaver extension yesterday that effectively shows a substitute image when an image link is broken, using the DOM on page javascript method.

It can be found at
http://www.linecraft.com/hide_broken/help.php

Submitted by remco on Tue, 2008-03-04 10:32

David
I,m having the same problem but both solutions dont work for me
What can i do ?

Its driving me nuts

Regards
Remco

Submitted by support on Tue, 2008-03-04 11:14

Hello Remco,

Are you trying to sort the invalid images issue (where there are image URLs but they don't work - this is quite hard to fix); or to show a default image if there is no image in the feed?

Cheers,
David.

Submitted by remco on Tue, 2008-03-04 12:10

The last one David
show a default image if there is no image in the feed?

You have my ftp details perhaps you can see whats wrong.

Best Regards
Remco

Submitted by support on Tue, 2008-03-04 12:13

Hello Remco,

I have still not received any email from you!

Did you reply to the one I sent to you?

Cheers,
David.

Submitted by remco on Tue, 2008-03-04 12:39

Yes david
very strange
I shall send it from the site its email adres
So i resend it from another new email adres

Best Regards
Remco

Submitted by support on Tue, 2008-03-04 13:22

Hello Remco,

I received your email now, thanks!

I have made the no image change, and also fixed your modified rewrite problem.

Cheers,
David.

Submitted by nosferatu on Fri, 2008-03-14 18:45

Hi,

Small change on this code:

!!! Works with html/product.php !!!

<?php
// $image_to_check = $product["image_url"];
$image_to_check = $mainProduct["image_url"];
$html = @getimagesize($image_to_check);
if (!$html){
$productimage = "http://www.site.com/images/noimage.gif";
}
else {
// $productimage = $product["image_url"];
$productimage = $mainProduct["image_url"];
}
?>
<!-- <a href='<?php print $product["productHREF"]; ?>'><img border='0' width='80' src='<?php print $productimage?>' alt='<?php print $product["name"]; ?>' /></a> -->
    <a href='<?php print $product["productHREF"]; ?>'><img border='0' width='80' src='<?php print $productimage?>' alt='<?php print $product["name"]; ?>' /></a>

Submitted by TWDesigns on Tue, 2008-07-29 17:18

Is there a better way to do this? I used the code mentioned above for searchresults.php and it loads the results really really slow, I'm assuming because it's checking for product image then no image?

Submitted by support on Tue, 2008-07-29 18:24

Hi,

As this method involves checking EVERY image as a page is loading, if the merchants server is being slow the page load will be significantly affected.

My personal preference is not to register images for merchants who's images are frequently missing or have other regular problems. If you choose to do this, you can display an image not available image if there is no image URL, by modifying html/searchresults.php and changing:

          <?php if ($product["image_url"]): ?>
          <a href='<?php print $product["productHREF"]; ?>'><img border='0' width='80' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' /></a>
          <?php endif; ?>

to:

          <?php if ($product["image_url"]): ?>
          <a href='<?php print $product["productHREF"]; ?>'><img border='0' width='80' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' /></a>
          <?php else: ?>
          <img width='80' src='/images/noimage.gif' />
          <?php endif; ?>

Cheers,
David.

Submitted by Syed on Wed, 2008-08-20 22:32

Hi David,

The above code works well in searchresults and product.php. In the index page in featured products still broken image appears. I changed to above code in featured.php but still not working. Any idea where it is going wrong?

Thanks,
Syed

Submitted by support on Thu, 2008-08-21 06:28

Hello Syed,

The relevant part of the original html/featured.php is as follows starting at line 10:

          <?php if ($product["image_url"]): ?>
            <p><a href='<?php print $product["productHREF"]; ?>'><img border='0' height='100' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' /></a></p>
          <?php endif; ?>

Therefore, to modify this as per the above, the following should work:

          <?php if ($product["image_url"]): ?>
            <p><a href='<?php print $product["productHREF"]; ?>'><img border='0' height='100' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' /></a></p>
          <?php else: ?>
            <img height='100' src='/path/to/default.gif' />
          <?php endif; ?>

Cheers,
David.

Submitted by stevencrocker on Sat, 2009-05-16 16:06

I think I have found a better solution to this replace broken image problem.

I just can't seem to get it to work within the template.

Can you take a look at the following url and let me know how/if this can be implemented.

Thanks!

{link saved}

Submitted by support on Sun, 2009-05-17 07:55

Hi Steven,

If you could email me your modified files i'll check it out for you; others have tried JavaScript solutions but found the result somewhat ugly but worth giving a go...

Cheers,
David.

Submitted by Keeop on Wed, 2009-06-24 18:03

Hi all,

I've just implemented this bit of code to deal with broken images, which seems to work very well. You will need to download and use the JQuery library and once you've got this referenced, place this code before the /head tag:

$(window).bind('load', function() {
$('img').each(function() {
if((typeof this.naturalWidth != "undefined" &&
this.naturalWidth == 0 )
|| this.readyState == 'uninitialized' ) {
$(this).attr('src', '/images/noimage.gif');
}
});
})

This will not scan for an alternative image to use but instead will display whatever default image you select - in my case noimage.gif.

As there's no scanning or checking remote images - it's all Javascript client side - it's pretty quick with minimal load. The code will check every image on the page that's using the standard img tag so there's no need to alter any of your pages and you can still use Lightbox type effects without problems.

Cheers.
Keeop

Submitted by bat on Mon, 2011-04-04 22:12

Is the code for the default image the same as this for the latest release? I tried your original one David at the top of the thread, but it didn't work.

Submitted by support on Tue, 2011-04-05 06:49

Hello bat,

Yes - absolutely the same for the latest distribution.

There are 2 things that may be happening. Firstly, it could be that your feeds _do_ have image_urls, but they are broken. To check this, when viewing the product page, use your browser's View > Source menu to look at the HTML, and search for the product IMG tag (for example by searching for width='180'). If you see what looks like a valid image URL at this point; try copying the URL and pasting it directly into your browser's address bar to confirm that it isn't working.

What I normally do in the case of a merchant that does not have reliable images is actually not to register the image URL field at all during Feed Registration (Step 2) for the associated feed. After that's done, your local "no image" image should be used instead.

If that doesn't appear to be the case, double check that the path to your "no image" image is correct; in particular if you have Price Tapestry installed in a sub-directory make sure that the sub-directory name is included in the path, for example:

<img width='80' src='/comparison/images/noimage.jpg' />

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Tue, 2011-04-05 07:04

Hi David
Thank you for your help, I was being gormless with the path but all is well now
Cheers

Submitted by nosferatu on Tue, 2012-12-25 18:37

I found a simple solution for the no image problem..

Put this code in your html/header.php

function onImgError(source) {
source.src = "http://www.yoursite.com/default.png";
// disable onerror to prevent endless loop
source.onerror = "";
return true;
}

Then in html/product.php on the line with the product image after scr=... put this onerror='onImgError(this)'
Same on html/searchresults.php on product image put onerror='onImgError(this)'

This solves and the problem with pageload.

Submitted by webman on Wed, 2017-01-18 03:27

Hi David,

Does the missing image code above relate to the 16/10A version of PT?

Many thanks

les :)

Submitted by support on Wed, 2017-01-18 08:18

Hello Les,

For 16/10A (Responsive HTML templates), for Search Results, edit html/searchresults.php and look for the placeholding non-braking space at line 23:

&nbsp;

...and REPLACE with:

<img class='pt_sr_image' src='<?php print $config_baseHREF?>images/default.gif' />

And for the main product page, edit html/product.php look for the following code at line 50:

  <?php endif; ?>

...and REPLACE with:

  <?php else: ?>
    <div class='small-2 columns show-for-small-only'>&nbsp;</div>
    <div class='small-8 medium-4 columns'><img src='<?php print $config_baseHREF?>images/default.gif' /></div>
    <div class='small-2 columns show-for-small-only'>&nbsp;</div>
  <?php endif; ?>

(with default image uploaded to the installation as images/default.gif)

Cheers,
David.
--
PriceTapestry.com