Search code examples
phpdrupal-7

Retrieving resolution of an image and print along with it


I have a custom php code to print a list of images in drupal. Here is my code for this functionality.

<?php
$dir = drupal_get_path('module', 'cproduct') . '/tdesignAPI/images  
 /Images';

$files1 = scandir($dir);

foreach ($files1 as $value) {
    if (strpos($value,'.png') !== false) {

    $p3 = drupal_get_path('module', 'cproduct') . '/tdesignAPI/images/Images/';?>

    <div class="sample_icons"><img src="<?php echo $p3 .$value;?>" width="100%" height="100%" /></div>
    <?php 
    }
    elseif(strpos($value,'.') === false) {
    //echo '<div class="sample_icons"><img src="tdesignAPI/images/folder.png" width="100%" height="100%" />' .$value. '</div>' ;
    }
        //echo "Value: $value<br />\n";
 }
 ?>

My requirement is to retrieve the resolution or size of the images listed and print it with each image. How can I achieve that?

Thanks in advance


Solution

  • Use getimagesize() function.

    Try

    foreach ($files1 as $value)
    {
        if (strpos($value,'.png') !== false)
        {
            $image_resolution = getimagesize($dir.$value);
            $image_resolution = $image_resolution[3];
    
        $p3 = drupal_get_path('module', 'cproduct') . '/tdesignAPI/images/Images/';
        ?>
            <div class="sample_icons"><img src="<?php echo $p3 .$value;?>" <?php echo $image_resolution?> /></div>
        <?php 
        }
        elseif(strpos($value,'.') === false)
        {
        //echo '<div class="sample_icons"><img src="tdesignAPI/images/folder.png" width="100%" height="100%" />' .$value. '</div>' ;
        }
            //echo "Value: $value<br />\n";
    }