set_post_thumbnail or media_sideload_image adds image multiple times in media library

<?php $post_id = get_queried_object_id(); $bla = “myImageURL”; // Set the image URL $image_url = $bla; // Check if the image already exists in the media library $existing_image = get_posts(array( ‘post_type’ => ‘attachment’, ‘meta_key’ => ‘_wp_attached_file’, ‘meta_value’ => sanitize_file_name(basename($image_url)) )); if (!$existing_image) { // Image does not exist in the media library, so download it $image_id … Read more

How to show file type of featured image?

If you want to show the File type of featured image. then try the below code. <?php $id1 = get_post_thumbnail_id($post->ID); $type = get_post_mime_type( $id1 ); $mime_type = explode(“https://wordpress.stackexchange.com/”, $type); $type=”.”.$mime_type[‘1’]; echo $type; ?> It will display the exact extension that you want “.jpeg” from “image/jpeg” Here “$post->ID” is current post id (or the id of … Read more

How to show file size of featured image? [duplicate]

Following function calculate file size, just you need change ‘/myfile/image.jpg’ with your featured image variable. <?php function file_size($url){ $size = filesize($url); if($size >= 1073741824){ $fileSize = round($size/1024/1024/1024,1) . ‘GB’; }elseif($size >= 1048576){ $fileSize = round($size/1024/1024,1) . ‘MB’; }elseif($size >= 1024){ $fileSize = round($size/1024,1) . ‘KB’; }else{ $fileSize = $size . ‘ bytes’; } return $fileSize; … Read more

Orientation of featured image in post?

Basic check of aspect ratio. Display as landscape, square or portrait if ( !has_post_thumbnail()) { return ‘Error’; } else { $image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ”); $image_w = $image[1]; $image_h = $image[2]; if ($image_w > $image_h) { echo ‘landscape’; } elseif ($image_w == $image_h) { echo ‘square’; } else { echo ‘portrait’; } }