php - Set imagesize in <img></img>, when getting it from a db
Get the solution ↓↓↓Solution:
use this CSS:
img {
max-width: 500px;
}
or set awidth
on your td and make the images width 100%. make sure you only set either height or width, otherwise the image won't (automatically) keep it's ratio.
Answer
Solution:
{echo "<tr><td colspan='2' style='width:500px;height:500px;'><img src='http://xxxx.se/ok/eventbilder/" . $row['photo'] . "'></td></tr>";}
Answer
Solution:
you need adaptive scaling: this function takes your image path and returns $x and $y scaled accordingly. Here we assume a container box $w pixels in width and $h pixels in height
// get size
$size = getimagesize($your_image_file_path);
$x = $size[0];
$y = $size[1];
// container box
$w = 500;
$h = 500;
$ratio = $x / $y;
// resize image
if($x > $w)
{
$x = $w;
$y = floor($x / $ratio);
}
if($y > $h)
{
$y = $h;
$x = floor($y * $ratio);
}
Answer
Solution:
You can also give a class to<img>
like<img class="dbimg" src="from database"></img>
and give all styles to that class
.dbimg{
width:auto;
max-width:500px; //by only define width it will automatically takes up height as per its proportions
//other styling
}
and the advantage to use this approach is
1.you can make any no of img with one single class no need to write code every time
and
2.you can make many combinations if you have more images like that to show in different sizes
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function format() on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.