Issues (4542)

config/functions.php (3 issues)

1
<?php
2
3
/*
4
    Upload an image and create the thumbnail. The thumbnail is stored
5
    under the thumbnail sub-directory of $uploadDir.
6
7
    Return the uploaded image name and the thumbnail also.
8
*/
9
function uploadImage($inputName, $uploadDir)
10
{
11
    $image = $_FILES[$inputName];
12
    $imagePath = '';
13
    $thumbnailPath = '';
14
15
    // if a file is given
16
    if (trim($image['tmp_name']) != '') {
17
        $ext = substr(strrchr($image['name'], '.'), 1);
0 ignored issues
show
The assignment to $ext is dead and can be removed.
Loading history...
18
19
        // generate a random new file name to avoid name conflict
20
        // then save the image under the new file name
21
        $imagePath = '';
22
        $result = move_uploaded_file($image['tmp_name'], $uploadDir.$imagePath);
23
24
        if ($result) {
25
            // create thumbnail
26
            $thumbnailPath = '';
27
            $result = createThumbnail($uploadDir.$imagePath, $uploadDir.'thumbnail/'.$thumbnailPath, THUMBNAIL_WIDTH);
28
29
            // create thumbnail failed, delete the image
30
            if (!$result) {
31
                unlink($uploadDir.$imagePath);
32
                $imagePath = $thumbnailPath = '';
33
            } else {
34
                $thumbnailPath = $result;
35
            }
36
        } else {
37
            // the image cannot be uploaded
38
            $imagePath = $thumbnailPath = '';
39
        }
40
    }
41
42
    return ['image' => $imagePath, 'thumbnail' => $thumbnailPath];
43
}
44
45
/*
46
    Create a thumbnail of $srcFile and save it to $destFile.
47
    The thumbnail will be $width pixels.
48
*/
49
function createThumbnail($srcFile, $destFile, $width, $quality = 75)
50
{
51
    $thumbnail = '';
52
53
    if (file_exists($srcFile) && isset($destFile)) {
54
        $size = getimagesize($srcFile);
55
        $w = number_format($width, 0, ',', '');
56
        $h = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
57
58
        $thumbnail = copyImage($srcFile, $destFile, $w, $h, $quality);
59
    }
60
61
    // return the thumbnail file name on sucess or blank on fail
62
    return basename($thumbnail);
63
}
64
65
/*
66
    Copy an image to a destination file. The destination
67
    image size will be $w X $h pixels
68
*/
69
function copyImage($srcFile, $destFile, $w, $h, $quality = 100)
70
{
71
    $tmpSrc = pathinfo(strtolower($srcFile));
0 ignored issues
show
The assignment to $tmpSrc is dead and can be removed.
Loading history...
72
    $tmpDest = pathinfo(strtolower($destFile));
73
    $size = getimagesize($srcFile);
74
75
    if ($tmpDest['extension'] == 'gif' || $tmpDest['extension'] == 'jpg') {
76
        $destFile = substr_replace($destFile, 'jpg', -3);
77
        $dest = imagecreatetruecolor($w, $h);
78
    //imageantialias($dest, TRUE);
79
    } elseif ($tmpDest['extension'] == 'png') {
80
        $dest = imagecreatetruecolor($w, $h);
81
    //imageantialias($dest, TRUE);
82
    } else {
83
        return false;
84
    }
85
86
    switch ($size[2]) {
87
        case 1:       //GIF
88
           $src = imagecreatefromgif($srcFile);
89
            break;
90
        case 2:       //JPEG
91
           $src = imagecreatefromjpeg($srcFile);
92
            break;
93
        case 3:       //PNG
94
           $src = imagecreatefrompng($srcFile);
95
            break;
96
        default:
97
           return false;
98
            break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
99
    }
100
101
    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
102
103
    switch ($size[2]) {
104
        case 1:
105
       case 2:
106
           imagejpeg($dest, $destFile, $quality);
107
            break;
108
        case 3:
109
           imagepng($dest, $destFile);
110
    }
111
112
    return $destFile;
113
}
114