
Originally Posted by
Goetch
Here is my current facebook image code...
Code:
<?php
if (isset($_GET['products_id'])) { // use products_image if products_id exists
$facebook_image = $db->Execute("select p.products_image from " . TABLE_PRODUCTS . " p where products_id='" . (int)$_GET['products_id'] . "'");
$fb_image = HTTP_SERVER . DIR_WS_CATALOG . DIR_WS_IMAGES . $facebook_image->fields['products_image'];
}
if ($fb_image == '') { // if no products image, use the default image if enabled
$fb_image = 'http://www.store.com/includes/templates/scb/images/logo.gif';
}
if ($fb_image != '') {
?>
<meta property="og:image" content="<?php echo $fb_image; ?>" />
I'd be inclined to replace all that with this instead:
Code:
<?php
// default image:
$fb_image = HTTP_SERVER . DIR_WS_CATALOG . 'includes/templates/scb/images/logo.gif';
if (isset($_GET['products_id'])) { // use products_image if products_id exists
$facebook_image = $db->Execute("select p.products_image from " . TABLE_PRODUCTS . " p where products_id='" . (int)$_GET['products_id'] . "'");
$prod_image = $facebook_image->fields['products_image'];
$prod_image_extension = substr($prod_image, strrpos($prod_image, '.'));
$prod_image_base = str_replace($prod_image_extension, '', $prod_image);
$prod_image_large = $prod_image_base . IMAGE_SUFFIX_LARGE . $prod_image_extension;
if (file_exists(DIR_WS_IMAGES . 'large/' . $prod_image_large)) {
$prod_image = DIR_WS_IMAGES . 'large/' . $prod_image_large;
}
$fb_image = HTTP_SERVER . DIR_WS_CATALOG . $prod_image;
}
if ($fb_image != '') {
?>
<meta property="og:image" content="<?php echo $fb_image; ?>" />
<?php } ?>
First, it sets a default up-front, which makes things simpler later.
Then it reads the image from the database, parses it into its parts, inserts the "lrg" bit, checks if the file exists, and if it does, then it replaces the value set as default earlier.
Then it carries on to output it as you were doing before.
Bookmarks