wright67uk Posted February 14, 2015 Share Posted February 14, 2015 I'm using the below code to display plant names with their accompanying images. The images are hot-linked (with the permission of the owner). The issue is sometimes an image doesn't exist, has been moved or the file name has been changed. When this happens, I want to display an image named sorry_lg.jpg instead. How do I go about doing this? I was thinking about giving the containing div a pre-defined background image, and then covering the background with the hot linked image. But surely there is a cleaner way of doing this? $result = db_query("SELECT * FROM `shrubs` WHERE `type` = '$cat' order by `name` "); if($result === false) { echo "something went wrong with this query"; } else { // Fetch all the rows in an array $rows = array(); while ($row = mysqli_fetch_assoc($result)) { // $rows[] = $row; $name = $row["name"]; $salesprice = $row["salesprice"]; // $image = str_replace(' ', '-', $name); $image = preg_replace('/[\s-]+/', '-', $name); $imagelow = strtolower($image); echo ' <div class="product"> <div style="float:left; width:170px"><img src="http://www.old-hall.com/uploads/images/osb/'.$imagelow.'_lg.jpg" width="150px" height="150px" /></div> <div style="float:right"><div style="margin-left:auto; margin-right:auto"><a href="http://www.1pw.co.uk/product_info.php?name='.$name.'"></div>'.$name.' - £'.$salesprice.'</a></div> </div> '; } } Quote Link to comment https://forums.phpfreaks.com/topic/294615-replace-image-where-none-displayed/ Share on other sites More sharing options...
QuickOldCar Posted February 14, 2015 Share Posted February 14, 2015 (edited) If was your own images would be able to do this $imagelow = strtolower($image); if (!file_exists($_SERVER['DOCUMENT_ROOT']."/uploads/images/osb/".$imagelow."_lg.jpg")) { $imagelow = 'sorry'; } Since is another website the only way is to download it or at least partially download the image first to tell if it exists. Edited February 14, 2015 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/294615-replace-image-where-none-displayed/#findComment-1505702 Share on other sites More sharing options...
wright67uk Posted February 14, 2015 Author Share Posted February 14, 2015 Is there a cURL method? Quote Link to comment https://forums.phpfreaks.com/topic/294615-replace-image-where-none-displayed/#findComment-1505703 Share on other sites More sharing options...
QuickOldCar Posted February 14, 2015 Share Posted February 14, 2015 You can do something like this with css <!DOCTYPE html> <html> <head> <style> .sorry { width:150px; height:150px; background-image:url("http://thumbs.dreamstime.com/x/sorry-sign-18772092.jpg"); background-color: #ffffff; background-size: 150px 150px; } </style> </head> <body> <img class="sorry" src="http://www.old-hall.com/uploads/images/osb/berberis-harlequin.jpg" /> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/294615-replace-image-where-none-displayed/#findComment-1505704 Share on other sites More sharing options...
QuickOldCar Posted February 14, 2015 Share Posted February 14, 2015 If you really want to connect to each image first to see if it exists can do this. getimagesize(); $imagelow = strtolower($image); $image_location = "http://www.old-hall.com/uploads/images/osb/".$imagelow."_lg.jpg"; $image_size=getimagesize($image_location); if(!is_array($image_size)){ $image_location = "http://www.old-hall.com/uploads/images/osb/sorry_lg.jpg"; } echo ' <div class="product"> <div style="float:left; width:170px"><img src="$image_location" width="150px" height="150px" /></div> <div style="float:right"><div style="margin-left:auto; margin-right:auto"><a href="http://www.1pw.co.uk/product_info.php?name='.$name.'"></div>'.$name.' - £'.$salesprice.'</a></div> </div> '; Quote Link to comment https://forums.phpfreaks.com/topic/294615-replace-image-where-none-displayed/#findComment-1505705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.