0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/data/meta/image-dimensions.js
Aileen Nowak e19e91044d 🙇 Blog icon utils and publisher.logo for JSON-LD (#8297)
refs #8221, closes #7688, refs #7558

🙇  Improve meta data publisher logo behaviour
This is a follow-up PR for #8285.

Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files.

This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions:
1. If we have a publication **logo**, we'll take that one
2. If we have no publication logo, but an **icon** we'll use this one.
3. If we have none of the above things, we fall back to our default `favicon.ico`

Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given.

I will create another follow-up PR, which checks the extension type for the file and offers it as a util.

🛠  Blog icon util

refs #7688

Serve functionality around the blog icon in its own util:
- getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions
- isIcoImageType -> returns true if file has `.ico` extension
- getIconType -> returns icon-type (`x-icon` or `png`)
- getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]`

📖  Get .ico sizes for meta data & logo improvement

refs #7558
refs #8221

Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files.

Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools.

- add getIconPath util
2017-04-11 18:32:06 +02:00

71 lines
3.1 KiB
JavaScript

var getCachedImageSizeFromUrl = require('../../utils/cached-image-size-from-url'),
Promise = require('bluebird'),
_ = require('lodash');
/**
* Get Image dimensions
* @param {object} metaData
* @returns {object} metaData
* @description for image properties in meta data (coverImage, authorImage and blog.logo), `getCachedImageSizeFromUrl` is
* called to receive image width and height
*/
function getImageDimensions(metaData) {
var fetch = {
coverImage: getCachedImageSizeFromUrl(metaData.coverImage.url),
authorImage: getCachedImageSizeFromUrl(metaData.authorImage.url),
// CASE: check if logo has hard coded image dimension. In that case it's an `ico` file, which
// is not supported by `image-size` and would produce an error
logo: metaData.blog.logo && metaData.blog.logo.dimensions ? metaData.blog.logo.dimensions : getCachedImageSizeFromUrl(metaData.blog.logo.url)
};
return Promise.props(fetch).then(function (resolve) {
var imageObj = {};
imageObj = {
coverImage: resolve.coverImage,
authorImage: resolve.authorImage,
logo: resolve.logo
};
_.forEach(imageObj, function (key, value) {
if (_.has(key, 'width') && _.has(key, 'height')) {
// We have some restrictions for publisher.logo:
// The image needs to be <=600px wide and <=60px high (ideally exactly 600px x 60px).
// Unless we have proper image-handling (see https://github.com/TryGhost/Ghost/issues/4453),
// we will fake it in some cases or not produce an imageObject at all.
if (value === 'logo') {
if (key.height <= 60 && key.width <= 600) {
_.assign(metaData.blog[value], {
dimensions: {
width: key.width,
height: key.height
}
});
} else if ((metaData.blog.logo && metaData.blog.logo.dimensions) || key.width === key.height) {
// CASES:
// 1. .ico files have image dimensions assigned already. If they're not
// within the requirements of Google, we fake them...
// 2. the logo (non-ico) is too large, but it is a square. We fake it as well...
_.assign(metaData.blog[value], {
dimensions: {
width: 60,
height: 60
}
});
}
} else {
_.assign(metaData[value], {
dimensions: {
width: key.width,
height: key.height
}
});
}
}
});
return metaData;
});
}
module.exports = getImageDimensions;