Blog
1 year ago
SDApi
1 year ago
Shop
1 year ago
admin-tab
1 year ago
filters
1 year ago
PostImage.php
1 year ago
api.php
1 year ago
index.php
1 year ago
utils.php
1 year ago
PostImage.php
64 lines
| 1 | <?php |
| 2 | namespace Kubio\AI; |
| 3 | |
| 4 | use IlluminateAgnostic\Arr\Support\Arr; |
| 5 | use Kubio\Core\Importer; |
| 6 | |
| 7 | class PostImage { |
| 8 | |
| 9 | static $images_by_keword = array(); |
| 10 | static $used_images = array(); |
| 11 | public static function get_featured_image_from_keywords( $keywords ) { |
| 12 | $image_url = self::get_image_from_api( $keywords ); |
| 13 | |
| 14 | if ( $image_url ) { |
| 15 | $image = Importer::importRemoteFile( |
| 16 | $image_url |
| 17 | ); |
| 18 | |
| 19 | if ( $image ) { |
| 20 | return $image['id']; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | private static function get_image_from_api( $keywords ) { |
| 28 | $cached_images = Arr::get( static::$images_by_keword, $keywords ); |
| 29 | if ( $cached_images && is_array( $cached_images ) && count( $cached_images ) > 0 ) { |
| 30 | $next_image = array_shift( $cached_images ); |
| 31 | while ( in_array( $next_image, static::$used_images ) && count( $cached_images ) > 0 ) { |
| 32 | $next_image = array_shift( $cached_images ); |
| 33 | } |
| 34 | Arr::set( static::$images_by_keword, $keywords, $cached_images ); |
| 35 | return $next_image; |
| 36 | } |
| 37 | $per_page = 5; |
| 38 | $image = kubio_ai_call_api( |
| 39 | 'v1/search-media', |
| 40 | array( |
| 41 | 'type' => 'image', |
| 42 | 'search' => $keywords, |
| 43 | 'per_page' => $per_page, |
| 44 | 'orientation' => 'landscape', |
| 45 | 'size' => 'small', |
| 46 | 'width' => '1152', |
| 47 | 'height' => '896', |
| 48 | ) |
| 49 | ); |
| 50 | $images = Arr::get( $image, 'content.items', array() ); |
| 51 | $next_image = null; |
| 52 | if ( is_array( $images ) && count( $images ) > 0 ) { |
| 53 | $next_image = array_shift( $images ); |
| 54 | while ( in_array( $next_image, static::$used_images ) && count( $images ) > 0 ) { |
| 55 | $next_image = array_shift( $images ); |
| 56 | } |
| 57 | static::$used_images[] = $next_image; |
| 58 | Arr::set( static::$images_by_keword, $keywords, $images ); |
| 59 | } |
| 60 | |
| 61 | return $next_image; |
| 62 | } |
| 63 | } |
| 64 |