upload.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Helper library for working with uploads
  4. */
  5. /**
  6. * Guess on the mimetype based on file extension
  7. *
  8. * @param string $originalName
  9. * @return string
  10. */
  11. function tp_upload_get_mimetype($originalName) {
  12. $extension = substr(strrchr($originalName, '.'), 1);
  13. switch (strtolower($extension)) {
  14. case 'png':
  15. return 'image/png';
  16. break;
  17. case 'gif':
  18. return 'image/gif';
  19. break;
  20. case 'jpg':
  21. case 'jpeg':
  22. return 'image/jpeg';
  23. break;
  24. default:
  25. return 'unknown';
  26. break;
  27. }
  28. }
  29. /**
  30. * Check if this is an image
  31. *
  32. * @param string $mime
  33. * @return bool false = not image
  34. */
  35. function tp_upload_check_format($mime) {
  36. $accepted_formats = array(
  37. 'image/jpeg',
  38. 'image/png',
  39. 'image/gif',
  40. 'image/pjpeg',
  41. 'image/x-png',
  42. );
  43. if (!in_array($mime, $accepted_formats)) {
  44. return false;
  45. }
  46. return true;
  47. }
  48. /**
  49. * Check if there is enough memory to process this image
  50. *
  51. * @param string $image_lib
  52. * @param int $requiredMemory
  53. * @return bool false = not enough memory
  54. */
  55. function tp_upload_memory_check($image_lib, $mem_required) {
  56. if ($image_lib !== 'GD') {
  57. return true;
  58. }
  59. $mem_avail = elgg_get_ini_setting_in_bytes('memory_limit');
  60. $mem_used = memory_get_usage();
  61. $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer
  62. if ($mem_required > $mem_avail) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. /**
  68. * Check if image is within limits
  69. *
  70. * @param int $image_size
  71. * @return bool false = too large
  72. */
  73. function tp_upload_check_max_size($image_size) {
  74. $max_file_size = (float) elgg_get_plugin_setting('maxfilesize', 'tidypics');
  75. if (!$max_file_size) {
  76. // default to 5 MB if not set
  77. $max_file_size = 5;
  78. }
  79. // convert to bytes from MBs
  80. $max_file_size = 1024 * 1024 * $max_file_size;
  81. return $image_size <= $max_file_size;
  82. }
  83. /**
  84. * Check if this image pushes user over quota
  85. *
  86. * @param int $image_size
  87. * @param int $owner_guid
  88. * @return bool false = exceed quota
  89. */
  90. function tp_upload_check_quota($image_size, $owner_guid) {
  91. static $quota;
  92. if (!isset($quota)) {
  93. $quota = elgg_get_plugin_setting('quota', 'tidypics');
  94. $quota = 1024 * 1024 * $quota;
  95. }
  96. if ($quota == 0) {
  97. // no quota
  98. return true;
  99. }
  100. $owner = get_entity($owner_guid);
  101. $image_repo_size = (int)$owner->image_repo_size;
  102. return ($image_repo_size + $image_size) < $quota;
  103. }