Conditions | 8 |
Paths | 64 |
Total Lines | 58 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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); |
||
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 | } |
||
83 |