Jump to content

how can i Echo "text" only if attached thumbnail resolution is over 500px by 500px in wordpress?


Go to solution Solved by adamsevera,

Recommended Posts

Hello,

 

can someone please help me out with the following.

 

how can i Echo "text" only if attached thumbnail resolution is over 500px x 500px in wordpress?

 

I want to echo a link if the attached thumbnail is bigger than the specified image resolution.

 

Thanks.

 

I don't use wordpress, so the actual implementation is up to you. But, you can check the size of an image using the function getimagesize().

 

Example

list($width, $height) = getimagesize('path/to/image/imagename.jpg');
if($width>500 || $height>500)
{
    echo "Image is greater than 500 x 500";
}

i Tried the following it returns "list($width, $height)"

 

$img_url is hotlink for full size thumbnail image

<?php
list($width, $height) = getimagesize('$img_url');
if($width>500 || $height>500)
{echo "Image is greater than 500 x 500";}
?>

 

I don't use wordpress, so the actual implementation is up to you. But, you can check the size of an image using the function getimagesize().

 

Example

list($width, $height) = getimagesize('path/to/image/imagename.jpg');
if($width>500 || $height>500)
{
    echo "Image is greater than 500 x 500";
}

One caveat. If what you have in your template is something like this:

 

<img src="/path/to/helper.php">
Then this is not going to work. The template itself would need the code to determine what to output.

This line of code is invalid

list($width, $height) = getimagesize('$img_url');

If you put a variable in a double quoted string it will be parsed as the value of the variable. But, if you put a variable in a single quoted string, as shown above, the characters of the variable name will be interpreted as those literal characters!

 

Example:

$img_url = "\path\to\image\imagename.jpg";
 
echo "$img_url"; //Output: \path\to\image\imagename.jpg
echo '$img_url'; //Output: $img_url
 

In your usage, no quotes are even needed, just use this

list($width, $height) = getimagesize($img_url);
Edited by Psycho
  • Solution

Found a Solution

<?

$imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
$imgurl = $imgdata[0]; // the url of the thumbnail picture
$imgwidth = $imgdata[1]; // thumbnail's width
$imgheight = $imgdata[2]; // thumbnail's height

?>

<? if ($imgwidth >= 500 || $imgheight >= 500){ echo "TEXT"; } ?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.