Hi I've this simple php script for showing images from a folder.My problem is that images are displayed very closely .I want to add some space between images and border for each image.Pls help me.
$files = glob("images/gallery/thumb/*.*");
for ($i=1; $i<count($files); $i++)
{
$image = $files[$i];
echo '<img src="'.$image .'" alt="Random image" />';
}
Or, if you want a sort of grid:
$files = glob("images/gallery/thumb/*.*");
$count = count($files);
for($i = 1; $i < $count; $i++){
$image = $files[$i];
echo '<img src="'.$image .'" alt="Random image" style="border:2px solid black; margin: 5px; float: left;" />';
}
Use plain HTML line break <br/> for the space between two images.
Use CSS style="border:2px solid black;" (change it to suit your need) for the border.
And I’ll suggest you to store count($files) in a variable; otherwise it’ll have to be evaluated in every iteration of your for loop, causing your script to run slower.
Now your entire code will be something like the following:
$files = glob("images/gallery/thumb/*.*");
$count = count($files);
for($i = 1; $i < $count; $i++){
$image = $files[$i];
echo '<img src="'.$image .'" alt="Random image" style="border:2px solid black;"/><br/>';
}
I would attach a class to to the images.
echo '<link rel="stylesheet" href="gallery.css" type="text/css"';
...
$files = glob("images/gallery/thumb/*.*");
foreach ($files as $image){
echo '<img src="'.$image .'" alt="Random image" class="galleryImage"/>';
}
and then create a .css file with the style for the class. This will allow you to easily change the style via .css without having to touch your php code, and will make your rendered html file slightly smaller.
gallery.css
img.galleryImage{
border: 2px solid #888888;
padding: 10px;
}
Related
I have a script that gets images from a database and displays them on exploretest.php page. I need each image to have a separate download button under each image which allows people to download that image but am unsure on how to go about this. Any help would be appreciated.
<?php
display();
function display(){
$conn = mysqli_connect("localhost","root","","loginsystem");
$sql="select * from testimage";
$query=mysqli_query($conn, $sql);
$num=mysqli_num_rows($query);
for ($i=0; $i < $num; $i++) {
$result=mysqli_fetch_array($query);
$img=$result['image'];
echo '<img class="exploreimg" src="data:image;base64, '.$img.'" style="max-height:300px;max-width:300px; margin-left:50px; margin-right:50px; padding-bottom:20px; padding-top:30px;">';
}
}
?>
echo '<img src="img/downloadbutton.png" alt="Download" style="max-height:50px; max-width: 100px; float: center; line-height: 10; "/>';
I have a question: I have used the following queries to get my arrays from Database:
$news = $db->QueryFetchArrayAll("SELECT login,id FROM `users` ORDER BY id DESC LIMIT 9");
$imgs = $db->QueryFetchArrayAll("SELECT usrpic,id FROM `users` ORDER BY id DESC LIMIT 9");
Now i need $news['login'] and $imgs['usrpic'] in one for each loop.
For example:
foreach($news as $new, $imgs as $img){
<img style="border: 1px solid #8C0E0E;"src="'.$img['usrpic'].'" width="20" height="20" border="0" />
<img style="border: 1px solid #8C0E0E;"src="'.$new['login'].'" width="20" height="20" border="0" />
}
Can anyone help me how i can do this ?
You can use next() function : http://php.net/manual/en/function.next.php
but don't forget to make sure news and img have same size :
if(count($news) == count($imgs)) {
foreach($news as $new) {
$img = next($imgs);
<img style="border: 1px solid #8C0E0E;"src="'.$img['usrpic'].'" width="20" height="20" border="0" />
<img style="border: 1px solid #8C0E0E;"src="'.$new['login'].'" width="20" height="20" border="0" />
}
}
If those are your actual queries, you should combine them into one:
$users = $db->QueryFetchArrayAll("SELECT id, login, usrpic FROM `users` ORDER BY id DESC LIMIT 9");
Then iterate the result array and output the images:
foreach ($users as $user) {
echo "<img src='{$user['usrpic']}' ... />";
echo "<img src='{$user['login']}' ... />";
}
Otherwise (keeping your original queries), you should make sure $news and $imgs have the same length, and then iterate one of them:
$size = count($imgs);
for ($i = 0; $i < $size; $i++) {
$img = $imgs[$i];
$new = $news[$i];
echo "<img src='{$img['usrpic']}' ... />";
echo "<img src='{$new['login']}' ... />";
}
The problem can be shown here. I was experimenting with thousands of fixed div boxes to create a randomly generated background. I am aware this has a big impact on performance.
However, I was wondering if there was any solution to the strange white lines in the background of my webpage. I am almost 100% certain there is nothing wrong with my php & css that is determining where the boxes are placed in my background but, here it is just in-case.
define('ROWS', 100);
define('COLUMNS', 100);
$boxes = array();
for($i = 0; $i < ROWS; $i++) {
$boxes[] = array();
for($j = 0; $j < COLUMNS; $j++) {
$boxes[$i][$j] = randColor();
?>
#back<?php echo $i*COLUMNS + $j; ?> {
background: #<?php echo $boxes[$i][$j]; ?>;
width: <?php echo 100.0/COLUMNS ?>%;
height: <?php echo 100.0/ROWS ?>%;
left: <?php echo $j * 100.0/COLUMNS?>%;
top: <?php echo $i * 100.0/ROWS ?>%;
position: fixed;
z-index: -300;
}
Try instead using <canvas> and split the canvas up in pieces instead of divs. It is a rectangular area in a HTML file on which you can draw anything including parts.
Example: Draw a Circle
Javascript
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
Html
<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">
</canvas>
I wish to place an image tag within a PHP array, dynamically fed from a database. The array will eventually render directly onto a page.
What is the correct syntax for the image tag within the array; i.e. once the array is rendered on a page, the image tag should show as an image and not as simple text.
Perhaps it might be easier if I show you what I mean:
foreach($rows as &$row)
{
$this->_rows[] = array(
'thumnail' => '<img style="width: 180px; height: 80px;" src="<?= THUMBNAILn_Assets::getProductThumbnail($row->id) ?>" />',
);
};
The image tag renders as simple text on the page, as opposed to an image.
Try this :
foreach($rows as &$row)
{
$this->_rows[] = array(
'thumnail' => '<img style="width: 180px; height: 80px;" src="'. THUMBNAILn_Assets::getProductThumbnail($row->id) .'" />',
);
}
No need of php tags (<?= and ?>) again inside the src attribute
foreach($rows as &$row)
{
$t_pic['thumnail'] = THUMBNAILn_Assets::getProductThumbnail($row->id);
$this->_rows[] = $t_pic;
}
or
$t_pic['thumnail'] = '<img style="width: 180px; height: 80px;" src="'. THUMBNAILn_Assets::getProductThumbnail($row->id) .'" />';
just make it more clear and simple, its not good practice to store the html tags in php variables
How do I get images in certain fixed size, example I've attached the image and codes for your review
Please click on this link (image) to understand my question
<img src="<?php echo $picfile; ?>" border="0" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>" align="left" style="border:1px solid black;margin-right:5px;">
You can get image details with
$details = get_image_size($filename).
$details[3] will contain the width and height in html format ready for you to use.
Could possibly try this
img src='$filename' style='width:250px; height: 250px'
allows you to strech image to specific size
to scale you could use
img src='$filename' style='width:auto; height: 250px'
There are a few ways to do this. As you are looping through your images, you want to keep track of the maximum width.
$newImageArray = array();
$maxWidth = 0;
$maxHeight = 0;
$i = 0;
forEach ( $imageArray as $picfile ) {
$newImageArray[$i]['picFile'] = $picfile;
$imgsize = get_image_size($picfile);
$newImageArray[$i]['width'] = $imgsize[0];
if ( $imgsize[0] > $maxWidth ) {
$maxWidth = $imgsize[0];
}
$newImageArray[$i]['height'] = $imgsize[1];
if ( $imgsize[1] > $maxHeight ) {
$maxHeight = $imgsize[1];
}
$1++;
}
forEach ( $newImageArray as $i) { ?>
<img src="<?php echo $picfile; ?>" border="0" width="<?php echo $maxWidth; ?>" height="<?php echo $maxHeight; ?>" align="left" style="border:1px solid black;margin-right:5px;">
<?php
}
Now you shouldn't really be doing this. A CSS based option would work better. You can add a wrapper container with a width on it and set the images to display:block;width:100%; and the images will always fill the space. I will edit with that solution shortly.
This will keep the image within a fixed width, and scale the image to fit inside of the box when it is too large.
HTML:
<div class="imgWrap">
<img src="http://www.captainangry.com/img/1236647133510anonib.jpg">
</div>
CSS:
.imgWrap {
width:100px;
height:100px;
float:left;
}
.imgWrap img {
display:block;
max-width:100%;
}
If you don't want to do any work in PHP then just wrap the image in a div and set overflow:hidden. This assumes that you know the height you want all of your images to be.
<div style="width:<?php echo $imgsize[0]; ?>; height:100px; overflow:hidden; border:1px solid black; margin-right:5px">
<img src="<?php echo $picfile; ?>" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>">
</div>