Skip to content

Instantly share code, notes, and snippets.

@danheberden
Forked from cowboy/gyazo.php
Created February 17, 2011 06:20
Show Gist options
  • Save danheberden/831139 to your computer and use it in GitHub Desktop.
Save danheberden/831139 to your computer and use it in GitHub Desktop.
<?PHP
/*
* PHP upload for Gyazo - v1.2 - 02/16/2011
* http://benalman.com/news/2009/10/gyazo-on-your-own-server/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*/
// The local path in which images will be stored (change as neceesary).
$path = '/srv/www/gyazo/'; // you can also use dirname( __FILE__ ) . "/relative/to/this/script/";
// The URI path at which images will be accessed (change as neceesary).
$uri = 'http://' . $_SERVER['HTTP_HOST'] . '/grab/';
// "imagedata" can be adjusted in the form-data name attr in gyazo's script configuration file.
// if it's non-existant or no size, then bail
if ( !isset( $_FILES['imagedata']['error'] ) || $_FILES['imagedata']['size'] < 1 ) {
echo $uri, 'invalid.png';
exit;
}
// Generate a unique filename.
$i = 0;
do {
// change -rand(4,7) to a static negative number if you want filenames of a certain length
$filename = substr( md5( time() . $i++ ), -rand(4,7) ) . '.png';
} while ( file_exists( "$path$filename" ) );
// move the file - if moving the file breaks, bail
if ( !move_uploaded_file( $_FILES['imagedata']['tmp_name'], "$path$filename" ) ) {
echo $uri, 'error.png';
exit;
}
// Compress the image (destroying any alpha transparency).
$image = @imagecreatefrompng( "$path$filename" );
imagepng( $image, "$path$filename", 9 );
imagedestroy( $image );
// Return the image URI.
echo $uri, $filename;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment