Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #429269

    Hi, I am trying to setup the theme and have pretty much everything working how I want except for two things

    First I am working in the single.php file.

    1. I am trying to call the MIME type of the image so the user can see what format it is in before they download it. THis is what I had on one of the default themplates that comes with WP and it worked great. It doesn’t work in Expose though(assuming it is being called a different way).

    <?php get_post_mime_type();
    switch (get_post_mime_type()) { 
        case "image/gif": 
            echo "GIF"; 
            break; 
        case "image/jpeg": 
            echo "JPEG"; 
            break; 
        case "image/png": 
            echo "PNG"; 
            break; 
        case "image/bmp": 
            echo "BMP"; 
            break; 
    } 
     ?>

    2. How does this template call the image and display it? I am setting up a download link and can’t figure out how to code it so it pulls the main image or attachment at the top of the page and insert it in for the download link. If that doesn’t make since just let me know.

    Thank you,
    Chris

    • This topic was modified 4 years ago by  Chris.
    #429277

    Also I was trying to move the star ratings below the image but above the EDIT link. Any ideas? I moved the code but it keeps placing the star ratings to the left of the image and not where the code is.

    #430615

    Anyone have any ideas?

    #430625

    I resolved #2 above myself. Still working on the other parts though.

    #431786

    Hey!

    Sorry for the delay and thank you for using the theme.

    How did you add the image? Is that a custom field? In the single.php file, you can add something like this:

    $image = get_post_thumbnail_id($post->ID);
        $type =  get_post_mime_type( $image );
        $mime_type = explode('/', $type);
        $type = '.'.$mime_type['1'];
        echo $type;

    You need to add a featured image in the post. Before you move forward, note that the Expose theme is a bit outdated so it’s better if you pick another theme like Enfold.

    Cheers!
    Ismael

    #433050

    Thank you! I added it and got it working.

    One question and I know it is probably being picky but how would I make the type all caps instead of lowercase?

    Which image are you asking about specifically?

    Sorry but yes some of those are custom fields. Like the one showing 150×150 or the image size and the file size example 6kB. I wanted them to be coded but couldn’t figure it out so used custom fields. Would that be easy to do?

    • This reply was modified 3 years, 12 months ago by  Chris.
    #433152

    Hi!

    You need to set Featured image in your custom post – https://codex.wordpress.org/Post_Thumbnails

    Cheers!
    Yigit

    #433154

    Not sure I am following you here. I have custom fields which I am trying to not use but not a custom posts? Also all my posts have a featured image on them. I did that earlier.

    • This reply was modified 3 years, 12 months ago by  Chris.
    #435127

    Hey!

    If you want the file name all caps, just replace the code with this:

    $image = get_post_thumbnail_id($post->ID);
        $type =  get_post_mime_type( $image );
        $mime_type = explode('/', $type);
        $type = strtoupper($mime_type['1']);
        echo $type;

    You can keep the custom field but you need to add the same image as featured image because the code need a featured image to work.

    Cheers!
    Ismael

    #435670

    Thanks Ismael. Appreciate that.

    Any idea on the file size in bytes and dimensions of the full size image? Pulling that from each image? That’s the last questions I have and will have everything I need.

    EDIT:

    I can get it to show the thumbnail size, but I am trying to pull the full size image dimensions.

    • This reply was modified 3 years, 11 months ago by  Chris.
    #435724

    OK, so I got the dimensions working right now.

    All I need to figure out is how to show the file size in bytes.

    #435762

    Here is the code I am working with. It displays a file size but it isn’t the correct size. The file is a 2MB file and this shows 934kB.

    Any ideas?

    <?php $upload_dir = wp_upload_dir();
    $metadata_size = image_get_intermediate_size( get_post_thumbnail_id(),
        'large'
    );
    $path_inter = $upload_dir[ 'basedir' ] . '/' . $metadata_size[ 'path' ];
    $bytes = filesize(
        $path_inter
    );
    $hr_size = size_format( $bytes ); ?>
    
    <?php echo $hr_size; ?>
    #435764

    Never mind I have figured it out! :)

    #435978

    Hey!

    Glad you figured it out. Mind sharing the fix? :)

    Regards,
    Ismael

    #436459

    Not at all. Here is how I chnaged the code above and it works great.

    <?php $upload_dir = wp_upload_dir();
    $metadata_size = image_get_intermediate_size( get_post_thumbnail_id(),
        'thumbnail'
    );
    $path_inter = $upload_dir[ 'basedir' ] . '/' . $metadata_size[ 'path' ];
    $bytes = filesize(
        get_attached_file(
            get_post_thumbnail_id()
        )
    );
    $hr_size = size_format( $bytes ); ?>
    
    <?php echo $hr_size; ?>
Viewing 15 posts - 1 through 15 (of 15 total)

The topic ‘MIME-type and image attachment link’ is closed to new replies.