src/Helpers/Files/FileHelper.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Helpers\Files;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\File\File;
  5. use Gregwar\Image\Image;
  6. class FileHelper
  7. {
  8. public function __construct(EntityManagerInterface $entityManager)
  9. {
  10. $this->em = $entityManager;
  11. }
  12. public function upload(?\Symfony\Component\HttpFoundation\File\File $file = null, ?string $path = null, $filename = null)
  13. {
  14. // Missing details?
  15. if(!$file && !$path && !$filename)
  16. throw new \Exception("Invalid file path given.");
  17. // Directory not created?
  18. if(!is_dir($path))
  19. mkdir($path);
  20. // Let's move it
  21. $file->move($path, $filename);
  22. return true;
  23. }
  24. public function move($path = null, $newPath = null)
  25. {
  26. return true;
  27. }
  28. public function delete($path = null)
  29. {
  30. if(!$path)
  31. throw new \Exception("Invalid path!");
  32. // Remove first slash
  33. $path = ltrim($path, '/');
  34. $realpath = realpath($path);
  35. $currentDirectory = getcwd() . '/files';
  36. $currentDirectoryLength = strlen($currentDirectory);
  37. // Compare string
  38. $subString = substr($realpath, 0, $currentDirectoryLength);
  39. // Dangerous match!
  40. if($subString != $currentDirectory)
  41. throw new \Exception("You do not have permission to access this directory");
  42. if(is_dir($path))
  43. {
  44. $path = rtrim($path, '/\\');
  45. $i = new \DirectoryIterator($path);
  46. foreach($i as $f)
  47. {
  48. if($f->isFile())
  49. {
  50. unlink($f->getRealPath());
  51. }
  52. else if(!$f->isDot() && $f->isDir())
  53. {
  54. $this->delete($f->getRealPath());
  55. }
  56. }
  57. rmdir($path);
  58. return true;
  59. }
  60. if(file_exists($path))
  61. {
  62. unlink($path);
  63. return true;
  64. }
  65. }
  66. public function slugifyFilename($filename = null)
  67. {
  68. // No filename
  69. if(!$filename)
  70. return null;
  71. // Slugify the name
  72. return preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $filename);
  73. }
  74. public function getFriendlyName($filename = null)
  75. {
  76. // No filename
  77. if(!$filename)
  78. return null;
  79. // Return and remove extension.
  80. return preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
  81. }
  82. public function thumbnail($image, $width = 200, $height = 200, $type = 'zoomcrop', $bgColour = 'FFFFFF')
  83. {
  84. $image = trim($image, '/');
  85. // Split up path
  86. $filename = basename($image);
  87. $directory = trim(dirname($image), '/');
  88. // check if file exists
  89. if(!file_exists($image))
  90. return "/assets/admin/img/noimage.png";
  91. // Split up by extension
  92. $bits = explode('.', $filename);
  93. if(count($bits) == 1)
  94. return "/assets/admin/img/noimage.png";
  95. $name = $bits[0];
  96. $extension = $bits[1];
  97. if($type == "resize")
  98. $nameType = "r";
  99. else
  100. $nameType = "zc";
  101. // Get new name
  102. $newName = $name . '-' . $nameType . '-' . $width . 'x' . $height . '.' . $extension;
  103. // Does it already exist?
  104. if(is_file($directory . '/' . $newName))
  105. {
  106. return '/' . $directory . '/' . $newName;
  107. }
  108. else
  109. {
  110. if($type == "resize")
  111. {
  112. // Resize it
  113. Image::open($image)
  114. ->resize($width, $height, $bgColour, 0, 0)
  115. ->save($directory . '/' . $newName);
  116. }
  117. else
  118. {
  119. // default zoomcrop
  120. Image::open($image)
  121. ->zoomCrop($width, $height, 'FFFFFF', 0, 0)
  122. ->save($directory . '/' . $newName);
  123. }
  124. // return image
  125. return '/' . $directory . '/' . $newName;
  126. }
  127. }
  128. public function generatePdfFromUrl(string $url, string $saveLocation, array $options = []): ?string
  129. {
  130. try
  131. {
  132. // Curl setup
  133. $ch = curl_init("https://www.pdfcommand.com/generate");
  134. curl_setopt($ch, CURLOPT_HEADER, false);
  135. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  136. curl_setopt($ch, CURLOPT_POSTFIELDS, [
  137. "key" => "8952573e-f689-11ee-a6df-f69977c54390",
  138. "type" => "url",
  139. "url" => $url,
  140. "options" => $options
  141. ]);
  142. // Execute it
  143. $response = curl_exec($ch);
  144. // Grab the HTTP code
  145. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  146. // Close curl session
  147. curl_close($ch);
  148. // Problem
  149. if($httpcode != 200)
  150. return null;
  151. // Grab the file
  152. $fc = file_get_contents($response);
  153. // Write it
  154. file_put_contents($saveLocation, $fc);
  155. return $saveLocation;
  156. }
  157. catch(\Exception $e)
  158. {
  159. return null;
  160. }
  161. }
  162. public function generatePdfFromHtml(string $html, string $saveLocation, array $options = []): ?string
  163. {
  164. try
  165. {
  166. // Does the save location directory not exist?
  167. if(!is_dir(dirname($saveLocation)))
  168. mkdir(dirname($saveLocation), 0777, true);
  169. // Add prefixes to options
  170. foreach($options AS $k => $v)
  171. {
  172. unset($options[$k]);
  173. $options["opt-" . $k] = $v;
  174. }
  175. // Curl setup
  176. $ch = curl_init("https://www.pdfcommand.com/generate");
  177. curl_setopt($ch, CURLOPT_HEADER, false);
  178. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  179. curl_setopt($ch, CURLOPT_POSTFIELDS, array_merge([
  180. "key" => "8952573e-f689-11ee-a6df-f69977c54390",
  181. "type" => "html",
  182. "html" => $html
  183. ], $options));
  184. // Execute it
  185. $response = curl_exec($ch);
  186. // Grab the HTTP code
  187. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  188. // Close curl session
  189. curl_close($ch);
  190. // Problem
  191. if($httpcode != 200)
  192. return null;
  193. // Grab the file
  194. $fc = file_get_contents($response);
  195. // Write it
  196. file_put_contents($saveLocation, $fc);
  197. return $saveLocation;
  198. }
  199. catch(\Exception $e)
  200. {
  201. dump($e);
  202. return null;
  203. }
  204. }
  205. }