PageRenderTime 65ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/children-hugs/util/util_upload.php

https://github.com/praveenselvam/Missing-Children-Finder
PHP | 69 lines | 45 code | 18 blank | 6 comment | 2 complexity | 5475f2ced4e1cbee0bbef6a3488b41dd MD5 | raw file
  1. <?php
  2. require_once("util/util_image.php");
  3. require_once("config/app_config.php");
  4. class Upload {
  5. private $fileperm = 0644;
  6. private $dirperm = 0777;
  7. private function create_if_not_exists($target_path,$dirpem)
  8. {
  9. if(!is_dir($target_path))
  10. {
  11. mkdir($target_path,$dirperm);
  12. }
  13. }
  14. public function upload_photo($photo_id)
  15. {
  16. if (is_uploaded_file($_FILES['child_photo']['tmp_name'])) {
  17. $target_path = $_SERVER['DOCUMENT_ROOT'].ApplicationConfig::$IMAGE_FOLDER_PATH;
  18. $this->create_if_not_exists($target_path, $this->dirperm);
  19. $orig_file = $target_path."full_".$photo_id.strtolower(
  20. strrchr(basename(($_FILES["child_photo"]["name"])),".")
  21. );
  22. $resized_file = $target_path."rz_".$photo_id.strtolower(
  23. strrchr(basename(($_FILES["child_photo"]["name"])),".")
  24. );
  25. $thumbnail_file = $target_path."tn_".$photo_id.strtolower(
  26. strrchr(basename(($_FILES["child_photo"]["name"])),".")
  27. );
  28. $resized_file_name = $photo_id.strtolower(
  29. strrchr(basename(($_FILES["child_photo"]["name"])),".")
  30. );
  31. move_uploaded_file($_FILES["child_photo"]["tmp_name"], $orig_file);
  32. /***
  33. * TODO: Write resizing functions.
  34. * - Allowed file extensions
  35. * - Write validations for file size checks.
  36. * - handle image lib missing.
  37. */
  38. $image = new SimpleImage();
  39. $image->load($orig_file);
  40. $image->resizeToWidth(ApplicationConfig::$RESIZE_IMAGE_WIDTH);
  41. $image->save($resized_file);
  42. $image->load($orig_file);
  43. $image->resizeToWidth(ApplicationConfig::$RESIZE_TN_IMAGE_WIDTH);
  44. $image->save($thumbnail_file);
  45. return $resized_file_name;
  46. }else{
  47. return null;
  48. }
  49. }
  50. }
  51. ?>