オレオレGyazo鯖構築


そもそもGyazoって何ですか

画面の一部を選択すると、切り取って自動的にgyazo.comにアップされ、画像を人と共有できます。スクリーン上のものはどんなコンテンツでも切り取れます。

Gyazo - Gyazoへようこそ : スクリーンショットの瞬間共有


必要なもの

クライアント側

gyazowin.cpp の 794-795行目を修正します。サーバアドレスは自分が使用するサーバ、ファイル名は upload.php とでもしておきます。

change

ビルド実行 成功すると gyazowin.exe が作成されます。

build

サーバ側

upload.php を作成します。
このスクリプトのこのまま使うのはセキュリティ上好ましくないのでBASIC認証等を設定した方がいいです。

<?php
/*
GyazoModoki Upload.php
This script accepts POST from anyone!!
You should be set .htpasswd
Edit your php.ini or .htaccess
e.g.
upload_max_filesize = 100M
post_max_size = 100M
Edit your .htaccess
image save directory : /foo/var/www/images/
http access sample : http://example.com/foo.png
e.g.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} ^(.*\.(jpg|png))$ [NC]
RewriteCond %{REQUEST_URI} !^/images/*.*$
RewriteRule ^(.*)$ /images/$1
*/
/*
The MIT License (MIT)
Copyright (c) 2014 k725
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
$path = dirname(__FILE__) . '/images/';
$uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
$length = 6;
$errimg = 'error.png';
if (!isset($_FILES['imagedata']['error']) || !is_int($_FILES['imagedata']['error']) || $_FILES['imagedata']['size'] < 1) {
echo $uri . $errimg;
exit;
}
for ($i = 0, $str = null; $i < $length; ) {
$num = mt_rand(0x30, 0x7A);
if ((0x30 <= $num && $num <= 0x39) || (0x41 <= $num && $num <= 0x5A) || (0x61 <= $num && $num <= 0x7A)) {
$str .= chr($num);
$i++;
}
}
$filename = $str . '.png';
$filepath = $path . $filename;
if (!isset($filepath) || file_exists($filepath) || !move_uploaded_file($_FILES['imagedata']['tmp_name'], $filepath)) {
echo $uri . $errimg;
} else {
$image = imagecreatefrompng($filepath);
imagealphablending($image, false);
imagesavealpha($image, true);
imagepng($image, $filepath, 9);
imagedestroy($image);
chmod($filepath, 0644);
echo $uri . $filename;
}
view raw upload.php hosted with ❤ by GitHub

編集し終わった upload.php をサーバにアップし、先ほどビルドしたクライアントでアップロードすれば多分成功しているはずです。

(追記 2014/03/02 21:36)
.htaccessでの制御のサンプルです
.htpasswdの作り方については各自ググってください(なげやり)
このサンプルだと cp.php でパスワードを要求します

AuthUserFile /foo/bar/.htpasswd
AuthName "Tyozo-Modoki"
AuthType Basic
<Files ~ "^cp\.php$">
	require valid-user
</Files>

example.com/example.png でアクセスさせ、内部的には example.com/images/example.png で処理させたい場合は以下のように設定します

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} ^(.*\.(jpg|png))$ [NC]
RewriteCond %{REQUEST_URI} !^/images/*.*$
RewriteRule ^(.*)$ /images/$1