- Change
$path
and$uri
as necessary ingyazo.php
and upload it to your server - Change
HOST
andCGI
as necessary inscript
and use it to replaceGyazo.app/Contents/Resources/script
(right-click -> Show Package Contents to get there) - Continue along as usual
Created
February 21, 2011 01:11
-
-
Save csnover/836506 to your computer and use it in GitHub Desktop.
Gyazo script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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