install cutycapt

安装完后会有/usr/bin/CutyCapt和/usr/bin/xvfb-run

# yum install -y epel-release
# yum install -y CutyCapt

example

$ xvfb-run --server-args="-screen 0, 1024x768x24" ./CutyCapt --url='http://baidu.com' --out=/tmp/baidu.png

报错处理

$ /usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" /usr/bin/CutyCapt --url='http://baidu.com' --out=/tmp/a.png
process 10606: D-Bus library appears to be incorrectly set up; failed to read machine uuid: Failed to open "/var/lib/dbus/machine-id": No such file or directory
See the manual page for dbus-uuidgen to correct this issue.
  D-Bus not built with -rdynamic so unable to print a backtrace
/usr/bin/xvfb-run: line 181: 10606 Aborted                 DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1

使用命令生成machine-id即可

dbus-uuidgen > /var/lib/dbus/machine-id

常见问题

xvfb相关问题汇总 CutyCapt官网 dbus生成machine-id

shell示例

#!/bin/sh
. ~/.bash_profile
function encodeurl()
{
    encoded_str=`echo "$*" | awk 'BEGIN {
        split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
        hextab [0] = 0
        for (i=1; i<=255; ++i) {
            ord [ sprintf ("%c", i) "" ] = i + 0
        }
    }
    {
        encoded = ""
        for (i=1; i<=length($0); ++i) {
            c = substr ($0, i, 1)
            if ( c ~ /[a-zA-Z0-9.-]/ ) {
                encoded = encoded c             # safe character
            } else if ( c == " " ) {
                encoded = encoded "+"   # special handling
            } else {
                # unsafe character, encode it as a two-digit hex-number
                lo = ord [c] % 16
                hi = int (ord [c] / 16);
                encoded = encoded "%" hextab [hi] hextab [lo]
            }
        }
        print encoded
    }' 2>/dev/null`
}

#-----------main-----------------
if [ $# -ne 3 ]; then
    echo "Usage: $0 <vod_url> v/h job_id vs_no"
    exit 1
fi

cap_bin=/usr/bin/CutyCapt
cap_dir=/ebs_data/opt/typhoonae/var/temp/web2png
job=$3
vs_no=$4
delay_tm=1000
if [ $2 = "v" ]; then
sc_width=720
sc_height=1280
else
sc_width=1280
sc_height=720
fi
if [ -n "$job" ]; then
    echo -n "..."
else
    job="1101"
fi

encodeurl "$1"
#vod_url="${encoded_str}"
vod_url=$1
echo "${cap_bin} --url='${vod_url}' --min-width=${sc_width} --min-height=${sc_height} --delay=${delay_tm} --max-wait=5000 --out=${cap_dir}/snap_screen_${job}.png"
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" $cap_bin --url="${vod_url}" --min-width=${sc_width} --min-height=${sc_height} --delay=${delay_tm} --max-wait=5000 --out=${cap_dir}/snap_screen_${job}.png
echo "Result: ${cap_dir}/snap_screen_${job}.png"