i want use php page(random image) in img src(<img src="ads.php"/>)
my ads.php page:
<?php
header('Content-Type: image/png');
$img = array();
$img[] = '<img src="images/1.png" />';
$img[] = '<img src="images/2.png" />';
shuffle($img);
readfile ($img[0]);
?>
The values you put into your $img array need to be pathnames to graphics, not img tags. When you're using readfile() it's trying to find '<img src="images/1.png" />' in the local filesystem.
<?php
header('Content-Type: image/png');
$img = array();
//$img[] = '<img src="images/1.png" />';
//$img[] = '<img src="images/2.png" />';
$img[] = '/path/to/images/1.png';
$img[] = '/path/to/images/2.png';
shuffle($img);
readfile ($img[0]);
?>
Consider how the browser is seeing your code. It's parsing the page and encounters:
<img src="ads.php" />
and goes "ahah, I have to go hit the ads.php script on the site and it'll feed me an image". This is NO different than if you had
<img src="ads.jpg" />
The url you're telling the browser to hit on the server MUST serve up an IMAGE.
But instead of an image, you're serving up more html.
if you want to out the image you have to use
<?php
header('Content-Type: image/png');
$img = array();
$img[] = '/path/to/images/1.png';
$img[] = '/path/to/images/2.png';
shuffle($img);
readfile ($img[0]);
?>
Related
How to hide an url with base64 like this?
<img src="show_image.php?url=aHR0cDovL2RvbWFpbi5jb20vaW1hZ2UuanBn">
In php you can encode in base64 with base64_encode(), and decode with base64_decode().
So, in your html page script you can do:
<?php
$imageUrl = 'the image url';
echo '<img src="show_image.php?url=' . base64_encode($imageUrl) . '">';
And of course, you'll need to decode it on your show_image script:
<?php
$imageUrl = base64_decode($_GET['url']);
$image = imagecreatefromstring(file_get_contents($imageUrl));
header('Content-Type: image/png');
imagepng($image);
<?php
$dir= 'C:\xampp\htdocs\img';
echo "<img src='".$dir."\america.jpg' alt='icon'>";
?>
I just written this code for test and I couldn't get able to display image on browser.
The path you are using is not correct, try this:
$path = '/img/';
$imgName = 'america.jpg';
echo '<img src="'. $path.$imgName .'" alt="icon">';
// If img folder is present in the same directory
or
$path = '../img/';
$imgName = 'america.jpg';
echo '<img src="'. $path.$imgName .'" alt="icon">';
// If img folder is present one directory upwards from current directory
I have a script to cache images locally. It works perfectly;
<?
$image = file_get_contents("$bg");
$filename = basename($bg);
file_put_contents("images/$filename", $image);
?>
<img src="<? echo $bg; ?>"><br>
However, I want my script to use the local image in the img tag if it's already downloaded and present in my folder. If the image isn't already downloaded, then save the external image and then use the local image.
In other words; Check if the image is already in the folder, if it's not - then download it and display the local file. If it's already in the folder, just display the local image without downloading the image again.
You can test the existance of file with file_exists function please see documentation :
http://php.net/manual/fr/function.file-exists.php
<?php
$filename = basename($bg);
if(!file_exists($filename))
{
$image = file_get_contents("$bg");
file_put_contents("images/$filename", $image);
}
?>
<img src="images/<?php print $filename?>"><br />
You can use file_exists():
<?php
$filename = basename($bg);
if(!file_exists("images/$filename")){
// we don't have it, Cache it first
$image = file_get_contents("$bg");
file_put_contents("images/$filename", $image);
}
?>
<img src="<? echo $bg; ?>"><br>
You can use file_exists function to check already exits or not.
Try example
<?php
$filename = basename($bg);
if(!file_exists($filename))
{
$image = file_get_contents("$bg");
file_put_contents("images/$filename", $image);
}
?>
<img src="images/<?=$filename?>"><br>
Ok more details:
Line with print_r outputs rendered images because in database
they are stored like <img src="" etc..
I want to see the path not image.
echo '<pre>';
foreach ($rows as $row){
if ($row->fulltext != ''){
print_r ( $row->fulltext );
echo "\n";
}
}
echo '</pre>';
Render image in browser:
$img = // get image path
echo '<img src='.$img.' />';
Output image path in browser:
$img = // get image path
echo $img;
How can I put image from the memory to the browser, without saving.
For example:
function getImage()
{
$imageFile = imagecreatefromjpeg('Map.jpg');
$imageObject = imagecreatefrompng('image2.png');
imagealphablending($imageFile, true);
imagecopy(....);
$ret = array($imageFile, $imageObject) ;
return $ret
}
<?php $ret = getImage(); ?>
<img src = <?php $ret[0];? alt=''>
Is this possible, without saving?
Yes,
Just try imagejpeg($img);
and put into <img src= path to the PHP script which render the image
See sample at: http://php.net/manual/en/function.imagecreatefromjpeg.php
Maybe if you would code your image to base64 and use it like that, it would work:
<?php
$img_str = base64_encode($imgbinary);
echo '<img src="data:image/jpg;base64,'.$img_str.'" />';
?>
HTML:
<img src="data:image/jpg;base64,R0lGODlhCgAKAJEAAAAAAP///81Wv81WvyH5BAEAAAMALAAAAAAKAAoAAAIUjIViq+x7QpunwXoZ lXFu/mjIUgAAOw==" alt="image" />
I infered that you want to do this in one request.
You should have a script which sends proper headers and then it should be recognized as an image by the browser. Something like:
<?php
ob_start();
// assuming you have image data in $imagedata
$length = strlen($imagedata);
header('Last-Modified: '.date('r'));
header('Accept-Ranges: bytes');
header('Content-Length: '.$length);
header('Content-Type: image/jpeg');
print($imagedata);
ob_end_flush();
?>