Advertisement
kevin_neven

Untitled

Jun 20th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. <?php
  2. class Image{
  3.  
  4. public static function create_thumbnail($path, $save, $width, $height) {
  5. $info = getimagesize($path);
  6. $size = array($info[0], $info[1]);
  7.  
  8. switch ($info["mime"]) {
  9. case 'image/png':
  10. $src = imagecreatefrompng($path);
  11. break;
  12. case 'image/jpeg':
  13. $src = imagecreatefromjpeg($path);
  14. break;
  15. case 'image/gif':
  16. $src = imagecreatefromgif($path);
  17. break;
  18. default:
  19. return false;
  20. break;
  21. }
  22.  
  23. $thumb = imagecreatetruecolor($width, $height);
  24.  
  25. $src_aspect = $size[0] / $size[1];
  26. $thumb_aspect = $width / $height;
  27.  
  28. if ($src_aspect < $thumb_aspect){
  29. //narrower
  30. $scale = $width / $size[0];
  31. $new_size = array($width, $width / $src_aspect);
  32. $src_pos = array(0, ($size[1] * $scale - $height) / $scale / 2);
  33. }else if ($src_aspect > $thumb_aspect){
  34. // wider
  35. $scale = $height / $size[1];
  36. $new_size = array($height * $src_aspect, $height);
  37. $src_pos = array(($size[0] * $scale - $width) / $scale / 2, 0);
  38. }else{
  39. //same shape
  40. $new_size = array($width, $height);
  41. $src_pos = array(0, 0);
  42. }
  43.  
  44. $new_size[0] = max($new_size[0], 1);
  45. $new_size[1] = max($new_size[1], 1);
  46.  
  47. imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $size[0], $size[1]);
  48.  
  49. if ($save ===false){//debug mode
  50. switch ($info["mime"]) {
  51. case 'image/png':
  52. return imagepng($thumb);
  53. break;
  54. case 'image/jpeg':
  55. return imagejpeg($thumb);
  56. break;
  57. case 'image/gif':
  58. return imagegif($thumb);
  59. break;
  60. }
  61. }else{//save actual images
  62. switch ($info["mime"]) {
  63. case 'image/png':
  64. return imagepng($thumb, $save);
  65. break;
  66. case 'image/jpeg':
  67. return imagejpeg($thumb, $save);
  68. break;
  69. case 'image/gif':
  70. return imagegif($thumb, $save);
  71. break;
  72. }
  73. }
  74. //kevin neven 09-06-2016
  75. }
  76. public static function upload($image, $path, $thumbnail = FALSE) {
  77. return fileManagement::upload($image, 5000, $path, array("png", "jpeg", "jpg", "gif"));
  78. }
  79. }
  80.  
  81.  
  82. <?php
  83. class fileManagement {
  84. public static function upload($a_file, $maxKB, $path, $allowedExt) {
  85. $path = rtrim($path,"/");
  86. $path .= "/";
  87. $file = $a_file;
  88.  
  89. //file properties
  90. $fileName = $file["name"];
  91. $fileTmp = $file["tmp_name"];
  92. $fileSize = $file["size"];
  93. $fileError = $file["error"];
  94.  
  95. //get the file extension
  96. $fileExt = explode(".", $fileName);
  97. $fileExt = strtolower(end($fileExt));
  98.  
  99.  
  100. if(in_array($fileExt, $allowedExt)){
  101.  
  102. if($fileError === 0){
  103. if($fileSize <= $maxKB * 1024){ //check if the filesize is smaller or equal to the max size
  104.  
  105. $fileNameAndPath = self::generateRandomFileName($fileExt,$path);
  106.  
  107. $errorAmount = 0;
  108.  
  109. while (file_exists($fileNameAndPath)){ //
  110. $fileNameAndPath = self::generateRandomFileName($fileExt,$path);
  111. $errorx++;
  112. if ($errorAmount == 5) {
  113. return "error";
  114. }
  115. }
  116.  
  117. if(move_uploaded_file($fileTmp, $fileNameAndPath)){
  118. return $fileNameAndPath;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. private static function generateRandomFileName($fileExtion, $path) {
  125. $filename = uniqid("",TRUE) . "." . $fileExtion; // make the file name... uniqid() returns a random string. so you would get 32y243hqtkglrhjlag.jpg
  126. return $path . $filename;
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement