-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize.php
More file actions
114 lines (85 loc) · 3.96 KB
/
Copy pathoptimize.php
File metadata and controls
114 lines (85 loc) · 3.96 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
function randomName($sub = '', $length = 10){
$letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","r","s","t","u","v","y","z","x","w","q");
$numbers = array(0,1,2,3,4,5,6,7,8,9);
$combine = array_merge($letters,$numbers);
$res = $sub;
$res .= $letters[rand(0,count($letters)-1)];
for($i = 0; $i < $length - 2; $i++){
$res .= $combine[rand(0,count($combine)-1)];
}
return $res;
}
function compress_png($path_to_png_file, $max_quality = 90)
{
if (!file_exists($path_to_png_file)) {
throw new Exception("File does not exist: $path_to_png_file");
}
$min_quality = 40;
$compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg( $path_to_png_file));
if (!$compressed_png_content) {
throw new Exception("Conversion to compressed PNG failed. Is pngquant 1.8+ installed on the server?");
}
return $compressed_png_content;
}
function optimizeImage($file, $dst='dst', $with_dir=false){
if($with_dir){
$directory = $file; //folder
$dst = $dst.'/';
$dir = array_diff(scandir($directory), array('..', '.'));
$quality = 50;
for($i = 2; $i < count($dir); $i++){
$filePath = $file.'/'.$dir[$i];
$info = getimagesize($filePath);
$extension = image_type_to_extension($info[2]);
$fileName = explode('/',$filePath);
$fileName = end($fileName);
$path = str_replace($fileName,'',$filePath);
if($extension == ".png"){
$path_to_uncompressed_file = $filePath;
$path_to_compressed_file = $dst.'/'.$fileName;
file_put_contents($path_to_compressed_file, compress_png($path_to_uncompressed_file,$quality));
echo $i.' is <font color="red">png</font> --- ';
}
elseif ($extension == ".jpg" || $extension == ".jpeg") {
$image = imagecreatefromjpeg($filePath);
imagejpeg($image, $dst.'/'.$fileName,$quality);
echo $i.' is <font color="green">jpeg</font> --- ';
}
}
}
else{
$filePath = $file['tmp_name'];
$quality = 50;
$info = getimagesize($filePath);
$extension = image_type_to_extension($info[2]);
$fname = explode(".", $file['name']);
$ext_for_save = ".".end($fname);
//$fileName = explode('/',$filePath);
//$fileName = end($fileName);
$fileName = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file['name']);;
$path = str_replace($fileName,'',$filePath);
if($extension == ".png"){
$image = imagecreatefrompng($filePath);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
imagejpeg($bg, $dst.'/'.$fileName.$ext_for_save, $quality);
imagedestroy($bg);
echo "$fileName is png format and compressed";
}
elseif ($extension == ".jpg" || $extension == ".jpeg") {
$image = imagecreatefromjpeg($filePath);
imagejpeg($image, $dst.'/'.$fileName.$ext_for_save,$quality);
echo $fileName.$ext_for_save." is jpeg format and compressed";
}
else{
}
}
}
$file = $_FILES['dosya'];
optimizeImage($file);
//echo randomName('emlakodam');
?>