/upload.php

https://github.com/mmeester/droparea · PHP · 124 lines · 94 code · 11 blank · 19 comment · 23 complexity · 694d407d26b20824f98b17461ac4f0d9 MD5 · raw file

  1. <?php
  2. // Maximum file size
  3. $maxsize = 1024; //Kb
  4. // Supporting image file types
  5. $types = Array('image/png', 'image/gif', 'image/jpeg');
  6. $headers = getallheaders();
  7. // LOG
  8. $log = '=== ' . @date('Y-m-d H:i:s') . ' ===============================' . "\n"
  9. . 'HEADERS:' . print_r($headers, 1) . "\n";
  10. $fp = fopen('log.txt', 'a');
  11. fwrite($fp, $log);
  12. fclose($fp);
  13. // Result object
  14. $r = new stdClass();
  15. // Result content type
  16. header('content-type: application/json');
  17. // File size control
  18. if ($headers['x-file-size'] > ($maxsize * 1024)) {
  19. $r->error = "Max file size: $maxsize Kb";
  20. }
  21. $folder = $headers['x-param-folder'] ? $headers['x-param-folder'] . '/' : '';
  22. if ($folder && !is_dir($folder))
  23. mkdir($folder);
  24. // File type control
  25. if (in_array($headers['x-file-type'], $types)) {
  26. // Create an unique file name
  27. if ($headers['x-param-value']) {
  28. $filename = $folder . $headers['x-param-value'];
  29. } else {
  30. $filename = $folder . sha1(@date('U') . '-' . $headers['x-file-name'])
  31. . '.' . $headers['x-param-type'];
  32. }
  33. // Uploaded file source
  34. $source = file_get_contents($_FILES["x-file-name"]["tmp_name"]);
  35. // Image resize
  36. imageresize($source, $filename,
  37. $headers['x-param-width'],
  38. $headers['x-param-height'],
  39. $headers['x-param-crop'],
  40. $headers['x-param-quality']);
  41. } else
  42. $r->error = "Unsupported file type: " . $headers['x-file-type'];
  43. // File path
  44. $path = str_replace('upload.php', '', $_SERVER['SCRIPT_NAME']);
  45. // Image tag
  46. $r->filename = $filename;
  47. $r->path = $path;
  48. $r->img = '<img src="' . $path . $filename . '" alt="image" />';
  49. echo json_encode($r);
  50. // Image resize function with php + gd2 lib
  51. function imageresize($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 80) {
  52. $quality = $quality ? $quality : 80;
  53. $image = imagecreatefromstring($source);
  54. if ($image) {
  55. // Get dimensions
  56. $w = imagesx($image);
  57. $h = imagesy($image);
  58. if (($width && $w > $width) || ($height && $h > $height)) {
  59. $ratio = $w / $h;
  60. if (($ratio >= 1 || $height == 0) && $width && !$crop) {
  61. $new_height = $width / $ratio;
  62. $new_width = $width;
  63. } elseif ($crop && $ratio <= ($width / $height)) {
  64. $new_height = $width / $ratio;
  65. $new_width = $width;
  66. } else {
  67. $new_width = $height * $ratio;
  68. $new_height = $height;
  69. }
  70. } else {
  71. $new_width = $w;
  72. $new_height = $h;
  73. }
  74. $x_mid = $new_width * .5; //horizontal middle
  75. $y_mid = $new_height * .5; //vertical middle
  76. // Resample
  77. error_log('height: '.$new_height.' - width: '.$new_width);
  78. $new = imagecreatetruecolor(round($new_width), round($new_height));
  79. imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
  80. // Crop
  81. if ($crop) {
  82. $crop = imagecreatetruecolor($width ? $width : $new_width, $height ? $height : $new_height);
  83. imagecopyresampled($crop, $new, 0, 0, ($x_mid - ($width * .5)), 0, $width, $height, $width, $height);
  84. //($y_mid - ($height * .5))
  85. }
  86. // Output
  87. // Enable interlancing [for progressive JPEG]
  88. imageinterlace($crop ? $crop : $new, true);
  89. $dext = strtolower(pathinfo($destination, PATHINFO_EXTENSION));
  90. if ($dext == '') {
  91. $dext = $ext;
  92. $destination .= '.' . $ext;
  93. }
  94. switch ($dext) {
  95. case 'jpeg':
  96. case 'jpg':
  97. imagejpeg($crop ? $crop : $new, $destination, $quality);
  98. break;
  99. case 'png':
  100. $pngQuality = ($quality - 100) / 11.111111;
  101. $pngQuality = round(abs($pngQuality));
  102. imagepng($crop ? $crop : $new, $destination, $pngQuality);
  103. break;
  104. case 'gif':
  105. imagegif($crop ? $crop : $new, $destination);
  106. break;
  107. }
  108. @imagedestroy($image);
  109. @imagedestroy($new);
  110. @imagedestroy($crop);
  111. }
  112. }
  113. ?>