Use an image file link to display a random banner - php

Not sure if this is even appropriate here, but I'm trying to work out how a banner rotator website is using standard html image code, to return a random banner?
<a target=_blank href=http://intellibanners.com/click.php?cid=campaign1>
<img border=0 src=http://intellibanners.com/campaign1.jpg>
</a>
In the example above, that html will display a random image from that campaign.
This started as a bit of a whim to see if I could set up something similar to work with different image sizes, but now it's driving me nuts... trying to figure out how to go from a image url to a database call to send back a different image!?
No lucky searching for ideas or examples, probably because I don't know what sort of processes or functions I should actually be looking for?
I'm thinking there must be some sort of url rewrite for all image calls, that redirects to a handler script...
That script makes the database call, grabs a random image from the nominated campaign, updates stats etc...
And possibly a php header response returns the corresponding image file?
But most of that is new to me and I'm not sure where/how to search for guidance or examples to get me started.
If anyone has any ideas on this, or even the kind of phrasing/functions I need that might help me get there in my own searching, it would be much appreciated!
(I've also installed and tested about 12 different rotator scripts hoping to find one that works the same, but no luck there either).
Thanks!
Matt

I wrote a php script that assumes to exist the wanted folders in the root of your website. Then you can invoke it in a simple way in any location of your website. Here is my script:
rotate.php
<?php
##########################################################
# Simple Script Random Images Rotator • 1.4 • 04.01.2020 #
# Alessandro Marinuzzi [alecos] • https://www.alecos.it/ #
##########################################################
function rotate($folder) {
if ((file_exists($_SERVER['DOCUMENT_ROOT'] . "/$folder")) && (is_dir($_SERVER['DOCUMENT_ROOT'] . "/$folder"))) {
$list = scandir($_SERVER['DOCUMENT_ROOT'] . "/$folder");
$fileList = array();
$img = '';
foreach ($list as $file) {
if ((file_exists($_SERVER['DOCUMENT_ROOT'] . "/$folder/$file")) && (is_file($_SERVER['DOCUMENT_ROOT'] . "/$folder/$file"))) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png') {
$fileList[] = $file;
}
}
}
if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder . '/' . $fileList[$imageNumber];
}
return $img;
} else {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/$folder", 0755, true);
}
}
?>
Now you can create a index.php file where you want and put this line:
<?php include("your_path/rotate.php"); ?>
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign1">
<img border="0" src="/<?php echo rotate('campaign1'); ?>">
</a>
other like this:
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign2">
<img border="0" src="/<?php echo rotate('campaign2'); ?>">
</a>
and so that...
You have only to create the wished folders in the root called campaign1, campaign2...etc...
then in each folder put the banner images of the current campaign...
for example: in the folder campaign1 put image like 001.png, 002,png, 003.png, 004.jpg, 005.jpg... (where these filenames match your campaign banners).
for example: in the folder campaign2 put image like 001.png, 002,png, 003.png, 004.jpg, 005.jpg, 006.gif, 007.png, 008.jpg... (where these filenames match your campaign banners).
Please note that if you don't want rename your banner images you can take the original names, my script will work in any way.
My script will turn all in wished output like this:
campaign1:
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign1">
<img border="0" src="/campaign1/002.jpg">
</a>
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign1">
<img border="0" src="/campaign1/007.jpg">
</a>
campaign2:
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign2">
<img border="0" src="/campaign2/004.png">
</a>
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign2">
<img border="0" src="/campaign2/005.gif">
</a>
See you and test all into your localhost website... for me works fine on PHP 7.4.5 and Apache 2.4.43.
The script will do the rest!
Hope this helps you!

Related

How may Sphider be modified to display an image in search results, without knowing the name of the image

