LoginSignup
1
3

More than 5 years have passed since last update.

PhantomJSでratina用2倍画像をキャプる

Posted at

phantomJSでキャプチャすると通常1倍の画像サイズとなるが
macで作業していると気になるしサイト作成時困るので、もともと2倍で書き出せるようにした。

sample.js
var page = require('webpage').create();
page.viewportSize = {
    //画像サイズは2倍で設定しておく
    width: 2800,
    height: 1800
};
page.open('https://google.co.jp', function(status) {
  if(status === "success") {
    page.evaluate(function () {
            //ここで2倍にする
      document.body.style.webkitTransform = "scale(2)";
           //画像の座標を0%にしておかないとこの次に2/1にしたときにずれる
      document.body.style.webkitTransformOrigin = "0% 0%";
      //画像サイズにもとづいて2倍にされるので、全体を半分の2/1にする
      document.body.style.width = "50%";
      //背景を白くしておかないとpngで書き出すと透明になる
      document.body.bgColor = 'white';
    });
    window.setTimeout(function() {
      page.render('image.png');
      phantom.exit();
    }, 8000);
  }
});

キャプチャ系はwkhtmltoimageよりも使える印象ではあるが、やはりサイトによってはsetTimeoutの時間が10秒くらいあってもうまく表示されたりされなかったり。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3