/wp-content/themes/rolopress-core/library/includes/upload.php

https://github.com/nunomorgadinho/SimplePhone · PHP · 145 lines · 101 code · 14 blank · 30 comment · 24 complexity · c109ec9852b9c79e30b16ef5f3f74cb1 MD5 · raw file

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