Skip to content

Instantly share code, notes, and snippets.

@csnover
Created February 21, 2011 01:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csnover/836506 to your computer and use it in GitHub Desktop.
Save csnover/836506 to your computer and use it in GitHub Desktop.
Gyazo script
  1. Change $path and $uri as necessary in gyazo.php and upload it to your server
  2. Change HOST and CGI as necessary in script and use it to replace Gyazo.app/Contents/Resources/script (right-click -> Show Package Contents to get there)
  3. Continue along as usual
<?php
// The local path in which images will be stored (change as neceesary).
$path = '/var/www/example.com/i/';
// The URI path at which images will be accessed (change as neceesary).
$uri = 'http://' . $_SERVER['HTTP_HOST'] . '/i/';
// Get binary image data from HTTP POST.
if(isset($_POST['imagedata']) && strlen($_POST['imagedata'])) {
$imagedata = $_POST['imagedata'];
}
elseif(isset($_FILES['imagedata']) && is_uploaded_file($_FILES['imagedata']['tmp_name'])) {
$imagedata = file_get_contents($_FILES['imagedata']['tmp_name']);
}
else {
error_log("No image data?");
return;
}
// Generate a unique filename.
do {
// $filename = substr( md5( $imagedata . $i++ ), -6 ) . '.png';
$filename = uniqid();
} while (file_exists("{$path}{$filename}.png") || file_exists("{$path}{$filename}.jpg"));
// Compress the image (destroying any alpha transparency).
$image = @imagecreatefromstring($imagedata);
if ($image !== false) {
$w = imagesx($image);
$h = imagesy($image);
imagealphablending($image, false);
imagesavealpha($image, true);
imagepng($image, "{$path}{$filename}.png", 9, PNG_ALL_FILTERS);
$jpeg = imagecreatetruecolor($w, $h);
$white = imagecolorallocate($jpeg, 255, 255, 255);
imagefill($jpeg, 0, 0, $white);
imagealphablending($jpeg, true);
imagesavealpha($jpeg, false);
imagecopy($jpeg, $image, 0, 0, 0, 0, $w, $h);
imagedestroy($image);
imagejpeg($jpeg, "{$path}{$filename}.jpg", 90);
imagedestroy($jpeg);
if (filesize("{$path}{$filename}.jpg") < filesize("{$path}{$filename}.png") * 0.9) {
unlink("{$path}{$filename}.png");
echo "{$uri}{$filename}.jpg";
}
else {
unlink("{$path}{$filename}.jpg");
echo "{$uri}{$filename}.png";
}
}
else {
error_log("image create failed");
}
#!/usr/bin/env ruby
require 'net/http'
# get id
user = IO.popen("whoami", "r+").gets.chomp
program = ARGV[0].to_s
idfile = "/Users/#{user}/Library/Gyazo/id"
old_idfile = File.dirname(program) + "/gyazo.app/Contents/Resources/id"
id = ''
if File.exist?(idfile) then
id = File.read(idfile).chomp
elsif File.exist?(old_idfile) then
id = File.read(old_idfile).chomp
end
# capture png file
tmpfile = "/tmp/image_upload#{$$}.png"
imagefile = ARGV[1]
if imagefile && File.exist?(imagefile) then
system "sips -s format png \"#{imagefile}\" --out \"#{tmpfile}\""
else
system "screencapture -i \"#{tmpfile}\""
if File.exist?(tmpfile) then
system "sips -d profile --deleteColorManagementProperties \"#{tmpfile}\""
end
end
if !File.exist?(tmpfile) then
exit
end
imagedata = File.read(tmpfile)
File.delete(tmpfile)
# upload
boundary = '----BOUNDARYBOUNDARY----'
HOST = 'example.com'
CGI = '/path/to/gyazo.php'
UA = 'Gyazo/1.0'
data = <<EOF
--#{boundary}\r
content-disposition: form-data; name="id"\r
\r
#{id}\r
--#{boundary}\r
Content-Disposition: form-data; name="imagedata"; filename="#{tmpfile}"\r
Content-Type: image/png\r
\r
#{imagedata}\r
--#{boundary}--\r
EOF
header ={
'Content-Length' => data.length.to_s,
'Content-Type' => "multipart/form-data; boundary=#{boundary}",
'User-Agent' => UA
}
Net::HTTP.start(HOST,80){|http|
res = http.post(CGI,data,header)
url = res.response.to_ary[1]
system "echo -n #{url} | pbcopy"
system "open #{url}"
# save id
newid = res.response['X-Gyazo-Id']
if newid and newid != "" then
if !File.exist?(File.dirname(idfile)) then
Dir.mkdir(File.dirname(idfile))
end
if File.exist?(idfile) then
File.rename(idfile, idfile+Time.new.strftime("_%Y%m%d%H%M%S.bak"))
end
File.open(idfile,"w").print(newid)
if File.exist?(old_idfile) then
File.delete(old_idfile)
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment