| Total Complexity | 55 |
| Total Lines | 240 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 2 | Features | 0 |
Complex classes like ImageManipulations often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImageManipulations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class ImageManipulations |
||
| 15 | { |
||
| 16 | use Configurable; |
||
| 17 | use Injectable; |
||
| 18 | |||
| 19 | private static $webp_enabled = true; |
||
| 20 | |||
| 21 | private static $webp_quality = 77; |
||
| 22 | |||
| 23 | private static $imageLinkCache = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * work out the best image link. |
||
| 27 | * |
||
| 28 | * There are basically three options: |
||
| 29 | * a. if the height and/or width matches or is smaller than it should be |
||
| 30 | * then just return natural image |
||
| 31 | * b. if crop is set to true then Fill |
||
| 32 | * c. otherwise resize Height/Width/Both |
||
| 33 | * |
||
| 34 | * @param Image $image |
||
| 35 | * @param null|bool $useRetina optional |
||
| 36 | * @param null|bool $forMobile optional |
||
| 37 | * @param null|int $resizeToWidth optional |
||
| 38 | */ |
||
| 39 | public static function get_image_link($image, string $name, ?bool $useRetina = null, ?bool $forMobile = null, ?int $resizeToWidth = 0): string |
||
| 40 | { |
||
| 41 | $cacheKey = $image->ClassName . '_' . $image->ID . '_' . $name . '_' . ($useRetina ? 'Y' : 'N') . '_' . ($forMobile ? 'MY' : 'MN'); |
||
| 42 | if (empty(self::$imageLinkCache[$cacheKey])) { |
||
| 43 | //work out perfect width and height |
||
| 44 | if (null === $useRetina) { |
||
| 45 | $useRetina = PerfectCMSImages::use_retina($name); |
||
| 46 | } |
||
| 47 | |||
| 48 | $crop = PerfectCMSImages::is_crop($name); |
||
| 49 | |||
| 50 | $multiplier = PerfectCMSImages::get_multiplier($useRetina); |
||
| 51 | $perfectWidth = PerfectCMSImages::get_width($name, true); |
||
| 52 | $perfectHeight = PerfectCMSImages::get_height($name, true); |
||
| 53 | |||
| 54 | if ($forMobile) { |
||
| 55 | $perfectWidth = PerfectCMSImages::get_mobile_width($name, true); |
||
| 56 | $perfectHeight = PerfectCMSImages::get_mobile_height($name, true); |
||
| 57 | } |
||
| 58 | |||
| 59 | $perfectWidth *= $multiplier; |
||
| 60 | $perfectHeight *= $multiplier; |
||
| 61 | |||
| 62 | //get current width and height |
||
| 63 | $myWidth = $image->getWidth(); |
||
| 64 | $myHeight = $image->getHeight(); |
||
| 65 | //if we are trying to resize to a width that is small than the perfect width |
||
| 66 | //and the resize width is small than the current width, then lets resize... |
||
| 67 | if (0 !== (int) $resizeToWidth) { |
||
| 68 | if ($resizeToWidth < $perfectWidth && $resizeToWidth < $myWidth) { |
||
| 69 | $perfectWidth = $resizeToWidth; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | $tmpImage = null; |
||
| 74 | if ($perfectWidth && $perfectHeight) { |
||
|
|
|||
| 75 | //if the height or the width are already perfect then we can not do anything about it. |
||
| 76 | if ($myWidth === $perfectWidth && $myHeight === $perfectHeight) { |
||
| 77 | $tmpImage = $image; |
||
| 78 | } elseif ($myWidth < $perfectWidth || $myHeight < $perfectHeight) { |
||
| 79 | $tmpImage = $image->Pad( |
||
| 80 | $perfectWidth, |
||
| 81 | $perfectHeight, |
||
| 82 | PerfectCMSImages::get_padding_bg_colour($name) |
||
| 83 | ); |
||
| 84 | } elseif ($crop) { |
||
| 85 | $tmpImage = $image->Fill($perfectWidth, $perfectHeight); |
||
| 86 | } else { |
||
| 87 | $tmpImage = $image->FitMax($perfectWidth, $perfectHeight); |
||
| 88 | } |
||
| 89 | } elseif ($perfectWidth) { |
||
| 90 | if ($myWidth === $perfectWidth) { |
||
| 91 | $tmpImage = $image; |
||
| 92 | } elseif ($crop) { |
||
| 93 | $tmpImage = $image->Fill($perfectWidth, $myHeight); |
||
| 94 | } else { |
||
| 95 | $tmpImage = $image->ScaleWidth($perfectWidth); |
||
| 96 | } |
||
| 97 | } elseif ($perfectHeight) { |
||
| 98 | if ($myHeight === $perfectHeight) { |
||
| 99 | $tmpImage = $image; |
||
| 100 | } elseif ($crop) { |
||
| 101 | $tmpImage = $image->Fill($myWidth, $perfectHeight); |
||
| 102 | } else { |
||
| 103 | $tmpImage = $image->ScaleHeight($perfectHeight); |
||
| 104 | } |
||
| 105 | } elseif ($forMobile) { |
||
| 106 | // todo: expplain this! |
||
| 107 | // basically, it is for mobile and there is not perfect height nor width |
||
| 108 | $tmpImage = null; |
||
| 109 | } else { |
||
| 110 | $tmpImage = $image; |
||
| 111 | } |
||
| 112 | if($tmpImage) { |
||
| 113 | $link = $tmpImage->Link(); |
||
| 114 | } |
||
| 115 | |||
| 116 | self::$imageLinkCache[$cacheKey] = (string) $link; |
||
| 117 | } |
||
| 118 | |||
| 119 | return self::$imageLinkCache[$cacheKey]; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * back-up image. |
||
| 124 | * |
||
| 125 | * @return null|Image |
||
| 126 | */ |
||
| 127 | public static function get_backup_image(string $name) |
||
| 128 | { |
||
| 129 | $image = null; |
||
| 130 | $backupObject = SiteConfig::current_site_config(); |
||
| 131 | if ($backupObject->hasMethod($name)) { |
||
| 132 | $image = $backupObject->{$name}(); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $image; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * placeholder image. |
||
| 140 | */ |
||
| 141 | public static function get_placeholder_image_tag(string $name): string |
||
| 142 | { |
||
| 143 | $multiplier = PerfectCMSImages::get_multiplier(true); |
||
| 144 | $perfectWidth = PerfectCMSImages::get_width($name, true); |
||
| 145 | $perfectHeight = PerfectCMSImages::get_height($name, true); |
||
| 146 | $perfectWidth *= $multiplier; |
||
| 147 | $perfectHeight *= $multiplier; |
||
| 148 | if ($perfectWidth || $perfectHeight) { |
||
| 149 | if (0 === $perfectWidth) { |
||
| 150 | $perfectWidth = $perfectHeight; |
||
| 151 | } |
||
| 152 | |||
| 153 | if (! $perfectHeight) { |
||
| 154 | $perfectHeight = $perfectWidth; |
||
| 155 | } |
||
| 156 | |||
| 157 | $text = "{$perfectWidth} x {$perfectHeight} /2 = " . round($perfectWidth / 2) . ' x ' . round($perfectHeight / 2) . ''; |
||
| 158 | |||
| 159 | return 'https://placehold.it/' . $perfectWidth . 'x' . $perfectHeight . '?text=' . urlencode($text); |
||
| 160 | } |
||
| 161 | |||
| 162 | return 'https://placehold.it/1500x1500?text=' . urlencode('no size set'); |
||
| 163 | } |
||
| 164 | |||
| 165 | public static function web_p_link(string $link): string |
||
| 204 | } |
||
| 205 | |||
| 206 | public static function add_fake_parts($image, string $link): string |
||
| 207 | { |
||
| 208 | // first get the timestamp |
||
| 209 | $time1 = strtotime($image->LastEdited); |
||
| 210 | $time2 = 0; |
||
| 211 | $path = Controller::join_links(Director::baseFolder(), PUBLIC_DIR, $link); |
||
| 212 | if(file_exists($path)) { |
||
| 213 | $time2 = filemtime($path); |
||
| 214 | } |
||
| 215 | |||
| 216 | // first convert to hash extension |
||
| 217 | if (class_exists('HashPathExtension')) { |
||
| 218 | /** @var null|Controller $curr */ |
||
| 219 | $curr = Controller::curr(); |
||
| 220 | if ($curr) { |
||
| 221 | if ($curr->hasMethod('HashPath')) { |
||
| 222 | $link = $curr->HashPath($link, false); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | // now you can add the time |
||
| 228 | $link .= '?time='.max($time1, $time2); |
||
| 229 | |||
| 230 | // finally add the title |
||
| 231 | if ($image->Title) { |
||
| 232 | $imageClasses = Config::inst()->get(PerfectCMSImages::class, 'perfect_cms_images_append_title_to_image_links_classes'); |
||
| 233 | if (in_array($image->ClassName, $imageClasses, true)) { |
||
| 234 | $link .= '&title=' . urlencode(Convert::raw2att($image->Title)); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | return $link; |
||
| 239 | } |
||
| 240 | |||
| 241 | public static function web_p_enabled(): bool |
||
| 254 | } |
||
| 255 | } |
||
| 256 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: