FBに記事をシェアしたけど画像が表示されてなかった。 [OGP](http://ogp.me/)の画像URLに、スマホのデカイ画像そのままあげたせいか? ということで、ImageMagickベースのサムネイル画像の生成ツールを仕込む。 ``` % npm install imagemagick ``` ※ RPM等でImageMagickを入れておく バックエンド側(NodeJS)はこんな感じ ``` var im = require( 'imagemagick' ); im.convert( [ srcPath, '-resize', maxWidth + 'x' + maxHeight + '>', destPath ], function( err, stdout, stderr ) { if ( err ) { // error... } else { res.download( destPath ); } } ); ``` セッションにメモリを使っていたせいで、バックエンドを再起動するたびログインし直す必要があり、デバッグが面倒になってきたので、永続化をRedisにて。 ``` app.use( session({ , saveUninitialized: true, resave: true }) ); ``` ↓ ``` redisStore = require( 'connect-redis' )( session ); app.use( session({ key: 'hogefuga', store: new redisStore({ host: '127.0.0.1', port: 6379, prefix: 'session' }), cookie: { path: '/', maxAge: 3600000 // 1h }, secret: 'hogefuga', saveUninitialized: true, resave: true }) ); ``` 一段落して、FBのデバッグツールで記事を解析させてみるが画像が出てこない。 > og:image was not defined, could not be downloaded or was not big enough. Please define a chosen image using the og:image metatag, and use an image that's at least 200x200px and is accessible from Facebook. Image 'http://b.st-hatena.com/images/entry-button/button-only@2x.png' will be used instead. と思ったら、バックエンドからファイルを返す時に、 ``` res.download( filePath ); ``` ではなく、こうしないといけなかった ``` var bin = fs.readFileSync( filePath ); res.end( bin, 'binary' ); ``` う、うんw 試行錯誤ついでに、Bootstrapっぽいパラメータで動作するようにした。 写真は3日に引いた伏見稲荷のおみくじ。 内容はさておき **1番**。でだしとしては、いんでないかい?
おみくじ#1
Posted on 2015/01/05 17:19
2015/01/05 17:19
Tomohiro Orikasa
views

Comments 0
コメントを投稿する