Using the code below with the Sphider search engine, Sphider will display an image in search results - if the html page and jpg share the same name (i.e) page14.html, and page14.jpg.
1) Without knowing the name of the image, how could the code be modified so that the first of 2-3 jpg's is displayed from the html page returned in search results ?
<?PHP
$url_path = "http://www.webpage.com/"; // The url to indexed pages.
$url_ext = ".html"; // The file ext. of indexed pages.
$image_path = "../"; // Path to image folder.
$image_ext = ".jpg"; // The file extension of images.
$image = str_replace("$url_path", "$image_path", $url);
$image = str_replace("$url_ext", "$image_ext", $image);
if (file_exists($image)) { // Do nothing
} else { $image = $image_path . "ICS" . $image_ext; } ?>
<img src="<?PHP print $image ?>" align="left" style="margin-right:10px;" />
Or,
2) How would I modify the above php code to display the images below in search results - without renaming all the images ?
I have many gallery pages with hundreds of the following html image links;
<img SRC="chair_0.jpg">
<img SRC="table_0.jpg">
<img SRC="bed_0.jpg">
In my case, all our webpage images have the same ID as the end of each page.
Following is an example of how our pages look like: www.mywebsite.com/category/sub-category/etc/details.php?category=6&subcategory=s&sc=180&productid=98
The pictures are located in another path, and share the same last digits (i.e 98) as web page.
Following is an example of picture folder: www.mywebsite.com/admin/picures/
In addition, all pictures have an extra "p" at the front and "_full" at end of the digits, as following example "p98_full.jpg", p109_full.jpg" or "p3_full.jpg".
Following method worked for me. You will need to past this on sphider/templates/standard/search_results.html line # 76, modify it a bit to match your setting and see if it works... (You do not need to add a url link to your site to this code, this should work as is)
<?PHP
$url_ext = ".php"; // The file extension of indexed pages.
$image_path = "../admin/pictures/"; // Path to image folder.
$pic_name = "p"; // Alphabet before image, if any.
$page_end = Substr ($url2, strrpos($url2, "=")+1); // Isolate digits or alphabets starting from end of page.
$image_ext = "_full.jpg"; // The file extension of images.
$image = $image_path . "$pic_name$page_end" . $image_ext; // Select isolated digits or alphabes.
if (file_exists($image)) { // Do nothing
} else { $image = $image_path . "ICS" . $image_ext; } ?>
<img src="<?PHP print $image ?>" alt="" align="left" style="margin-right:10px; border:0;" />

Image display with <img src=""> not working

