Completed
Push — feature/6.x ( db50a0...aa9894 )
by Schlaefer
03:28
created

ImageProcessor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resize() 0 31 4
A convertToJpeg() 0 8 2
A fixOrientation() 0 9 2
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);
0 ignored issues
show
Bug introduced by
It seems like $destination can also be of type false; however, parameter $dst_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        imagecopyresized(/** @scrutinizer ignore-type */ $destination, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
Loading history...
Bug introduced by
It seems like $source can also be of type false; however, parameter $src_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        imagecopyresized($destination, /** @scrutinizer ignore-type */ $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
Loading history...
46
47
        $raw = $destination;
0 ignored issues
show
Unused Code introduced by
The assignment to $raw is dead and can be removed.
Loading history...
48
49
        $type = $file->getMime();
50
        switch ($type) {
51
            case 'image/jpeg':
52
                imagejpeg($destination, $file->getPath());
0 ignored issues
show
Bug introduced by
It seems like $destination can also be of type false; however, parameter $image of imagejpeg() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
                imagejpeg(/** @scrutinizer ignore-type */ $destination, $file->getPath());
Loading history...
53
                break;
54
            case 'image/png':
55
                imagepng($destination, $file->getPath());
0 ignored issues
show
Bug introduced by
It seems like $destination can also be of type false; however, parameter $image of imagepng() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                imagepng(/** @scrutinizer ignore-type */ $destination, $file->getPath());
Loading history...
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