I am doing an hit counter (view) system, and now I need to find a way to merge all number images into one to be possible to embed in another pages, could someone help me out?
Here is the code I have:
$number = trim(file_get_contents('visitas.txt'));
$file = 'visitas.txt';
$views = file_get_contents ($file);
$fdata = intval($views)+1;
file_put_contents($file, $fdata);
$array = str_split($number);
if(!empty($array)){
foreach($array as $single){
echo '<img style="height:20px" src="'.$single.'.png">';
}
}else{
echo '<img src="0.png">';
}
$image = imagecreatefromstring(file_get_contents('sample.jpg'));
At the moment everything is working, each refresh is counting and sum the number writed by image, but I need to embed this counter in another pages, something like:
<img src='https://countersite.com.br/img-counter-385503.jpg'>
Here is a image of the counter working:
UPDATED
I need help to merge the image already created with the numbers (0.png, 1.png...) into one image (counter1.png).
Related
i was trying to rotate url using file get content but there's little error which I am not able to figure out.
<?php
//Rotate
$urls = array();
$divs[] = '/fun.jpg';
$divs[] = '/nuf.jpg';
echo file_get_contents(src="'. $divs[rand(0, count($divs)-1)] .'");
?>
But it's not returning any random div.
Well, if you just show images from same folder, as your PHP script is in, you should be able to do it like this. (did not test it)
<?php
header("Content-Type: image/jpg");
$divs[] = 'fun.jpg';
$divs[] = 'nuf.jpg';
echo file_get_contents($divs[array_rand($divs)]);
?>
You need to include header: Show image using file_get_contents
And for picking random key from array you can use array_rand() function: https://www.php.net/manual/en/function.array-rand.php
I can not find an answer(that works) to this anywhere on the web.
I am trying to get Lightbox to load images from a directory as they will be frequently updated.
If anyone can correct what I'm doing wrong, or has a solution using either PHP or a hidden div populated automatically by a specific directory it would be greatly appreciated.
Here is what I have come up with but is not seeming to work;
<?php $dirname = "img/love/"; $images = glob($dirname."*.jpg"); foreach($images as $image) { echo '<img data-lightbox="love" src="'.$image.'" /><br />'; } ?>
and here is my test page: http://knowledgeoverfame.com/tslp/leet/alt/index.html
I didn't find any similar questions here with an answer to this but if i may have missed it please enlighten me :)
Thanks!
Try using scandir() to get an array of your image files.
Example:
<?php
$images = scandir($your_folder);
foreach ( $images as $key => $filename ) {
if ( $key > 1 ) { //ignores the first two values which refer to the current and parent directory
echo '<img data-lightbox="love" src="'.$your_folder."/".$filename.'" /><br />';
}
}
?>
for reference: PHP scandir()
What you want to do - use lightbox to display images from a folder - is a problem which has been solved lots of times with varying degrees of complexity. If you do a google search for something like "php image gallery" you will find sample scripts. I did just this about a year ago and after some experimentation I chose Minigal Nano as it was a powerful script but simple enough for it to be quite easy to see how it worked and then rework for my own use. I also found this an effective way to learn php.
This script will pull the images from a directory. I used it with fancybox but you can modify to fit your needs.
<?php
$directory = 'yourDirectory/gallery/thumbs'; //where the gallery thumbnail images are located
$files = glob($directory."/*.{jpg,jpeg,gif,png}", GLOB_BRACE);
natsort($files); //sort by filename
?>
<?php
for($x = 0; $x < count($files); $x++){
$thumb = $files[$x];
$file = basename($thumb);
$nomargin = $x%4 == 0?" nomargin":"";
$title = htmlspecialchars($file);
?>
<div class="thumbs fancybox<?= $nomargin ?>" style="background:url('<?= $thumb ?>') no-repeat 50% 50%;">
<a rel="group" href="yourDirectory/gallery/<?= $file ?>"></a>
</div>
<?php
}//end for loop
?>
I'm accessing an API which is returning the value of an array. I want a image to be displayed based on the result. For example if the div contains "above average" then display an image called aboveAverage.png.
echo $cinfo['crimes']['2013-03']['anti-social-behaviour']['crime_level'];
This results is "Above Average" How can I display a particular image to correspond to this?
Something like ->
if Div "crime_level" = above average then:
display aboveAverage.png
I'm pretty new to PHP, sorry i'm a noob.
Use a case selecting switch
switch ($cinfo['crimes']['2013-03']['anti-social-behaviour']['crime_level']) {
case "Above Average":
$image = "aboveAverage.png";
break;
case "Below Average":
$image = "belowAverage.png";
break;
default:
$image = "unknown.png";
};
echo "<img src=\"$image\" />";
Doing it this way allows you to remove your logic from your display by setting the variable as $image so you're not having to update 20 different potential image tags. It also allows you to cater for many different cases of the string without ending up lost in if then else hell
If the values of
$cinfo['crimes']['2013-03']['anti-social-behaviour']['crime_level']
are finite and you happen to know them all you could use an associative array, where the key would be the value returned by the API and the corresponding value to that key would be the file name of the image you want to display. Something like:
$images = array(
'Above Average' => 'aboveAverage.png',
'Below Average' => 'belowAverage.png',
// etc
);
$img = 'default.png'; // set a default image file
$crimeRate = $cinfo['crimes']['2013-03']['anti-social-behaviour']['crime_level'];
if (array_key_exists($crimeRate, $images)) {
$img = $images[$crimeRate];
}
// output the image
echo "<img src=" . $img . " />";
There are two parts to your question. The first is how to compare the array element to a string. This is accomplished like this:
if ( $cinfo['crimes']['2013-03']['anti-social-behaviour']['crime_level'] == "Above average" ) {
// The second part of your question is how to display an image.
echo '<img src="aboveAverage.php" />;
}
There are other ways to display images via PHP scripts, such as setting an appropriate header and dumping the image content directly. If you are looking for such a solution, see Output an Image in PHP.
Here is some approach:
$toDisplay = array("Above average", "Average") ;
if (in_array($cinfo['crimes']['2013-03']['anti-social-behaviour']['crime_level'], $toDisplay)){
echo "<img src='image.png' alt='image'/>" ;
}
A simple if and echo will do:
if ($cinfo['crimes']['2013-03']['anti-social-behaviour']['crime_level'] == "above average") {
echo "<img src='aboveAverage.png' />
}
I am building a Wordpress website. Please don't mind that I'm not working on a local or testing server, or my code. It's messy at the moment while I try to figure out this programming bit. By going to my website, you will see a row of images along the top, which is within a jcarousel.
It works, but if you hit next until you get to the "portrait" photo, there is a gap. The gap is because I have to set a width in pixels within my HTML or CSS for the carousel to work correctly. If set to auto, the carousel collapses. Makes sense to me, but it's frustrating that I cannot work around that. I want the "landscape" and "portrait" photos to have an identical height of 427, but the width I want it to self-adjust to the actual image size.
So I think I understand this bit after playing around for 3 days - I cannot tell you how many carousel codes I've used. Originally I thought that was the problem, and some were. I found out, widths of each image slide are set within the JavaScript, so if I gave it a width of 640, I would get the same result as I am getting now.
My first question is this:
I'm calling my images from a directory on my server, written specifically for Wordpress (author). Here is the code:
<?php
$uploads = wp_upload_dir();
if ($dir = opendir($uploads['basedir'].'/picture-atlantic')) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$images[] = $file;
}
}
closedir($dir);
}
echo '<ul>';
foreach($images as $image) {
echo '<li><img src="';
echo $uploads['baseurl'].'/picture-atlantic/'.$image;
echo '" alt="" width="auto" height="auto" /></li>';
}
echo '</ul>';
?>
As you can see, it's calling from the picture-atlantic directory and displaying it within an unordered list, which is required for some reason with my jcarousel.
Finally: how can I use this same code to get my images, but also display them at their actual sizes? In the code above, if I define the width and height, it does it for all the images. However, as I mentioned before, I have portrait pictures that use different dimensions. I found this code: erikastokes.com/php/how-to-get-image-size-with-php.php, but I'm not sure how to implement it in my existing code.
Could someone please help me rewrite it?
You could set the width of the image to 100% and add a style to reposition portrait images at top: -50%;, but you will not be able to see the entire image if it is in landscape.
<?php
$uploads = wp_upload_dir();
if ($dir = opendir($uploads['basedir'].'/picture-atlantic')) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$images[] = $file;
}
}
closedir($dir);
}
echo '<ul>';
foreach($images as $image) {
$myimage = $uploads['baseurl'].'/picture-atlantic/'.$image;
$size = getimagesize($myimage);
$top_50 = '';
if($size[1] > $size[0]) {
$top_50 = 'style="position: realative; top: -50%;"';
}
echo '<li><img src="';
echo $uploads['baseurl'].'/picture-atlantic/'.$image;
echo '" alt="" width="100%" height="auto" '.$top_50.'/></li>';
}
echo '</ul>';
?>
Welp, I read that I cannot use getimagesize if I'm already using php to dynamically display my images. I ended up settling with uploading images through Wordpress, within an unordered list, and placing my carousel markup in the post loop. It works just fine now.
Oh, I read on http://www.css-tricks.com that Wordpress adds the image attributes automatically, and that is what solved the image width problem while displaying landscape and portrait style photos at 100% across the screen.
To those who are trying to do a similar task, don't use jj nexgen gallery for inserting your images. jj nexgen gallery is a great plugin, along with the others associated with it. It just won't work for what I was trying to do because it doesn't add the attributes to the image. I would download their wordpress plugins if you aren't trying to do this, so I'm guessing the majority of you.
Thanks again for the replies.
I have a folder named as images. I just want to fetch those images from the folder and show them randomly on web page. When the user refreshes the page, the image should change.
Thank you.
An idea could be:
you fetch all the images names frome the images folder
you randomly pick one
you visualize that one
so you can go like this
<?
$dir = '/my_directory_location';
$files = scandir($dir);
$rand_img = array_rand($files, 2);
$imcolumn = "<img src=$rand_img alt=$rand_img><br>";
?>
<?= $imcolumn ?>
<?php
$folder = "templates/images/";
$search = glob($folder."/*");
shuffle($search);
foreach ($search as $image) {
print "<img src='".$image."'><Br>";
}
?>