EDIT :: I found out that i am having a permission error. For whatever reason i dont have "permission" to grab the image from the images folder ... *
a few days ago i asked this question about uploading images and was very satisfied with the anwser. I integrated it into my site and it works no problem.
Image - Upload not responding, no access to $_FILES
The problem i am having now is i cannot retrieve the images in any way shape or form. I have tried opening them and setting them to read, with fopen($image, r). I tried even more complicated things like loading the image back into a database ... then it hit me that the anwser is simple.
I CAN see the image in my images folder, so should work no problem. But it doesnt =(
Here is the PHP script one more time,
if (isset($_FILES['fupload'] ))
{
$max_size = mysql_real_escape_string(strip_tags($_POST['MAX_FILE_SIZE']));
$file = $_FILES['fupload']['name'];
if(isset($max_size) && !empty($max_size) && !empty($file)) {
$file_type = $_FILES['fupload']['type'];
$tmp = $_FILES['fupload']['tmp_name'];
$file_size = $_FILES['fupload']['size'];
$allowed_type = array('image/png', 'image/jpg', 'image/jpeg', 'image/gif');
if(in_array($file_type, $allowed_type)) {
if($file_size < $max_size) {
$path ='images/'.$file;
$move = move_uploaded_file($tmp, $path);
$sql = "UPDATE info.profile SET Profile_pic='".$move."', pic_name='".$file."' WHERE Nick='".$_SESSION['Nick']."'";
mysql_query($sql) or die ("Error: " .mysql_error());
$_SESSION['pic'] = $path;
echo "image added successfully :) :) :) ";
}
I left out all the else statements, since they are not relevent. The folowing is an example of where the image should be displayed.
<td rowspan=2 align=center width=100px> <img src="<?php $_SESSION['pic']; ?>"
alt="Here would be the picture, if it was working !!"> </td>
The alt is a small teaser of course, but i have tried everything and with the current script i managed to get the alt text to display. Sometimes a "bad image" icon displayes (like when you open images in an email before allowing images to be displayed)
When i echo the path it shows the "right" path, just like i would enter when manualy giving it in. The reason i store the path in DB and in a session is because the image is a profile image and should only be displayed for THAT user ... wouldnt want other users having other images as their profile pic. =)
Did you cross check the file permission issue?
What is the response code return when you copy and paste the image path to the browser?
e.g. http://yourserver.com/part-to-image/image.jpg
If 404 returned, you may need to troubleshoot the server directory mapping.
If 500 returned, might be file permission.
Apart from that your code
<?php $_SESSION['pic']; ?>
Should it require echo to embed to html tag?
<?php echo $_SESSION['pic']; ?>
The path should be relative to the current directory. Have u checked it ?
Try the answer given in comment by Rohit.
<img src='<?php echo $_SESSION['pic']; ?>' />

open picture links in new page with php

I have the code to dump pics in a directory and produce the result in a feed like instagram, i just want it to open the image onto a page with css and area for the picture already plotted out. I just need a jumping off point to open the images individually.
I hope this helps:
<?php
$dest = "index.php?id=" . SHOWPIC . "&path=" . $path;
?>
<a href="<?php echo $dest; ?>">
<img alt="Photo" src="<?php echo $path;?>">
</a>
SHOWPIC here is a constant that contains an integer. index.php should have something like:
if($_REQUEST["id"] == SHOWPIC)
require_once "showpic.php";
$path in my example is meant to contain the path to the image displayed.
showpic.php will then read the contents of $_REQUEST["path"] and display the pic. That's also the place where you can add CSS or whatever you like to the site.

Get images one by one from directory

in php i want to ask how to get images one by one from folder directory and display in the jquery slider i have tried this php code by it's not working as i want?
<ul class="jcarousel-list">
<li class="jcarousel-item">
<?php
$directory = "data/uploads/topslider/";
if (glob($directory . "*") != false)
{
$filecount = count(glob($directory . "*"));
}
else
{
}
$files_index = glob("data/uploads/"."top"."slider/*.*");
for ($i=0; $i<$filecount; $i++)
{
$num2 = $files_index[$i];
?>
<img src="<?php echo $num2;?>" width="50" height="50" alt="" /> <?
}?></li>
</ul>
i want display like this:
Image1 Imag2 Image3......and So On from single folder or directory
There are four main things to check:
Is your $directory path correct relative to your current working directory in PHP? You can determine your current working directory by doing a temporary echo getcwd();. It needs to return the parent directory of your "data" folder for your code to work.
Is the data folder accessible from the current page on the web? e.g. if you manually remove the page from the URL (say, index.php) and add data/uploads/topslider/someimage.png where someimage.png is an image you know exists in your topslider folder, does the image load properly in the browser? If not, you'll need to update the way you build the src attribute in your img tag(s).
You're only adding one jcarousel-item for all your images, which doesn't seem right to me. I'm guessing you're expected to add a list item for each image.
You don't need to call glob twice just to ascertain how many files you have to work with. Just do a foreach statement once:
echo '<ul class="jcarousel-list">';
foreach(glob($directory . '*') as $filename) {
echo '<li class="jcarousel-item"><img src="'.$filename.'" width="50" height="50" alt="" /></li>';
}
echo '</ul>';

Sphider with Product Images in results page - Sphider 1.3.5

I hope this can be of great help to others, it took me awhile of PHP learning, but gave me a good PHP high when I pieced it together.
For those of you who are looking to put images next to your sphider results...
I use Sphider for indexing a product folder with thousands of items, here is an example of my file structure for use with this snippet:
www/
product/
item1.php
item2.php
item3.php
images/
item1.png
item2.png
item3.png
Insert this into sphider/templates/standard/search_results.html (or sphider/templates/dark/search_results.html) in line 75 just after " class="title">
<?PHP
// Edit here
$url_path = "http:// www.yoursite.com/product/"; // The url leading to your indexed pages.
$url_ext = ".php"; // The file extension of indexed pages.
$image_path = "../images/"; // Path to image folder.
$image_ext = ".png"; // The file extension of images.
// To here
$image = str_replace("$url_path", "$image_path", $url);
$image = str_replace("$url_ext", "$image_ext", $image);
if (file_exists($image)) {
// do nothing or Your Option
} else { $image = $image_path . "ICS" . $image_ext; } ?>
<img src="<?PHP print $image ?>" alt="" align="left" style="margin-right:10px; border:0;" />
Basically, if you have a page called 1234.php in your Sphider index, this will try to find picture 1234.png to go along side it of the search result. If an image is not found it will display a default image 'ICS.png' (Image Coming Soon).
Happy coding

Categories