Jump to content

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


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);

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"; } ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.