| Conditions | 13 |
| Paths | 39 |
| Total Lines | 96 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 97 | public function renderAppIcon($app, $size) { |
||
| 98 | $appIcon = $this->util->getAppIcon($app); |
||
| 99 | if($appIcon === false) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | if ($appIcon instanceof ISimpleFile) { |
||
| 103 | $appIconContent = $appIcon->getContent(); |
||
| 104 | $mime = $appIcon->getMimeType(); |
||
| 105 | } else { |
||
| 106 | $appIconContent = file_get_contents($appIcon); |
||
| 107 | $mime = mime_content_type($appIcon); |
||
| 108 | } |
||
| 109 | |||
| 110 | if($appIconContent === false || $appIconContent === "") { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | $color = $this->themingDefaults->getColorPrimary(); |
||
| 115 | |||
| 116 | // generate background image with rounded corners |
||
| 117 | $background = '<?xml version="1.0" encoding="UTF-8"?>' . |
||
| 118 | '<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">' . |
||
| 119 | '<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' . |
||
| 120 | '</svg>'; |
||
| 121 | // resize svg magic as this seems broken in Imagemagick |
||
| 122 | if($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") { |
||
| 123 | if(substr($appIconContent, 0, 5) !== "<?xml") { |
||
| 124 | $svg = "<?xml version=\"1.0\"?>".$appIconContent; |
||
| 125 | } else { |
||
| 126 | $svg = $appIconContent; |
||
| 127 | } |
||
| 128 | $tmp = new Imagick(); |
||
| 129 | $tmp->readImageBlob($svg); |
||
| 130 | $x = $tmp->getImageWidth(); |
||
| 131 | $y = $tmp->getImageHeight(); |
||
| 132 | $res = $tmp->getImageResolution(); |
||
| 133 | $tmp->destroy(); |
||
| 134 | |||
| 135 | if($x>$y) { |
||
| 136 | $max = $x; |
||
| 137 | } else { |
||
| 138 | $max = $y; |
||
| 139 | } |
||
| 140 | |||
| 141 | // convert svg to resized image |
||
| 142 | $appIconFile = new Imagick(); |
||
| 143 | $resX = (int)(512 * $res['x'] / $max * 2.53); |
||
| 144 | $resY = (int)(512 * $res['y'] / $max * 2.53); |
||
| 145 | $appIconFile->setResolution($resX, $resY); |
||
| 146 | $appIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
||
| 147 | $appIconFile->readImageBlob($svg); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * invert app icons for bright primary colors |
||
| 151 | * the default nextcloud logo will not be inverted to black |
||
| 152 | */ |
||
| 153 | if ($this->util->invertTextColor($color) |
||
| 154 | && !$appIcon instanceof ISimpleFile |
||
| 155 | && $app !== "core" |
||
| 156 | ) { |
||
| 157 | $appIconFile->negateImage(false); |
||
| 158 | } |
||
| 159 | $appIconFile->scaleImage(512, 512, true); |
||
| 160 | } else { |
||
| 161 | $appIconFile = new Imagick(); |
||
| 162 | $appIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
||
| 163 | $appIconFile->readImageBlob($appIconContent); |
||
| 164 | $appIconFile->scaleImage(512, 512, true); |
||
| 165 | } |
||
| 166 | // offset for icon positioning |
||
| 167 | $border_w = (int)($appIconFile->getImageWidth() * 0.05); |
||
| 168 | $border_h = (int)($appIconFile->getImageHeight() * 0.05); |
||
| 169 | $innerWidth = (int)($appIconFile->getImageWidth() - $border_w * 2); |
||
| 170 | $innerHeight = (int)($appIconFile->getImageHeight() - $border_h * 2); |
||
| 171 | $appIconFile->adaptiveResizeImage($innerWidth, $innerHeight); |
||
| 172 | // center icon |
||
| 173 | $offset_w = 512 / 2 - $innerWidth / 2; |
||
| 174 | $offset_h = 512 / 2 - $innerHeight / 2; |
||
| 175 | |||
| 176 | $finalIconFile = new Imagick(); |
||
| 177 | $finalIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
||
| 178 | $finalIconFile->readImageBlob($background); |
||
| 179 | $finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT); |
||
| 180 | $finalIconFile->setImageArtifact('compose:args', "1,0,-0.5,0.5"); |
||
| 181 | $finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h); |
||
| 182 | $finalIconFile->setImageFormat('png24'); |
||
| 183 | if (defined("Imagick::INTERPOLATE_BICUBIC") === true) { |
||
| 184 | $filter = Imagick::INTERPOLATE_BICUBIC; |
||
| 185 | } else { |
||
| 186 | $filter = Imagick::FILTER_LANCZOS; |
||
| 187 | } |
||
| 188 | $finalIconFile->resizeImage($size, $size, $filter, 1, false); |
||
| 189 | |||
| 190 | $appIconFile->destroy(); |
||
| 191 | return $finalIconFile; |
||
| 192 | } |
||
| 193 | |||
| 211 |