Hướng dẫn dùng imagesize trong PHP

Asked 4 years, 3 months ago

Viewed 616 times

I try to put image size in image src tag, but it still does not work. The image can be displayed fine.

<?php
$files = glob("uploads/*.*");

echo "<table border =\"1\" style='border-collapse: collapse'>";

for ($row=1; $row <= 4; $row++) { 
    echo "<tr> \n";
    for ($col=1; $col<=4; $col++) { 

       $f=$f+1;
       $getfile = $files[$f]; 
       echo "<td>";
       echo "<img src=$getfile  > ";
       echo "</td>";
        }
    echo "</tr>";
    }
    echo "</table>";

Nigel Ren

54.6k11 gold badges39 silver badges53 bronze badges

asked Jun 12, 2018 at 6:13

4

You can use getimagesize() function http://php.net/manual/en/function.getimagesize.php to fetch image size

<?php 
    $files = glob("uploads/*.*");
    echo "<table border =\"1\" style='border-collapse: collapse'>";

    for ($row=1; $row <= 4; $row++) {
        echo "<tr> \n";
        for ($col=1; $col<=4; $col++) {

            $f=$f+1;
            $getfile = $files[$f];
            $size = getimagesize($getfile);
            echo "<td>";
            echo "<img src=$getfile  $size[3]> ";
            echo "</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
    ?>

answered Jun 12, 2018 at 6:20

Bhumi ShahBhumi Shah

8,9617 gold badges58 silver badges96 bronze badges

3

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

I am echoing an image which works fine, but then i want to set how big it needs to be, and I just can't seem to get the syntax right.

This is what I have:

echo "<img src='" . $row['imglink'] . "' height="130" width="150"> ";

What am I doing wrong? If I remove the height and width it works fine.

cyborg86pl

2,6082 gold badges26 silver badges44 bronze badges

asked Apr 22, 2014 at 15:43

0

You are mixing single and double quotes. So you get a syntax error.

This works:

echo "<img src='" . $row['imglink'] . "' height='130' width='150'> ";

If you like to output double quotes you have to escape:

echo "<img src=\"" . $row['imglink'] . "\" height=\"130\" width=\"150\"> ";

Or you switch to cover the string in single quotes:

echo '<img src="' . $row['imglink'] . '" height="130" width="150"> ';

answered Apr 22, 2014 at 15:46

theHackertheHacker

3,9341 gold badge18 silver badges30 bronze badges

try changing double quotes into single quotes for these attributes

height='130' width='150'

since double quotes are actually delimiting the PHP string. Optionally you may instead escape the double quotes like so

height=\"130\" width=\"150\"

but this syntax is less readable and more error prone (not recommended)

answered Apr 22, 2014 at 15:45

Fabrizio CalderanFabrizio Calderan

117k25 gold badges164 silver badges168 bronze badges

ok its here

echo '<img src="'$row['imglink']'" height="130" width="150">';

answered Apr 22, 2014 at 15:45

4

You can uses variables inside double quotes, like this:

<?
    $img_url = $row['imglink'];
    echo "<img src='$img_url' height='130' width='150'>";
?>

answered Apr 22, 2014 at 15:50

Pedro LobitoPedro Lobito

88.2k29 gold badges238 silver badges256 bronze badges

Sometimes the way that you're using causes to errors if you don't do it properly.Therefore you can try this way also.

<img src="<?php echo $row['imglink']?>" height="130" width="150" />

if you're using this inside a loop,you can do like this.

<?php foreach($result as $rows):?>
<img src="<?php echo $row['imglink']?>" height="130" width="150" />
<?php endforeach; ?>

Hope this helped to you.

answered Apr 22, 2014 at 15:52

CodeCanyonCodeCanyon

9191 gold badge11 silver badges35 bronze badges

Not the answer you're looking for? Browse other questions tagged php html syntax or ask your own question.