1 | <?php |
||||||
2 | // This file is part of BOINC. |
||||||
3 | // http://boinc.berkeley.edu |
||||||
4 | // Copyright (C) 2008 University of California |
||||||
5 | // |
||||||
6 | // BOINC is free software; you can redistribute it and/or modify it |
||||||
7 | // under the terms of the GNU Lesser General Public License |
||||||
8 | // as published by the Free Software Foundation, |
||||||
9 | // either version 3 of the License, or (at your option) any later version. |
||||||
10 | // |
||||||
11 | // BOINC is distributed in the hope that it will be useful, |
||||||
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||||||
14 | // See the GNU Lesser General Public License for more details. |
||||||
15 | // |
||||||
16 | // You should have received a copy of the GNU Lesser General Public License |
||||||
17 | // along with BOINC. If not, see <http://www.gnu.org/licenses/>. |
||||||
18 | |||||||
19 | // Scale an image using the most powerful GD software available on the server |
||||||
20 | // while keeping aspect ratio the same |
||||||
21 | // |
||||||
22 | function intelligently_scale_image($sourcefile, $fw, $fh) { |
||||||
23 | $gd_info = gd_info(); |
||||||
24 | // libgd version numbers seem to be always three numbers |
||||||
25 | preg_match('/(\d).(\d).(\d)/', $gd_info['GD Version'], $match); |
||||||
26 | $newGD = ($match[1]>=2); |
||||||
27 | |||||||
28 | list($ow, $oh, $from_type) = getimagesize($sourcefile); |
||||||
29 | switch($from_type) { |
||||||
30 | case 1: // GIF |
||||||
31 | $srcImage = imageCreateFromGif($sourcefile); |
||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||||
32 | break; |
||||||
33 | case 2: // JPG |
||||||
34 | $srcImage = imageCreateFromJpeg($sourcefile); |
||||||
0 ignored issues
–
show
|
|||||||
35 | break; |
||||||
36 | case 3: // PNG |
||||||
37 | $srcImage = imageCreateFromPng($sourcefile); |
||||||
0 ignored issues
–
show
|
|||||||
38 | break; |
||||||
39 | } |
||||||
40 | |||||||
41 | $tempw = $fw; |
||||||
42 | $temph = number_format((($oh*$fw)/$ow), 0); |
||||||
43 | |||||||
44 | if($temph < $fh) { |
||||||
45 | $tempw = number_format((($ow*$fh)/$oh), 0); |
||||||
46 | $temph = $fh; |
||||||
47 | } |
||||||
48 | |||||||
49 | if ($newGD){ |
||||||
50 | $tempImage = imageCreateTrueColor($tempw, $temph); |
||||||
0 ignored issues
–
show
|
|||||||
51 | // Seems not to work: |
||||||
52 | // imageAntiAlias($tempImage, true); |
||||||
53 | imagecopyresampled($tempImage, $srcImage, 0, 0, 0, 0, $tempw, $temph, $ow, $oh); |
||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||||
54 | } else { |
||||||
55 | $tempImage = imageCreate($tempw, $temph); |
||||||
0 ignored issues
–
show
|
|||||||
56 | imagecopyresized($tempImage, $srcImage, 0, 0, 0, 0, $tempw, $temph, $ow, $oh); |
||||||
57 | } |
||||||
58 | |||||||
0 ignored issues
–
show
|
|||||||
59 | |||||||
60 | // Calculate offsets |
||||||
61 | if($temph < $fh) { |
||||||
62 | $offsety = number_format(($temph/2)-($fh/2), 0); |
||||||
63 | $offsetx = 0; |
||||||
64 | } else { |
||||||
65 | $offsety = 0; |
||||||
66 | $offsetx = number_format(($tempw/2)-($fw/2), 0); |
||||||
67 | } |
||||||
68 | |||||||
69 | if ($newGD){ |
||||||
70 | $destImage = imageCreateTrueColor($fw, $fh); |
||||||
0 ignored issues
–
show
|
|||||||
71 | // Seems not to work: |
||||||
72 | // imageAntiAlias($tempImage, true); |
||||||
73 | imagecopyresampled($destImage, $tempImage, 0, 0, $offsetx, $offsety, $fw, $fh, $fw, $fh); |
||||||
0 ignored issues
–
show
It seems like
$offsetx can also be of type string ; however, parameter $src_x of imagecopyresampled() does only seem to accept integer , 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
![]() It seems like
$offsety can also be of type string ; however, parameter $src_y of imagecopyresampled() does only seem to accept integer , 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
![]() |
|||||||
74 | } else { |
||||||
75 | $destImage = imageCreate($fw, $fh); |
||||||
0 ignored issues
–
show
|
|||||||
76 | imagecopyresized($destImage, $tempImage, 0, 0, $offsetx, $offsety, $fw, $fh, $fw, $fh); |
||||||
0 ignored issues
–
show
It seems like
$offsety can also be of type string ; however, parameter $src_y of imagecopyresized() does only seem to accept integer , 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
![]() It seems like
$offsetx can also be of type string ; however, parameter $src_x of imagecopyresized() does only seem to accept integer , 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
![]() |
|||||||
77 | } |
||||||
78 | |||||||
79 | return $destImage; //imageJpeg($destImage, $destfile, $jpegquality); |
||||||
80 | } |
||||||
81 | |||||||
82 | ?> |
||||||
83 |