From d1349d9046a25adecde058351dae1ae1d6042131 Mon Sep 17 00:00:00 2001 From: Marvin Herbold Date: Fri, 6 Nov 2015 10:09:17 -0700 Subject: [PATCH] Initial commit. --- config.php | 13 +++++ dl/index.php | 48 +++++++++++++++++ editor.html | 56 ++++++++++++++++++++ img/index.php | 131 +++++++++++++++++++++++++++++++++++++++++++++++ upload/index.php | 73 ++++++++++++++++++++++++++ 5 files changed, 321 insertions(+) create mode 100644 config.php create mode 100644 dl/index.php create mode 100644 editor.html create mode 100644 img/index.php create mode 100644 upload/index.php diff --git a/config.php b/config.php new file mode 100644 index 0000000..ab12202 --- /dev/null +++ b/config.php @@ -0,0 +1,13 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/img/index.php b/img/index.php new file mode 100644 index 0000000..b2fe93e --- /dev/null +++ b/img/index.php @@ -0,0 +1,131 @@ +newImage( $width, $height, "#707070" ); + + $image->setImageFormat( "png" ); + + $x = 0; + $y = 0; + $size = 40; + + $draw = new ImagickDraw(); + + while ( $y < $height ) + { + $draw->setFillColor( "#808080" ); + + $points = [ + [ "x" => $x, "y" => $y ], + [ "x" => $x + $size, "y" => $y ], + [ "x" => $x + $size * 2, "y" => $y + $size ], + [ "x" => $x + $size * 2, "y" => $y + $size * 2 ] + ]; + + $draw->polygon( $points ); + + $points = [ + [ "x" => $x, "y" => $y + $size ], + [ "x" => $x + $size, "y" => $y + $size * 2 ], + [ "x" => $x, "y" => $y + $size * 2 ] + ]; + + $draw->polygon( $points ); + + $x += $size * 2; + + if ( $x > $width ) + { + $x = 0; + $y += $size * 2; + } + } + + $draw->setFillColor( "#B0B0B0" ); + $draw->setFontSize( 20 ); + $draw->setGravity( Imagick::GRAVITY_CENTER ); + $draw->annotation( 0, 0, $width . " x " . $height ); + + $image->drawImage( $draw ); + + header( "Content-type: image/png" ); + + echo $image; + } + else + { + $file_name = $_GET[ "src" ]; + + $path_parts = pathinfo( $file_name ); + + switch ( $path_parts[ "extension" ] ) + { + case "png": + $mime_type = "image/png"; + break; + + case "gif": + $mime_type = "image/gif"; + break; + + default: + $mime_type = "image/jpeg"; + break; + } + + $file_name = $path_parts[ "basename" ]; + + $image = new Imagick( realpath( $uploads_dir . $file_name ) ); + + if ( $_GET[ "method" ] == "resize" ) + { + $image->resizeImage( $width, $height, Imagick::FILTER_LANCZOS, 0 ); + } + else // $_GET[ "method" ] == "cover" + { + $image_geometry = $image->getImageGeometry(); + + $width_ratio = $image_geometry[ "width" ] / $width; + $height_ratio = $image_geometry[ "height" ] / $height; + + $resize_width = $width; + $resize_height = $height; + + if ( $width_ratio > $height_ratio ) + { + $resize_width = 0; + } + else + { + $resize_height = 0; + } + + $image->resizeImage( $resize_width, $resize_height, Imagick::FILTER_LANCZOS, 0 ); + + $image_geometry = $image->getImageGeometry(); + + $x = ( $image_geometry[ "width" ] - $width ) / 2; + $y = ( $image_geometry[ "height" ] - $height ) / 2; + + $image->cropImage( $width, $height, $x, $y ); + } + + header( "Content-type: " . $mime_type ); + + echo $image; + } +} diff --git a/upload/index.php b/upload/index.php new file mode 100644 index 0000000..1efa4c0 --- /dev/null +++ b/upload/index.php @@ -0,0 +1,73 @@ + $file_name, + "url" => $uploads_url . $file_name, + "size" => filesize( $uploads_dir . $file_name ) + ); + + if ( file_exists( realpath( $thumbnails_dir . $file_name ) ) ) + { + $file[ "thumbnailUrl" ] = $thumbnails_url . $file_name; + } + + $files[] = $file; + } + } + + header( "Content-Type: application/json; charset=utf-8" ); + header( "Connection: close" ); + + echo json_encode( array( "files" => $files ) ); +} +else if ( !empty( $_FILES ) ) +{ + $files = array(); + + foreach ( $_FILES[ "files" ][ "error" ] as $key => $error ) + { + if ( $error == UPLOAD_ERR_OK ) + { + $tmp_name = $_FILES[ "files" ][ "tmp_name" ][ $key ]; + + $file_name = $_FILES[ "files" ][ "name" ][ $key ]; + + if ( move_uploaded_file( $tmp_name, $uploads_dir . $file_name ) === TRUE ) + { + $image = new Imagick( realpath( $uploads_dir . $file_name ) ); + + $image->resizeImage( $thumbnail_width, $thumbnail_height, Imagick::FILTER_LANCZOS, 0, TRUE ); + $image->writeImage( realpath( $thumbnails_dir ) . "/". $file_name ); + $image->destroy(); + + $file = array( + "name" => $file_name, + "url" => $uploads_url . $file_name, + "size" => filesize( $uploads_dir . $file_name ), + "thumbnailUrl" => $thumbnails_url . $file_name + ); + + $files[] = $file; + } + } + } + + header( "Content-Type: application/json; charset=utf-8" ); + header( "Connection: close" ); + + echo json_encode( array( "files" => $files ) ); +}