|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class thumb |
|
5
|
|
|
*/ |
|
6
|
|
|
class Thumb |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @param $filename |
|
10
|
|
|
* @param $maxWidth |
|
11
|
|
|
* @param $maxHeight |
|
12
|
|
|
* @return array |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function make($filename, $maxWidth, $maxHeight) |
|
15
|
|
|
{ |
|
16
|
|
|
$imgPath = XOOPS_UPLOAD_PATH . '/apcal/' . $filename; |
|
17
|
|
|
|
|
18
|
|
|
$imgInfo = getimagesize($imgPath); |
|
19
|
|
|
$oWidth = $imgInfo[0]; |
|
20
|
|
|
$oHeight = $imgInfo[1]; |
|
21
|
|
|
$ratio = $oHeight / $oWidth; |
|
22
|
|
|
$nWidth = $maxWidth; |
|
|
|
|
|
|
23
|
|
|
$nHeight = $maxHeight; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
if ($ratio > 1) { |
|
26
|
|
|
$nHeight = $maxHeight; |
|
27
|
|
|
$nWidth = $nHeight / $ratio; |
|
28
|
|
|
} else { |
|
29
|
|
|
$nWidth = $maxWidth; |
|
30
|
|
|
$nHeight = $nWidth * $ratio; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return array('width' => $nWidth, 'height' => $nHeight); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param $filename |
|
38
|
|
|
* @param $maxWidth |
|
39
|
|
|
* @param $maxHeight |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function save($filename, $maxWidth, $maxHeight) |
|
42
|
|
|
{ |
|
43
|
|
|
$thumbPath = XOOPS_UPLOAD_PATH . '/apcal/thumbs/' . $filename; |
|
44
|
|
|
$imgPath = XOOPS_UPLOAD_PATH . '/apcal/' . $filename; |
|
45
|
|
|
$nSize = self::make($filename, $maxWidth, $maxHeight); |
|
46
|
|
|
$imgInfo = getimagesize($imgPath); |
|
47
|
|
|
$oWidth = $imgInfo[0]; |
|
48
|
|
|
$oHeight = $imgInfo[1]; |
|
49
|
|
|
$fileType = $imgInfo[2]; |
|
50
|
|
|
|
|
51
|
|
|
switch ($fileType) { |
|
52
|
|
|
case IMAGETYPE_JPEG: |
|
53
|
|
|
default: |
|
54
|
|
|
$img = imagecreatefromjpeg($imgPath); |
|
55
|
|
|
if (!$img) { |
|
56
|
|
|
$img = imagecreatefromstring(file_get_contents($imgPath)); |
|
57
|
|
|
} |
|
58
|
|
|
break; |
|
59
|
|
|
case IMAGETYPE_GIF: |
|
|
|
|
|
|
60
|
|
|
$img = imagecreatefromgif($imgPath); |
|
61
|
|
|
break; |
|
62
|
|
|
case IMAGETYPE_PNG: |
|
63
|
|
|
$img = imagecreatefrompng($imgPath); |
|
64
|
|
|
break; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$nImg = imagecreatetruecolor($nSize['width'], $nSize['height']); |
|
68
|
|
|
$bgColor = imagecolorallocate($nImg, 0xFF, 0xFF, 0xFF); |
|
69
|
|
|
imagefill($nImg, 0, 0, $bgColor); |
|
70
|
|
|
imagecopyresampled($nImg, $img, 0, 0, 0, 0, $nSize['width'], $nSize['height'], $oWidth, $oHeight); |
|
71
|
|
|
|
|
72
|
|
|
switch ($fileType) { |
|
73
|
|
|
case IMAGETYPE_JPEG: |
|
74
|
|
|
default: |
|
75
|
|
|
imagejpeg($nImg, $thumbPath, 75); |
|
76
|
|
|
break; |
|
77
|
|
|
case IMAGETYPE_GIF: |
|
|
|
|
|
|
78
|
|
|
imagegif($nImg, $thumbPath); |
|
79
|
|
|
break; |
|
80
|
|
|
case IMAGETYPE_PNG: |
|
81
|
|
|
imagepng($nImg, $thumbPath); |
|
82
|
|
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $filename |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function exists($filename) |
|
91
|
|
|
{ |
|
92
|
|
|
return file_exists(XOOPS_UPLOAD_PATH . '/apcal/thumbs/' . $filename); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.