1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Saito - The Threaded Web Forum |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
8
|
|
|
* @link https://github.com/Schlaefer/Saito |
9
|
|
|
* @license http://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ImageUploader\Lib; |
13
|
|
|
|
14
|
|
|
use claviska\SimpleImage; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Determine mime-type for a file and try to fix some common mime-type issues. |
18
|
|
|
*/ |
19
|
|
|
class ImageProcessor |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Resizes a file |
23
|
|
|
* |
24
|
|
|
* @param \ImageUploader\Lib\File $file File |
25
|
|
|
* @param int $target Target size |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public static function resize(File $file, int $target): void |
29
|
|
|
{ |
30
|
|
|
if ($file->getSize() < $target) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$raw = $file->read(); |
35
|
|
|
|
36
|
|
|
[$width, $height] = getimagesizefromstring($raw); |
37
|
|
|
$ratio = $file->getSize() / $target; |
38
|
|
|
$ratio = sqrt($ratio); |
39
|
|
|
|
40
|
|
|
$newwidth = (int)($width / $ratio); |
41
|
|
|
$newheight = (int)($height / $ratio); |
42
|
|
|
$destination = imagecreatetruecolor($newwidth, $newheight); |
43
|
|
|
|
44
|
|
|
$source = imagecreatefromstring($raw); |
45
|
|
|
imagecopyresized($destination, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$raw = $destination; |
|
|
|
|
48
|
|
|
|
49
|
|
|
$type = $file->getMime(); |
50
|
|
|
switch ($type) { |
51
|
|
|
case 'image/jpeg': |
52
|
|
|
imagejpeg($destination, $file->getPath()); |
|
|
|
|
53
|
|
|
break; |
54
|
|
|
case 'image/png': |
55
|
|
|
imagepng($destination, $file->getPath()); |
|
|
|
|
56
|
|
|
break; |
57
|
|
|
default: |
58
|
|
|
throw new \RuntimeException(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Fix image orientation according to image exif data |
64
|
|
|
* |
65
|
|
|
* @param string $path Path to file |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public static function fixOrientation(string $path): void |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
|
|
(new SimpleImage()) |
72
|
|
|
->fromFile($path) |
73
|
|
|
->autoOrient() |
74
|
|
|
->toFile($path, null, 75); |
75
|
|
|
} catch (\Throwable $e) { |
76
|
|
|
throw new \RuntimeException('Fixing orientation failed.'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Convert image file to jpeg |
82
|
|
|
* |
83
|
|
|
* @param string $path Path to file |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public static function convertToJpeg(string $path): void |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
|
|
(new SimpleImage()) |
90
|
|
|
->fromFile($path) |
91
|
|
|
->toFile($path, 'image/jpeg', 75); |
92
|
|
|
} catch (\Throwable $e) { |
93
|
|
|
throw new \RuntimeException('Converting file to jpeg failed.'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|