Actually i'm using a simple PHP snippet to iterate through a directory to grab images and insert them into the html markup.
Now im facing the problem that all images are oriented in landscape. So also portrait images are oriented as landscape images.
Here's my snippet:
<?php
$projectpath = 'Projects';
$projects = glob($projectpath . '/*' , GLOB_ONLYDIR);
foreach($projects as $key=>$value): ?>
<section class="project">
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="project-images">
<?php
$handle = opendir(dirname(realpath(__FILE__)).'/'.$value);
while($file = readdir($handle)){
if($file !== '.' && $file !== '..'){
echo '<img class="img-responsive" alt="'. $file .'" src="'. $value .'/'. $file .'"/>';
}
}
?>
</div>
</div>
</div>
</div>
</section>
<?php endforeach; ?>
So what do i'm missing here? Any suggestions?
I doubt those images really have different orientations. More likely all images are in landscape layout (greater width than height), so the visualization is correct from a technical point of view.
I guess you usually look at the images with some viewer on your PC which auto rotates the images based on some orientation flag stored inside the images meta data. But such flag does not change the real, technical orientation (or better resolution). Instead such flag is only a hint for visualization of the images. Your project does not consult that flag, that is why your images appear to be rotated, whilst in reality they are not.
So the solution is one of two options:
rotate the images prior to using them inside your project, so that their real, technical orientation is correct and the images only have to be displayed without any additional rotation.
use js or php or whatever to detect that orientation flag and evaluate it. Based on the outcome you can set some css class which results in a css based rotation of those images that need to be rotated for visualization.
Here is simple PHP example to check if the image is portrait or landscape:
<?php
$imagePath = 'images/imagename.png';
$imageSize = getimagesize($imagePath); //returns an array
$imageWidth = $imageSize[0]; // array key 0 is width
$imageHeight = $imageSize[1]; // array key 1 is height
if($imageWidth < $imageHeight){
echo '<img class="ROTATE">'//add the rotate class
} elseif($imageWidth > $imageHeight){
echo '<img>'//do not add the rotate class
} else {
echo '<img>'//do not add the rotate class
?>
Take in mind that this is only an example and that you'll have to tweak the script to work properly for you.
I need to get the height and width of an image in order to set stretch it according to the size of the div. I could have used max-width:100% and max-height:100% but there are some images that are smaller than the div so I decided I would just manually assign the width and height.
I prefer to get these dimensions in php imagesy() and imagesx().
<?php $h=imagesy($list['Image1']);
$ w = imagesy($list['Image1'])?>
<img src="<?php echo $list['Image1']; ?>"
<?php if($h>$w) echo"style='height:100%;'";
else echo"style='width:100%;'"; ?> />
PHP functions like getimagesize() will return the dimensions of an image. But you are first going to have the load the image into the function. In other words you're going to have to fetch the image first.
If you have allow_url_fopen enabled then you should be able to invoke it with an absolute URL:
$size = getimagesize("http://www.example.com/gifs/logo.gif");
Okay, I was able to read just now that getimagesize returns an array with the width at [0] and height at [1]. So I was still able to compare them directly, hence, dynamically resizing them to fit the div containing the image. ;)
<?php $h=(getimagesize($list['Image1']));?>
<img src="<?php echo $list['Image1']; ?>"
style="<?php
if($h[0]>$h[1])
{
echo'width:100%;margin:0 auto;';
}
else
{
echo'height:100%;vertical-alignment:auto;';
} ?>"
alt="<?php echo $list['Image1']; ?>" />
I am using the jQuery Flexslider plugin in order to display a little over 100 images. I have gotten them all to display using a foreach loop in php as seen below:
<?php foreach (glob('images/glob/*') as $filename): ?>
<li> <img src="<?= $filename ?>"/> </li>
<?php endforeach; ?>
My images all have the same height in 600px, but their widths are varying. Some are portrait images while others are landscape. I'm wondering if there is a way in php that I can add classes to style the two accordingly based on their widths.
For example:
if
( $img width > $img height ).addClass (landscape)
else
( $img height > $img width ).addClass (portrait)
Obviously that statement above won't work but can someone please show me how to add that into my foreach statement so I can add classes to the varying images?
Use getimagesize: http://www.php.net/manual/en/function.getimagesize.php
foreach (glob('images/*') as $filename) {
$size = getimagesize($filename);
if ( $size[0] > $size[1] ) {
$class="landscape";
}
else {
$class="portrait";
}
print '<li> <img src="'. $filename.'" '.$size[3].' class="'.$class.'" /> </li>';
}
$size[3] being there to add proper width= and height= attributes to img element.
I'm working on a website with an image grid with infinite scrolling. For an image grid I'm using Final Tiles Grid Gallery plugin, which has an infinite scrolling feature and works as follows:
It requires PHP file with html structure of elements that should be added when users reaches certain part of the web page
Part of get-images.php:
<div class="tile">
<a class="tile-inner" href="photos/1.jpg">
<img class="item" src="images/3235535.jpg" />
</a>
Now, when certain part of page is reached the plugin calls ajax function to add more pictures. What I'm trying to achieve is automating the process of adding the code to the PHP file. I have too many images to add them all manually. I have very little knowledge in PHP and I found this little piece of code on stackoverflow to help me out:
<?php
$dir = "images/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
?>
<img src="images/<?php echo $images[$i]; ?>" alt="" />
Now, how do I combine those to achieve my goal? Also, I am planning to append the link to every picture added and the link should correspond with the name of the image (For example the name of image is "12345", then the link should be "abc.com/12345." I know I am asking too much and the second part of my question is completely on me to figure out but I will appreciate any help. Thanks.
Try it:
<?php
$count = 20;
function fetch_rand()
{
$images = glob('images/*.*');
$rand = array_rand($images);
return $images[$rand];
}
$my_list = array();
$i = 0;
while($i<$count)
{
$select = fetch_rand();
if(!in_array($select,$my_list))
{
array_push($my_list,$select);
$i++;
}
}
//print_r($my_list); //just for check
foreach($my_list as $image)
{
$image_link = explode('.',end(explode('/',$image)));
$image_link = $image_link[0];
//echo $image_link; //just for check
?>
<div class="tile">
<a class="tile-inner" href="abc.com/<?php echo $image_link; ?>">
<img class="item" src="<?php echo $image; ?>" />
</a>
</div>
<?php
}
?>
it will select random file from your image directory and never show duplicated.
Note: change $count value to your prefer value and image directory address in fetch_rand function.
I have a background image with object of some shape (example below). I would like to add new images - layers (simple div with position:absolute - left/top) to this image and only on the shape where I want.
And then with PHP code added images (for example 10, 50,..) to only this shape and no other place:
Is this possible to do on any simple way with PHP/JS/jquery/...? I just need to pass how many items and that many images is added to that area...
Here's a complete rewrite of my answer.
Given a random picture, fit many smaller pictures only where some color is matched:
I decided to go with a crab as a starting pictures, because crabs are cool:
I only want to add red dots where there is blue in the picture
To do this, I will split my answer in 3 sections:
HTML
I start with a very simple HTML file:
<html>
<head>
<title>Crab Example</title>
</head>
<body>
<div>
<h1>Dots on the crab example</h1>
</div>
<div id="crabSection">
<img src="crab.png">
</div>
</body>
</html>
This gives us a starting point for rendering our dotted crab!
PHP
Now, in PHP, I open both my crab.png and my dot.png to analyze their content, and locate random positions where the dot.png can fit in an all blue section. Right after the <img src="crab.png">, I inserted the following:
<?php
$crab = imagecreatefrompng("crab.png");
$dot = imagecreatefrompng("dot.png");
$numDesiredDots = 10;
$numCreatedDots = 0;
$crabWidth = imagesx($crab);
$crabHeight = imagesy($crab);
$dotWidth = imagesx($dot);
$dotHeight = imagesy($dot);
$spawnableWidth = $crabWidth - $dotWidth;
$spawnableHeight = $crabHeight - $dotHeight;
srand(time());
$testingForDotSubpart = imagecreatetruecolor($dotWidth, $dotHeight);
$validCoordinates = array();
$invalidCoordinates = array();
$colorWereLookingFor = 0xFF; // ARGB - our crab is blue
Here, a few details:
$numDesiredDots is hardcoded to 10, but it could easily be a parameter!
$spawnableWidth and $spawnableHeight represent the greatest coordinate a dot can be placed in without going out of the picture
srand(time()); is simply used to have a different random everytime
$testingForDotSubpart is a small image I will be using to test a given coordinate to see if it only contains pixels of the right color
$colorWereLookingFor is set to blue now, because my crab is blue, if you wanted red, it should be something like 0xFF0000. For this example I used the same PNG for the HTML and the image processing, but you could easily just create a mask for the image processing and have a full color image for the HTML.
Now, we need to create valid coordinates for each dot, which is done with the following php:
while($numCreatedDots < $numDesiredDots)
{
$randomX = rand() % $spawnableWidth;
$randomY = rand() % $spawnableHeight;
imagecopy($testingForDotSubpart, $crab, 0, 0, $randomX, $randomY, $dotWidth, $dotHeight);
$valid = true;
for($x = 0; $x < $dotWidth; $x++)
{
for($y = 0; $y < $dotHeight; $y++)
{
if(imagecolorat($testingForDotSubpart, $x, $y) != $colorWereLookingFor)
{
$valid = false;
break 2;
}
}
}
if($valid)
{
array_push($validCoordinates, array('x' => $randomX, 'y' => $randomY));
$numCreatedDots++;
}
else
{
// you can get rid of this else, it's just to show you how many fails there are
array_push($invalidCoordinates, array('x' => $randomX, 'y' => $randomY));
}
}
Again, some explanation:
We iterate as long as we haven't created all the dots we want, for a very complex image, this might take too much time, you could add a maximum number of tries
We start by creating a random X,Y coordinate
We copy the small window where the dot could end up
We test all the pixels inside this window to make sure they are of the right color
If the window is valid, we add the coordinates to an array
For debugging purposes, I added an $invalidCoordinates array to show how many tries failed - the more complex the picture, the more fails there will be
Now that we have computed all our positions, we need to clean up the resources:
imagedestroy($testingForDotSubpart);
imagedestroy($dot);
imagedestroy($crab);
Finally, I added some debug output that you can get rid of, but we need to output the dots on the crab! To show you that each dot is unique, I attached a JavaScript alert that shows the dot index:
echo "<p>Valid Coords: <br>";
foreach($validCoordinates as $coord)
{
echo "X: " . $coord['x'] . " Y: " . $coord['y'] . "<br>\n";
}
echo "<br>Invalid Coords " . count($invalidCoordinates) . "</p>\n";
// Now add the dots on the crab!
for($i = 0; $i < count($validCoordinates); $i++)
{
$coord = $validCoordinates[$i];
echo "<div class='dot' style='left:".$coord['x'].";top:".$coord['y'].";'><a href='javascript:alert(".$i.");'><img src='dot.png'></a></div>\n";
}
?>
Here, I am using the style left and top to give precise pixel positioning to the dots. To have them match precisely with the parent picture, we need to use position:relative; and position:absolute; as described in the next section.
CSS
As you can see, I'm using a class for the div, this is to take advantage of the relative positioning. I added at the top of the file, right after the title, the following
#crabSection { position:relative; }
.dot { margin: 0px 0px 0px 0px; position:absolute; }
Result
Here's a run of the given script... you could easily save the HTML generated so you don't have to recompute positions everytime:
Hope this helps!
Edit: here's the full code, if you need it: pastebin
Edit 2: note that there is no overlap checking, you might want to check that the rectangle defined by $randomX, $randomY, $randomX + $dotWidth and $randomY + $dotHeight doesn't overlap an existing rectangle from the coordinates in $validCoordinates.
Edit 3: Once generated you can simply open the source of the page, and copy the div to your HTML so it's not regenerated everytime.
<div class='dot' style='left:100;top:105;'><a href='javascript:alert(0);'><img src='dot.png'></a></div>
<div class='dot' style='left:150;top:151;'><a href='javascript:alert(1);'><img src='dot.png'></a></div>
<div class='dot' style='left:128;top:73;'><a href='javascript:alert(2);'><img src='dot.png'></a></div>
<div class='dot' style='left:144;top:93;'><a href='javascript:alert(3);'><img src='dot.png'></a></div>
<div class='dot' style='left:164;top:91;'><a href='javascript:alert(4);'><img src='dot.png'></a></div>
<div class='dot' style='left:108;top:107;'><a href='javascript:alert(5);'><img src='dot.png'></a></div>
<div class='dot' style='left:22;top:101;'><a href='javascript:alert(6);'><img src='dot.png'></a></div>
<div class='dot' style='left:54;top:151;'><a href='javascript:alert(7);'><img src='dot.png'></a></div>
<div class='dot' style='left:32;top:121;'><a href='javascript:alert(8);'><img src='dot.png'></a></div>
<div class='dot' style='left:142;top:87;'><a href='javascript:alert(9);'><img src='dot.png'></a></div>
Also, as long as the color mask described by the $crab picture opened in PHP, you can change the img src to something else if you wanted a colorful crab. I added a crabc.png file which is now used by my img, but it still has the same outline as the crab.png file:
Which gives this final look:
Hmm, I'm not sure it's possible to add them to fit a shape unless that shape is created in svg.
You could have a div with an image in it which is an inverted arrow i.e. the image is white with the arrow cut out and a bgcolor on the div.
Then you could add the dots to the same div and just set their z-index to lower than the arrow image.
Then the dots will only appear in the arrow, only problem is some might be added but not be visible (only a problem if you need the user to interact with them).
What exactly do you want to do with the dots?