Random URL File Get Content PHP - php

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

Related

System hit counter SaaS - Via TXT - Imagecopymerge

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).

php - set photo to image by path [duplicate]

This question already has answers here:
Output an Image in PHP
(12 answers)
Closed 4 years ago.
Well, my page contains an img like this:
<img id=".."class=".." src="setPhoto/second_photo.php">
Now, as you can see in src I have put a .php file:
<?php
$id = $_SESSION['u_id'];
$sql = "SELECT * FROM users WHERE id=$id";
$result = mysql_query("$sql");
if ($row = mysqli_fetch_assoc($result)) {
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['profile_front_photo'];
} else {
// no result from database
mysql_close($link);
header("Content-type: image/jpeg");
echo '../../../_images/default.jpg'; //Here is the promblem
}
?>
This code with the path, is not working. I want to set a photo to img by using a path.
Is it possible?
The browser asked for an image. Instead of getting an image back, it got back a string "../../../_images/default.jpg".
Instead, you want to open that file and pass its contents through. You also need to set the correct MIME type in the response.
You should be able to find simple tutorials for this online, or take a look at e.g. https://secure.php.net/manual/en/function.fpassthru.php
You can try this:
dirname(__FILE__)
it will get the root folder your project and then you specified your path like this
dirname(__FILE__). '/imagefoldername/_images/default.jpg';
Make it
$path='../../../_images/default.jpg';
echo "<img src='.$path.'>";

How to load random URL from a file in php

I am trying to create news portal.
and I want to load random news link which is in the list.txt file
and created a php script which calls the list.txt and load the link from the list.
checkout my codes.
script.php
<?php
$loadlist = explode("\n", file_get_contents('list.txt'));
$rand = rand(0,count($loadlist)-1);
// Here is our random link URL
$picked = $loadlist[$rand];
?>
<meta http-equiv="refresh" content="2;url=<?php echo $picked; ?>">
list.txt
http://news.com/news1.html
http://news.com/news2.html
http://news.com/news3.html
http://news.com/news4.html
http://news.com/news5.html
Above code is working fine now, thanks
Get a random integer based on item count of your $loadlist array.
$loadlist = explode("\n", file_get_contents('list.txt'));
$rand = rand(0,count($loadlist)-1);
// Here is our random link URL
$picked = $loadlist[$rand];

Rotated image wont display in php

So i am trying to create a compass to show wind direction.
Function rotate($angle) {
$original = imagecreatefrompng("img/Arrow.png");
$compass = imagerotate($original, $angle, 0);
return $compass;
}
That will be displayed using some html that i am echoing. The variable angle is being passed from a database. The html on the php script looks like this:
<img src='".rotate($row['wind_dir'])."'/>
The image never displays, and clearly the browser does not know where it is.
When i view the html in my browser, the above line shows as
<img src="Resource id #4"/>
and when i click on it, it navigates to a 404.
What am i doing wrong? Have i forgotten a line in the image rotation function?
EDIT:
Having tried some of the responses below, i get an image, but it only shows as a black box!
EDIT2:
Well after much fiddling, it turns out all that was needed was to the third value of imagerotate() to -1 as follows:
$original = imagecreatefrompng("img/goog.png");
$compass = imagerotate($original, $angle, -1);
imagealphablending($compass, true);
imagesavealpha($compass, true);
header('Content-Type: image/png');
imagepng($compass);
imagedestroy($compass);
I posted a comment about using CSS or JS rotation instead but since then I've had a better idea.
The compass is always going to be Arrow.png in one of 360 positions.
Use a batch process in Photoshop or PHP to create 360 versions. One for each degree. Then you can just call Arrow_120.png for example for 120 degrees. You remove the issue with your existing code of creating images on the fly while avoiding compatibility issues with CSS / JS.
You have to execute the function and send header: try like below, say our php file name is rotate.php :
rotate.php
function rotate($angle) {
$original = imagecreatefrompng("test.png");
$compass = imagerotate($original, $angle, 0);
header('Content-Type: image/png');
imagepng($compass);
imagedestroy($compass);
}
if(isset($_GET['angle'])){
rotate($_GET['angle']);
}
THen in your html you can call the web resource i.e you php file as:
<img src="url_to_rotate.php?angle=90" />
Also remember to sanitize the GET input before executing it.
The displayed image should be a image file. To achieve this you should use imagejpeg();
So basically you should have 2 php files:
1: Creates the image file using your code and imagejpeg();
<?php
$original = imagecreatefrompng("img/Arrow.png");
$compass = imagerotate($original, $_GET['angle'], 0);
header('Content-Type: image/jpeg');
imagejpeg($compass);
?>
2: The php file that displays the image.
<img src='image.php?angle=".$row['wind_dir']."'/>
if you want just one file you could do the following:
<?php
$original = imagecreatefrompng("img/Arrow.png");
$compass = imagerotate($original, $_GET['angle'], 0);
ob_start();
imagepng($compass);
$stringdata = ob_get_contents();
ob_end_clean();
$imageData = base64_encode($stringdata);
$src = 'data: image/png;base64,'.$imageData;
echo '<img src="',$src,'">';
?>

Read multiple image php

I have two separate files, one is to display the html/php document image, and the other is a php file that renders the image using the header function content-type:image/jpeg.
I tried using it with one image and it works well. However, I need to display multiple images. How could I do this?
The html/php doc has an img tag that points out to the php file that renders the image
echo "<image src=Image.php>";
The image.php
$selectimage = mysql_query("SELECT Image from ImageTbl", $con);
if($selectimage)
{
header("Content-type:image/jpeg");
while($row = mysql_fetch_array($selectimage))
{
echo $row["Image"];
}
}
make two files one for image another for fetching the row like this
image.php
$image_id = $_GET["id"];
header("Content-type:image/jpeg");
//query database to get only one image from id
echo $row["Image"];
another file
getimages.php
//query for image data
while($row = mysql_fetch_array())
{
echo "<img src='image.php?id=$row[id]' />";
}
You can't output all the images together, because to the browser, it will look like the data of multiple images mushed together, which is nonsensical. Also, each image tag can only display one image. To solve this, give the image table an ID field to identify the image.
Then in the file that outputs HTML, do something like this (passing the ID for the image you need):
echo "<image src='Image.php?id=1>";
echo "<image src='Image.php?id=2>";
echo "<image src='Image.php?id=3>";
And then in the file that outputs the image, do:
$id = intval($_REQUEST['id']); // intval will validate the ID to be an int
$selectimage = mysql_query("SELECT Image from ImageTbl WHERE id=$id LIMIT 1", $con);
if ($selectimage) {
$row = mysql_fetch_array($selectimage);
if ($row) { // check if the image really exists
header("Content-type:image/jpeg");
echo $row["Image"];
}
}
Use a foreach loop to loop through the requested records and echo them out independently to the img tags which your using.
That would be the best way in my opinion.
If you want to display number of different images using one script, try to add some unique hash to the script name ( for e.g. md5( microtime() ) )
$seed = md5( microtime() );
echo '<image src="Image.php' . $seed . '">';

Categories