Image
2 days ago
Select
2 days ago
Arrays.php
2 days ago
AvailableTranslations.php
2 days ago
Creatable.php
2 days ago
Dashicon.php
2 days ago
Date.php
2 days ago
Html.php
2 days ago
Icon.php
2 days ago
Image.php
2 days ago
LocalFile.php
2 days ago
Mbstring.php
2 days ago
MediaIdResolver.php
2 days ago
Menu.php
2 days ago
Network.php
2 days ago
Post.php
2 days ago
Strings.php
2 days ago
Taxonomy.php
2 days ago
User.php
2 days ago
UserRoles.php
2 days ago
WpDateFormat.php
2 days ago
Image.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Helper; |
| 6 | |
| 7 | use AC\Helper\Image\Markup; |
| 8 | use AC\Helper\Image\SizeResolver; |
| 9 | |
| 10 | class Image extends Creatable |
| 11 | { |
| 12 | private function resize(string $file, int $max_w, int $max_h, bool $crop = false): ?string |
| 13 | { |
| 14 | $editor = wp_get_image_editor($file); |
| 15 | |
| 16 | if (is_wp_error($editor)) { |
| 17 | return null; |
| 18 | } |
| 19 | |
| 20 | $editor->set_quality(90); |
| 21 | |
| 22 | $resized = $editor->resize($max_w, $max_h, $crop); |
| 23 | |
| 24 | if (is_wp_error($resized)) { |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | $filename = $editor->generate_filename(); |
| 29 | $saved = $editor->save($filename); |
| 30 | |
| 31 | if (is_wp_error($saved)) { |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | return $filename; |
| 36 | } |
| 37 | |
| 38 | public function get_image_by_id(int $id, $size): ?string |
| 39 | { |
| 40 | if (! wp_get_attachment_url($id)) { |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | $attributes = wp_get_attachment_image_src($id, $size); |
| 45 | |
| 46 | // Is Image |
| 47 | if ($attributes) { |
| 48 | [$src, $width, $height] = $attributes; |
| 49 | |
| 50 | $pair = SizeResolver::create()->normalize_pair($size); |
| 51 | |
| 52 | if ($pair !== null) { |
| 53 | return Markup::create()->cover($src, $pair[0], $pair[1], $id); |
| 54 | } |
| 55 | |
| 56 | // In case of SVG |
| 57 | if (is_string($size) && 'svg' === pathinfo($src, PATHINFO_EXTENSION) && 'full' !== $size) { |
| 58 | $_size = SizeResolver::create()->get_sizes_by_name($size); |
| 59 | |
| 60 | $width = (int)($_size['width'] ?? 0); |
| 61 | $height = (int)($_size['height'] ?? 0); |
| 62 | } |
| 63 | |
| 64 | return $this->markup_image($src, (int)$width, (int)$height, $id); |
| 65 | } |
| 66 | |
| 67 | // Is File - render as pill instead of mime-type icon |
| 68 | if (wp_get_attachment_image_src($id, $size, true)) { |
| 69 | $filename = LocalFile::create()->get_file_name($id) ?? ''; |
| 70 | |
| 71 | return Markup::create()->file_pill($id, $filename); |
| 72 | } |
| 73 | |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | public function get_image_by_url(string $url, $size): ?string |
| 78 | { |
| 79 | $dimensions = SizeResolver::create()->get_dimensions($size); |
| 80 | |
| 81 | $width = $dimensions[0] ?? 0; |
| 82 | $height = $dimensions[1] ?? 0; |
| 83 | |
| 84 | $local = LocalFile::create(); |
| 85 | $image_path = $local->url_to_path($url); |
| 86 | |
| 87 | if (is_file($image_path)) { |
| 88 | // try to resize image if it is not already resized |
| 89 | if (! $this->is_resized_image($image_path)) { |
| 90 | $resized = $this->resize( |
| 91 | $image_path, |
| 92 | $width, |
| 93 | $height, |
| 94 | true |
| 95 | ); |
| 96 | |
| 97 | if ($resized) { |
| 98 | $src = $local->path_to_url($resized); |
| 99 | |
| 100 | return $this->markup_image($src, $width, $height); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return $this->markup_image($url, $width, $height); |
| 105 | } |
| 106 | |
| 107 | // External image |
| 108 | return Markup::create()->cover($url, $width, $height); |
| 109 | } |
| 110 | |
| 111 | public function get_image(string $image_id_or_url, $size = 'thumbnail'): ?string |
| 112 | { |
| 113 | if (! $image_id_or_url) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | if (is_numeric($image_id_or_url)) { |
| 118 | return $this->get_image_by_id((int)$image_id_or_url, $size); |
| 119 | } |
| 120 | |
| 121 | if (Strings::create()->is_image($image_id_or_url)) { |
| 122 | return $this->get_image_by_url($image_id_or_url, $size); |
| 123 | } |
| 124 | |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | public function get_local_image_info(string $url): ?array |
| 129 | { |
| 130 | $path = LocalFile::create()->get_path($url); |
| 131 | |
| 132 | if (! $path) { |
| 133 | return null; |
| 134 | } |
| 135 | |
| 136 | return getimagesize($path) ?: null; |
| 137 | } |
| 138 | |
| 139 | private function markup_image(string $src, int $width, int $height, ?int $media_id = null): string |
| 140 | { |
| 141 | $tooltip = $media_id |
| 142 | ? Html::create()->get_tooltip_attr(LocalFile::create()->get_file_name($media_id) ?? '') |
| 143 | : ''; |
| 144 | |
| 145 | return Markup::create()->image($src, $width, $height, $media_id, $tooltip); |
| 146 | } |
| 147 | |
| 148 | private function is_resized_image(string $path): bool |
| 149 | { |
| 150 | $fileinfo = pathinfo($path); |
| 151 | |
| 152 | return (bool)preg_match('/-[0-9]+x[0-9]+$/', $fileinfo['filename']); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @deprecated 7.1 Use AC\Helper\LocalFile::create()->get_file_name() instead. |
| 157 | */ |
| 158 | public function get_file_name(int $attachment_id): ?string |
| 159 | { |
| 160 | return LocalFile::create()->get_file_name($attachment_id); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @deprecated 7.1 Use AC\Helper\LocalFile::create()->get_file_extension() instead. |
| 165 | */ |
| 166 | public function get_file_extension(int $attachment_id): string |
| 167 | { |
| 168 | return LocalFile::create()->get_file_extension($attachment_id); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @deprecated 7.1 Use AC\Helper\LocalFile::create()->get_path() instead. |
| 173 | */ |
| 174 | public function get_local_image_path(string $url): ?string |
| 175 | { |
| 176 | return LocalFile::create()->get_path($url); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @deprecated 7.1 Use AC\Helper\LocalFile::create()->get_size() instead. |
| 181 | */ |
| 182 | public function get_local_image_size(string $url): ?int |
| 183 | { |
| 184 | return LocalFile::create()->get_size($url); |
| 185 | } |
| 186 | |
| 187 | } |
| 188 |