-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUtils.php
More file actions
81 lines (71 loc) · 2.85 KB
/
Utils.php
File metadata and controls
81 lines (71 loc) · 2.85 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Grav\Plugin;
use Grav\Common\Grav;
use Symfony\Component\Yaml\Yaml;
class Utils
{
public static function return_ERROR($errMsg){
http_response_code(500); // 500 - Internal server error
echo $errMsg;
die();
}
public static function trim_all($str){
$str = str_replace( "'" , "" , $str );
$str = str_replace( '"' , "" , $str );
$str = trim($str);
$str = htmlspecialchars(html_entity_decode($str, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
$str = str_replace( "\n" , "<br>" , $str );
return $str;
}
public static function startsWith ($string, $startString) {
$len = strlen($startString);
return (substr($string, 0, $len) === $startString);
}
private static function make_jpeg_thumbnail($source, $target){
$source = realpath($source);
$im = new \Imagick();
$im->setResolution(300,300); // set loaded resolution
$im->readImage($source."[0]"); // 0-first page, 1-second page
$im->transformimagecolorspace(\Imagick::COLORSPACE_SRGB); //CMYK to RGB
$im->setImageBackgroundColor('#ffffff'); //prevents black background
$im = $im->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN); //merge layers
$im->setimageformat("jpeg"); //converts to JPEG
$im->setCompression(\Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(80);
$im->resizeImage(595, 842, \Imagick::FILTER_LANCZOS,1); //set saved resolution
$im->writeImage($target); //save
$im->clear();
$im->destroy();
}
public static function save_PDF($pdf_save_path, $pdf_file_name, $makeThumbnail=True){
$file = $_FILES['PDF']['tmp_name'];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file);
if ($mime != 'application/pdf') {
Utils::return_ERROR('Nahraný soubor není PDF!');
}
if(!is_dir($pdf_save_path)){
mkdir($pdf_save_path);
}
$saveFilesPath = $pdf_save_path ."/". $pdf_file_name;
move_uploaded_file($file, $saveFilesPath);
if($makeThumbnail){
self::make_jpeg_thumbnail($saveFilesPath, $saveFilesPath . ".jpg");
}
}
public static function log($msg){
$user = Grav::instance()['session']->user;
if ($user->authenticated) {
$username = !empty($user->fullname) ? $user->fullname : $user->username;
}
else {
$username = "System";
}
$log_msg = "{$username} | {$msg}";
Grav::instance()['log']->info($log_msg);
}
public static function format_date($date){
return date("Y-m-d", strtotime(str_replace(' ','', $date))); // d. m. Y => Y-m-d
}
}
?>