Conditions | 12 |
Paths | 16 |
Total Lines | 71 |
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 |
||
48 | function outputThumbnail($p_sFilePath, $p_sThumbDirectory, $p_iImageWidth, $p_sOutputType = 'png') { |
||
49 | |||
50 | $bIOnlyKnowHowToWorkImagickByUsingTheCommandline = true; |
||
51 | |||
52 | if(!is_dir($p_sThumbDirectory)) { |
||
53 | throw new Exception('The directory to store the Thumbnails in does not exist at "'.$p_sThumbDirectory.'"'); |
||
54 | } |
||
55 | elseif(!is_writable($p_sThumbDirectory)) { |
||
56 | throw new Exception('The directory to store the Thumbnails is not writable "'.$p_sThumbDirectory.'"'); |
||
57 | } |
||
58 | |||
59 | //@TODO: check if file is on of following: '.gif', '.jpg', '.png', '.svg', '.tiff', '.ps', '.pdf', '.bmp', '.eps', '.psd', |
||
60 | /* for .swf support look at http://www.swftools.org/ ? |
||
61 | * for html preview, it is already possible to do to pdf... http://code.google.com/p/wkhtmltopdf/ |
||
62 | * although it's a bit of a hack that might work. |
||
63 | */ |
||
64 | |||
65 | $sSaveFileName = sanitize($p_sFilePath) . '.' . $p_sOutputType; |
||
66 | $sSaveFilePath = $p_sThumbDirectory . $sSaveFileName; |
||
67 | |||
68 | if(class_exists('Imagick')) { |
||
69 | $bRefresh=false; |
||
70 | if($bRefresh === true && is_file($sSaveFilePath)) { |
||
71 | unlink($sSaveFilePath); |
||
72 | }#if |
||
73 | |||
74 | if(!is_file($sSaveFilePath)) { |
||
75 | $aSize = getimagesize($p_sFilePath); |
||
76 | // Create thumb for file |
||
77 | if($bIOnlyKnowHowToWorkImagickByUsingTheCommandline === true) { |
||
78 | // Save Locally |
||
79 | $iImageWidth = $p_iImageWidth; |
||
80 | if(isset($aSize[0]) && $aSize[0] <= $p_iImageWidth) { |
||
81 | $iImageWidth = $aSize[0]; |
||
82 | }#if |
||
83 | |||
84 | $sCommand = |
||
85 | 'convert \'' . urldecode($p_sFilePath) . '[0]\'' |
||
86 | . ' -colorspace RGB' |
||
87 | . ' -geometry ' . $iImageWidth |
||
88 | . ' \'' . $sSaveFilePath . '\'' |
||
89 | ; |
||
90 | |||
91 | $aResult = executeCommand($sCommand); |
||
92 | |||
93 | if($aResult['return'] !== 0) { |
||
94 | throw new Exception('Error executing '.$aResult['stderr'] . '(full command : "'. $aResult['stdin'] .'")'); |
||
95 | }#if |
||
96 | } |
||
97 | else { |
||
98 | // This probably still needs some fixing before it'll actually work :-S |
||
99 | $oImagick = new imagick( $p_sFilePath.'[0]'); // Read First Page of PDF |
||
100 | |||
101 | $oImagick->setResolution($p_iImageWidth, 0); // If 0 is provided as a width or height parameter, aspect ratio is maintained |
||
102 | |||
103 | $oImagick->setImageFormat( "png" ); // Convert to png |
||
104 | header('Content-Type: image/png'); // Send out |
||
105 | |||
106 | echo $oImagick; |
||
107 | }#if |
||
108 | }#if |
||
109 | $oImagick = new imagick($sSaveFilePath); // Read First Page |
||
110 | header('Content-Type: image/png'); // Send out |
||
111 | echo $oImagick; |
||
112 | } /** @noinspection SpellCheckingInspection */ |
||
113 | elseif(function_exists('imagettfbbox') === true) { |
||
114 | throw new Exception('Currently only Imagick is supported'); |
||
115 | }else { |
||
116 | throw new Exception('Either Imagick or the GD2 library need to installed on the server'); |
||
117 | }#if |
||
118 | } |
||
119 | |||
235 |