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); |
||
32 | break; |
||
33 | case 2: // JPG |
||
34 | $srcImage = imageCreateFromJpeg($sourcefile); |
||
35 | break; |
||
36 | case 3: // PNG |
||
37 | $srcImage = imageCreateFromPng($sourcefile); |
||
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); |
||
51 | // Seems not to work: |
||
52 | // imageAntiAlias($tempImage, true); |
||
53 | imagecopyresampled($tempImage, $srcImage, 0, 0, 0, 0, $tempw, $temph, $ow, $oh); |
||
54 | } else { |
||
55 | $tempImage = imageCreate($tempw, $temph); |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
56 | imagecopyresized($tempImage, $srcImage, 0, 0, 0, 0, $tempw, $temph, $ow, $oh); |
||
57 | } |
||
58 | |||
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); |
||
71 | // Seems not to work: |
||
72 | // imageAntiAlias($tempImage, true); |
||
73 | imagecopyresampled($destImage, $tempImage, 0, 0, $offsetx, $offsety, $fw, $fh, $fw, $fh); |
||
74 | } else { |
||
75 | $destImage = imageCreate($fw, $fh); |
||
76 | imagecopyresized($destImage, $tempImage, 0, 0, $offsetx, $offsety, $fw, $fh, $fw, $fh); |
||
77 | } |
||
78 | |||
79 | return $destImage; //imageJpeg($destImage, $destfile, $jpegquality); |
||
80 | } |
||
81 | |||
82 | ?> |
||
83 |