PHP: Use local image if it exsists - php

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>

Related

Using srcset with random img from dir (php)

I'm looking for a way to use the responsive image attribute 'srcset' alongside php. I'm currently using the following code in order to pick a random image from a directory on the server:
<?php
$dir = "img/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
?>
<img src="img/<?php echo $images[$i]; ?>" alt="image">
I've created different versions of the images in the directory with a suffix (i.e., image-1-small.jpg, image-1-big.jpg ... image-2-small.jpg, image-2-big.jpg etc.).
How would I implement the srcset attribute into the php string? I'm looking for an output similar to this:
<img sizes="100vw" srcset="img/image-1-small.jpg 400w, img/image-2-medium.jpg 800w, img/image-1-big.jpg 1600w" src="img/image-1-small.jpg" alt="image-1">
Thanks!
You can try something like this:
<?php
$dir = "img/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
// Get image file name.
$image_name_full = $images[$i];
// Define display types.
$image_display_types = array("-small.jpg", "-medium.jpg", "-big.jpg");
// Remove image display type from image name.
$image_name = str_replace($image_display_types, "", $image_name_full);
?>
<img sizes="100vw" srcset="img/<?php echo $image_name; ?>-small.jpg 400w, img/<?php echo $image_name; ?>-medium.jpg 800w, img/<?php echo $image_name; ?>-big.jpg 1600w" src="img/<?php echo $image_name; ?>-small.jpg" alt="<?php echo $image_name; ?>">

PHP file_exists and save locally

I'm having a script that checks remote server for a file, and if it is already available locally it should use that one. If it isn't available locally, then download the image to the server. However, I'm struggling with the script overwriting images that I already have locally. If it's already available locally, the script shouldn't do anything with the file - just display it.
In some cases, the script tries to download a image that I already have - and the filesize for the file it overwrites becomes 0kb even though the file worked perfectly earlier.
What am I doing wrong here?
<?php
$url = "http://www.myserver.com/image123.jpg";
$filename = basename($url);
if(!file_exists(images/$filename))
{
// File doesn't exsist, download it!
$image = file_get_contents("$url");
file_put_contents("images/$filename", $image);
$image = "images/$filename";
} else {
// We already got this file, display it!
$image = "images/$filename";
}
echo $image;
?>
<?php
$url = "http://www.myserver.com/image123.jpg";
$filename = basename($url);
$path = "images/$filename";
if( !file_exists($path) ) {
$image = file_get_contents($url);
file_put_contents($path, $image);
}
echo $path;
?>

What is wrong with my code on printing a random image in a website?

this is the code in order to display a random image on my website,
but for some reason the images are not popping up and there is an error on the line where $rand_image was declared. The error says undefined index. I have 7 images in the directory.
$imagesDir = '\socimages\Badminton'; // my image directory
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); // glob
$rand_image = $images[array_rand($images)]; // applying the random function
...
...
<img src="<?php echo $rand_image[0];?>"< alt="" height="246" width="246"></p></div>
$rand_image is the image already you don't need $rand_image[0];
<img src="<?php echo htmlspecialchars($rand_image);?>"< alt="" height="246" width="246"></p></div>
Also, $rand_image will probably be the path on disk, not the web accessible path so you'll need to map it.
I'd do something like this.
$imagesDir = '/socimages/Badminton/'; // my image directory
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); // glob
if ($images !== FALSE) {
$rand_image = $images[array_rand($images)]; // applying the random function
} else {
die("This thang ain't working, yo.");
}
...
...
<img src="<?php echo $rand_image;?>"< alt="" height="246" width="246"></p></div>

How to append .jpg extension for the images urls when there is no extension?

I am generating zoom, thumbnail, and preview images for some images. Some images are saved without their extension. When I am displaying in the image gallery, images without extension are not zooming. This is because the lightbox which we are using is expecting a .jpg extension for images. So the images are not zooming when clicking on it. Zooming on images with the extension are ok.
For this I am appending the .jpg extension like the code below, but it is not working.
<?php foreach($images as $line):
$image_info = pathinfo($line['big']->filename);
if(!array_key_exists('extension', $image_info))
{
$zoom_file = $line['big']->filename."?ext=.jpg";
}
else
{
$zoom_file = $line['big']->filename;
}
?>
<li>
<a href="<?php echo $media_cdn.$zoom_file?>">
<img src="<?php echo $media_cdn.$line['normal']->filename ?>"
width = "200px" alt="image" />
</a>
</li>
<?php endforeach;?>
This should get you pointed in the right direction...
$image = $from->somewhere;
$ext = strtolower(end(explode(".", $image)));
$image = ($ext != "jpg") ? $image.".jpg" : $image;
edit
I don't believe this line is correct:
$zoom_file = $line['big']->filename."?ext=.jpg";
Perhaps it should be:
$zoom_file = $line['big']->filename.".jpg"; ?

PHP Random image not working

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]);
?>

Categories