| Conditions | 13 |
| Paths | 39 |
| Total Lines | 96 |
| Code Lines | 69 |
| 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 |
||
| 123 | public function renderAppIcon($app, $size) { |
||
| 124 | $appIcon = $this->util->getAppIcon($app); |
||
| 125 | if($appIcon === false) { |
||
| 126 | return false; |
||
| 127 | } |
||
| 128 | if ($appIcon instanceof ISimpleFile) { |
||
| 129 | $appIconContent = $appIcon->getContent(); |
||
| 130 | $mime = $appIcon->getMimeType(); |
||
| 131 | } else { |
||
| 132 | $appIconContent = file_get_contents($appIcon); |
||
| 133 | $mime = mime_content_type($appIcon); |
||
| 134 | } |
||
| 135 | |||
| 136 | if($appIconContent === false || $appIconContent === "") { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | |||
| 140 | $color = $this->themingDefaults->getColorPrimary(); |
||
| 141 | |||
| 142 | // generate background image with rounded corners |
||
| 143 | $background = '<?xml version="1.0" encoding="UTF-8"?>' . |
||
| 144 | '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:cc="http://creativecommons.org/ns#" width="512" height="512" xmlns:xlink="http://www.w3.org/1999/xlink">' . |
||
| 145 | '<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' . |
||
| 146 | '</svg>'; |
||
| 147 | // resize svg magic as this seems broken in Imagemagick |
||
| 148 | if($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") { |
||
| 149 | if(substr($appIconContent, 0, 5) !== "<?xml") { |
||
| 150 | $svg = "<?xml version=\"1.0\"?>".$appIconContent; |
||
| 151 | } else { |
||
| 152 | $svg = $appIconContent; |
||
| 153 | } |
||
| 154 | $tmp = new Imagick(); |
||
| 155 | $tmp->readImageBlob($svg); |
||
| 156 | $x = $tmp->getImageWidth(); |
||
| 157 | $y = $tmp->getImageHeight(); |
||
| 158 | $res = $tmp->getImageResolution(); |
||
| 159 | $tmp->destroy(); |
||
| 160 | |||
| 161 | if($x>$y) { |
||
| 162 | $max = $x; |
||
| 163 | } else { |
||
| 164 | $max = $y; |
||
| 165 | } |
||
| 166 | |||
| 167 | // convert svg to resized image |
||
| 168 | $appIconFile = new Imagick(); |
||
| 169 | $resX = (int)(512 * $res['x'] / $max * 2.53); |
||
| 170 | $resY = (int)(512 * $res['y'] / $max * 2.53); |
||
| 171 | $appIconFile->setResolution($resX, $resY); |
||
| 172 | $appIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
||
| 173 | $appIconFile->readImageBlob($svg); |
||
| 174 | |||
| 175 | /** |
||
| 176 | * invert app icons for bright primary colors |
||
| 177 | * the default nextcloud logo will not be inverted to black |
||
| 178 | */ |
||
| 179 | if ($this->util->invertTextColor($color) |
||
| 180 | && !$appIcon instanceof ISimpleFile |
||
| 181 | && $app !== "core" |
||
| 182 | ) { |
||
| 183 | $appIconFile->negateImage(false); |
||
| 184 | } |
||
| 185 | $appIconFile->scaleImage(512, 512, true); |
||
| 186 | } else { |
||
| 187 | $appIconFile = new Imagick(); |
||
| 188 | $appIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
||
| 189 | $appIconFile->readImageBlob($appIconContent); |
||
| 190 | $appIconFile->scaleImage(512, 512, true); |
||
| 191 | } |
||
| 192 | // offset for icon positioning |
||
| 193 | $border_w = (int)($appIconFile->getImageWidth() * 0.05); |
||
| 194 | $border_h = (int)($appIconFile->getImageHeight() * 0.05); |
||
| 195 | $innerWidth = (int)($appIconFile->getImageWidth() - $border_w * 2); |
||
| 196 | $innerHeight = (int)($appIconFile->getImageHeight() - $border_h * 2); |
||
| 197 | $appIconFile->adaptiveResizeImage($innerWidth, $innerHeight); |
||
| 198 | // center icon |
||
| 199 | $offset_w = 512 / 2 - $innerWidth / 2; |
||
| 200 | $offset_h = 512 / 2 - $innerHeight / 2; |
||
| 201 | |||
| 202 | $finalIconFile = new Imagick(); |
||
| 203 | $finalIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
||
| 204 | $finalIconFile->readImageBlob($background); |
||
| 205 | $finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT); |
||
| 206 | $finalIconFile->setImageArtifact('compose:args', "1,0,-0.5,0.5"); |
||
| 207 | $finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h); |
||
| 208 | $finalIconFile->setImageFormat('png24'); |
||
| 209 | if (defined("Imagick::INTERPOLATE_BICUBIC") === true) { |
||
| 210 | $filter = Imagick::INTERPOLATE_BICUBIC; |
||
| 211 | } else { |
||
| 212 | $filter = Imagick::FILTER_LANCZOS; |
||
| 213 | } |
||
| 214 | $finalIconFile->resizeImage($size, $size, $filter, 1, false); |
||
| 215 | |||
| 216 | $appIconFile->destroy(); |
||
| 217 | return $finalIconFile; |
||
| 218 | } |
||
| 219 | |||
| 237 |