2011年12月12日 星期一

透過ksnapshot直接上傳圖片到imgur.com

imgur.com是我很喜歡的一個圖床網站,
ksnapshot也是在kde一個非常優異的截圖軟體,
如果你的linux有裝ksnapshot也有裝kipi-plugins,
當按你下print screen之後,
你會發現ksnapshot的「傳送到」功能裏面有很多網站可以選取,
有flickr、picasa、facebook,但是唯獨就是少了我常用的imgur,

於是我找到一個由Bart Nagel所寫的script來進行修改,原本他的script只能在terminal下對圖檔進行傳送,不過因為我要跟ksnapshot做結合,所以有些在文字介面底下才有的特性被我做了一些更動,這讓我在截圖時會更加方便好用。
下面的script相依於 curl、 notify-send 、xsel or xclip
原始碼如下
#!/bin/bash

# imgur script by Bart Nagel
# version 2
# I release this as public domain. Do with it what you will.

# Required: curl
#
# Optional: notify-send xsel or xclip for automatically putting the URL on the X selection
# for easy pasting
#
# Instructions:
# Put it somewhere in your path and maybe rename it:
# mv ~/Downloads/imgur.sh ~/bin/imgur
# Make it executable:
# chmod +x ~/bin/imgur
# Stick your API key in the top:
# vim ~/bin/imgur
# Upload an image:
# imgur images/hilarious/manfallingover.jpg
# The URL will be displayed (and the delete page's URL will be displayed on
# stderr). If you have xsel or xclip the URL will also be put on the X
# selection, which you can usually paste with a middle click.

# API Key provided by Alan@imgur.com
#apikey="b3625162d3418ac51a9ee805b1840452"

# API Key provided by Max Wu
apikey="0c5996e47ef509f3f31b32e2d2934d01"

# function to output usage instructions
function usage {
echo "Usage: $(basename $0)
Upload an image to imgur and output its new URL to stdout. Its delete page is
output to stderr.
If xsel or xclip is available, the URL is put on the X selection for easy
pasting." >&2
}

# check API key has been entered
if [ "$apikey" = "Your API key" ]; then
echo "You first need to edit the script and put your API key in the variable near the top." >&2
exit 15
fi

# check arguments
if [ "$1" = "-h" -o "$1" = "--help" ]; then
usage
exit 0
elif [ $# -ne 1 ]; then
if [ $# == 0 ]; then
echo "No file specified" >&2
else
echo "Unexpected arguments" >&2
fi
usage
exit 16
elif [ ! -f "$1" ]; then
echo "File \"$1\" not found" >&2
exit 1
fi

# check curl is available
which curl >/dev/null 2>/dev/null || {
echo "Couln't find curl, which is required." >&2
exit 17
}

# upload the image
response=$(curl -F "key=$apikey" -H "Expect: " -F "image=@$1" \
http://imgur.com/api/upload.xml 2>/dev/null)
# the "Expect: " header is to get around a problem when using this through the
# Squid proxy. Not sure if it's a Squid bug or what.
if [ $? -ne 0 ]; then
echo "Upload failed" >&2
exit 2
elif [ $(echo $response | grep -c "") -gt 0 ]; then
echo "Error message from imgur:" >&2
echo $response | sed -r 's/.*(.*)<\/error_msg>.*/\1/' >&2
exit 3
fi

# parse the response and output our stuff
url=$(echo $response | sed -r 's/.*(.*)<\/original_image>.*/\1/')
deleteurl=$(echo $response | sed -r 's/.*(.*)<\/delete_page>.*/\1/')
echo $url
echo "Delete page: $deleteurl" >&2

# put the URL on the clipboard if we have xsel or xclip
if [ $DISPLAY ]; then
{ which xsel >/dev/null 2>/dev/null && echo -n $url | xsel; } \
|| { which xclip >/dev/null 2>/dev/null && echo -n $url | xclip -selection c;notify-send "Done"; } \
|| notify-send "Haven't copied to the clipboard: no xsel or xclip" >&2
else
notify-send "Haven't copied to the clipboard: no \$DISPLAY" >&2
fi


你可以把原始碼複製回去,也可以點這裡下載
既然script都寫好了,那麼該怎麼把script弄上ksnapshot上的「傳送到」(send to)選單上呢?
原本我怎麼樣都找不到相關的文檔,甚至還去找了source code來看,無奈功力太差看不懂,只好去kubuntu的論壇求救了,結果,沒想到方法出奇的簡單,我真是想太多了,
打開「系統設定」點選「檔案關聯」,找到image,再找到png
按下右邊的「增加」,
將你放置script的位置給輸入進去
按下確定後,他就會出現在「應用程式優先順序」中的第一個,
你可以點他按「編輯」來進行一些編修,
例如更改圖示(在壓縮檔中有imgur的圖示),
或者修改他的名稱、描述
修改完以後記得按下確定、套用,
接下來按下print screen的按鍵來試試看,
然後再按下「傳送到」,應該就會出現如下的選單,
看到第6個的選項了嗎?imgur出現了!
點下去之後,他就會把目前的截圖送到imgur的網站上,
傳送完成後會出現一個「Done」的字樣,
並且此時你只要按下「貼上」或者「ctrl+v」他就會貼上該圖的網址,
真的是超讚的!!
這個script的相依套件有curl,notify-send,xclip,如果有缺的就apt-get一下就好了,
ksnapshot不只可以擷取全螢幕也可以做區域選取,或者是當前視窗的截圖,真的是很棒的截圖軟體。
補充一下,因為是針對png圖檔進行檔案關聯的關係,所以在dolphin下也可以使用這個功能喔!
 直接針對圖檔也可以直接上傳,讚!!!

參考來源: