Advertisement
Guest User

image upload

a guest
Oct 5th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. function insert() {
  2.  
  3. $data = array();
  4.  
  5. /* Uploading Profile Images */
  6. $imagePath = realpath(APPPATH . '../assets/images/');
  7. $logo = $_FILES['logo']['tmp_name'];
  8. $favicon = $_FILES['favicon']['tmp_name'];
  9. if ($logo !== "") {
  10. $config['upload_path'] = $imagePath;
  11. $config['allowed_types'] = 'jpg|png|jpeg|gif';
  12. $config['file_name'] = date('Ymd_his_') . rand(10, 99) . rand(10, 99) . rand(10, 99);
  13. $this->load->library('upload', $config);
  14. if ($this->upload->do_upload('logo')) {
  15. $uploadData = $this->upload->data();
  16. $data['logo'] = $uploadData['file_name'];
  17. }
  18.  
  19. $config['image_library'] = 'gd2';
  20. $config['source_image'] = $uploadData['full_path'];
  21. $config['new_image'] = $imagePath . '/crop';
  22. $config['quality'] = '100%';
  23. $config['maintain_ratio'] = FALSE;
  24. //Set cropping for y or x axis, depending on image orientation
  25. if ($uploadData['image_width'] > $uploadData['image_height']) {
  26. $config['width'] = $uploadData['image_height'];
  27. $config['height'] = $uploadData['image_height'];
  28. $config['x_axis'] = (($uploadData['image_width'] / 2) - ($config['width'] / 2));
  29. } else {
  30. $config['height'] = $uploadData['image_width'];
  31. $config['width'] = $uploadData['image_width'];
  32. $config['y_axis'] = (($uploadData['image_height'] / 2) - ($config['height'] / 2));
  33. }
  34.  
  35. $this->image_lib->clear();
  36. $this->image_lib->initialize($config);
  37. $this->image_lib->crop();
  38.  
  39. $config['source_image'] = $imagePath . '/crop/' . $uploadData['file_name'];
  40. $config['new_image'] = $imagePath . '/final';
  41. $config['maintain_ratio'] = FALSE;
  42. $config['width'] = 250;
  43. $config['height'] = 250;
  44.  
  45. $this->image_lib->clear();
  46. $this->image_lib->initialize($config);
  47. $this->image_lib->resize();
  48.  
  49. /* Deleting Uploaded Image After Croping and Resizing */
  50. /* Why Deleting because it's saving space */
  51. unlink($uploadData['full_path']);
  52. }
  53.  
  54.  
  55. if ($favicon !== "") {
  56. $config['upload_path'] = $imagePath;
  57. $config['allowed_types'] = 'jpg|png|jpeg|gif';
  58. $config['file_name'] = date('Ymd_his_') . rand(10, 99) . rand(10, 99) . rand(10, 99);
  59. $this->load->library('upload', $config);
  60. if ($this->upload->do_upload('favicon')) {
  61. $uploadData = $this->upload->data();
  62. $data['favicon'] = $uploadData['file_name'];
  63. }
  64.  
  65. $config['image_library'] = 'gd2';
  66. $config['source_image'] = $uploadData['full_path'];
  67. $config['new_image'] = $imagePath . '/crop';
  68. $config['quality'] = '100%';
  69. $config['maintain_ratio'] = FALSE;
  70. //Set cropping for y or x axis, depending on image orientation
  71. if ($uploadData['image_width'] > $uploadData['image_height']) {
  72. $config['width'] = $uploadData['image_height'];
  73. $config['height'] = $uploadData['image_height'];
  74. $config['x_axis'] = (($uploadData['image_width'] / 2) - ($config['width'] / 2));
  75. } else {
  76. $config['height'] = $uploadData['image_width'];
  77. $config['width'] = $uploadData['image_width'];
  78. $config['y_axis'] = (($uploadData['image_height'] / 2) - ($config['height'] / 2));
  79. }
  80.  
  81. $this->image_lib->clear();
  82. $this->image_lib->initialize($config);
  83. $this->image_lib->crop();
  84.  
  85. $config['source_image'] = $imagePath . '/crop/' . $uploadData['file_name'];
  86. $config['new_image'] = $imagePath . '/final';
  87. $config['maintain_ratio'] = FALSE;
  88. $config['width'] = 250;
  89. $config['height'] = 250;
  90.  
  91. $this->image_lib->clear();
  92. $this->image_lib->initialize($config);
  93. $this->image_lib->resize();
  94.  
  95. /* Deleting Uploaded Image After Croping and Resizing */
  96. /* Why Deleting because it's saving space */
  97. unlink($uploadData['full_path']);
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement