1 |
www |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Service;
|
|
|
6 |
|
|
|
7 |
class StorageService
|
|
|
8 |
{
|
|
|
9 |
// The directory where we save image files.
|
|
|
10 |
private $saveToDir = './data/upload/';
|
|
|
11 |
|
|
|
12 |
// Returns path to the directory where we save the image files.
|
|
|
13 |
public function getSaveToDir()
|
|
|
14 |
{
|
|
|
15 |
return $this->saveToDir;
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
/*
|
|
|
19 |
// Returns the array of uploaded file names.
|
|
|
20 |
public function getSavedFiles()
|
|
|
21 |
{
|
|
|
22 |
// The directory where we plan to save uploaded files.
|
|
|
23 |
|
|
|
24 |
// Check whether the directory already exists, and if not,
|
|
|
25 |
// create the directory.
|
|
|
26 |
if(!is_dir($this->saveToDir)) {
|
|
|
27 |
if(!mkdir($this->saveToDir)) {
|
|
|
28 |
throw new \Exception('Could not create directory for uploads: ' .
|
|
|
29 |
error_get_last());
|
|
|
30 |
}
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
// Scan the directory and create the list of uploaded files.
|
|
|
34 |
$files = [];
|
|
|
35 |
$handle = opendir($this->saveToDir);
|
|
|
36 |
while (false !== ($entry = readdir($handle))) {
|
|
|
37 |
|
|
|
38 |
if($entry=='.' || $entry=='..')
|
|
|
39 |
continue; // Skip current dir and parent dir.
|
|
|
40 |
|
|
|
41 |
$files[] = $entry;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
// Return the list of uploaded files.
|
|
|
45 |
return $files;
|
|
|
46 |
}*/
|
|
|
47 |
|
|
|
48 |
// Returns the path to the saved file.
|
|
|
49 |
public function getFilePathByName($fileName)
|
|
|
50 |
{
|
|
|
51 |
// Take some precautions to make file name secure.
|
|
|
52 |
$fileName = str_replace("/", "", $fileName); // Remove slashes.
|
|
|
53 |
$fileName = str_replace("\\", "", $fileName); // Remove back-slashes.
|
|
|
54 |
|
|
|
55 |
// Return concatenated directory name and file name.
|
|
|
56 |
return $this->saveToDir . $fileName;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
// Returns the file content. On error, returns boolean false.
|
|
|
60 |
public function getFileContent($filePath)
|
|
|
61 |
{
|
|
|
62 |
return file_get_contents($filePath);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// Retrieves the file information (size, MIME type) by image path.
|
|
|
66 |
public function getImageFileInfo($filePath)
|
|
|
67 |
{
|
|
|
68 |
// Try to open file
|
|
|
69 |
if (!is_readable($filePath)) {
|
|
|
70 |
return false;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// Get file size in bytes.
|
|
|
74 |
$fileSize = filesize($filePath);
|
|
|
75 |
|
|
|
76 |
// Get MIME type of the file.
|
|
|
77 |
$finfo = finfo_open(FILEINFO_MIME);
|
|
|
78 |
$mimeType = finfo_file($finfo, $filePath);
|
|
|
79 |
if($mimeType===false) {
|
|
|
80 |
$mimeType = 'application/octet-stream';
|
|
|
81 |
}
|
|
|
82 |
return [
|
|
|
83 |
'size' => $fileSize,
|
|
|
84 |
'type' => $mimeType
|
|
|
85 |
];
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// Resizes the image, keeping its aspect ratio.
|
|
|
89 |
public function resizeImage($filePath, $desiredWidth = 240)
|
|
|
90 |
{
|
|
|
91 |
// Get original image dimensions.
|
|
|
92 |
list($originalWidth, $originalHeight) = getimagesize($filePath);
|
|
|
93 |
|
|
|
94 |
// Calculate aspect ratio
|
|
|
95 |
$aspectRatio = $originalWidth/$originalHeight;
|
|
|
96 |
// Calculate the resulting height
|
|
|
97 |
$desiredHeight = $desiredWidth/$aspectRatio;
|
|
|
98 |
|
|
|
99 |
// Get image info
|
|
|
100 |
$fileInfo = $this->getImageFileInfo($filePath);
|
|
|
101 |
|
|
|
102 |
// Resize the image
|
|
|
103 |
$resultingImage = imagecreatetruecolor($desiredWidth, $desiredHeight);
|
|
|
104 |
if (substr($fileInfo['type'], 0, 9) =='image/png') {
|
|
|
105 |
$originalImage = imagecreatefrompng($filePath);
|
|
|
106 |
} else {
|
|
|
107 |
$originalImage = imagecreatefromjpeg($filePath);
|
|
|
108 |
}
|
|
|
109 |
imagecopyresampled($resultingImage, $originalImage, 0, 0, 0, 0,
|
|
|
110 |
$desiredWidth, $desiredHeight, $originalWidth, $originalHeight);
|
|
|
111 |
|
|
|
112 |
// Save the resized image to temporary location
|
|
|
113 |
$tmpFileName = tempnam("/tmp", "FOO");
|
|
|
114 |
imagejpeg($resultingImage, $tmpFileName, 80);
|
|
|
115 |
|
|
|
116 |
// Return the path to resulting image.
|
|
|
117 |
return $tmpFileName;
|
|
|
118 |
}
|
|
|
119 |
}
|