-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
40 lines (32 loc) · 1.59 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
function generateThumbnail($sourceFile, $thumbnailWidth, $thumbnailHeight) {
list($width, $height) = getimagesize($sourceFile);
$pathinfo = pathinfo($sourceFile);
$thumb = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
switch ($pathinfo['extension']) {
case 'gif': $source = imagecreatefromgif($sourceFile); break;
case 'jpg': $source = imagecreatefromjpeg($sourceFile); break;
case 'png': $source = imagecreatefrompng($sourceFile); break;
case 'webp': $source = imagecreatefromwebp($sourceFile); break;
default: return ''; break;
};
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $width, $height);
$directory = $pathinfo['dirname'] . '/thumbnails/' . $pathinfo['filename'] . '.' . $pathinfo['extension'];
switch ($pathinfo['extension']) {
case 'gif': $source = imagegif($thumb, $directory, 100); break;
case 'jpg': $source = imagejpeg($thumb, $directory, 100); break;
case 'png': $source = imagepng($thumb, $directory, 100); break;
case 'webp': $source = imagewebp($thumb, $directory, 100); break;
default: return ''; break;
};
return $directory;
};
function checkThumbnailExist($sourceFile, $thumbnailWidth, $thumbnailHeight) {
$pathinfo = pathinfo($sourceFile);
$directory = $pathinfo['dirname'] . '/thumbnails/' . $pathinfo['filename'] . '.' . $pathinfo['extension'];
if (file_exists($directory)) {
return $directory;
} else {
return generateThumbnail($sourceFile, $thumbnailWidth, $thumbnailHeight);
};
};