Display last uploaded image - php

I working on an image gallery with the thumbnails and main image on the same page. I have ordered the thumbnails so that the last uploaded image standing on top but I also want that this image is displayed as main image. My site is http://www.robcnossen.nl/view_album.php?album_id=7
I can't find if it can be done with mysql but maybe I am looking right.
I'm trying to find the solution in PHP but all what I find on internet I can't get it working for me.
I thought that array multisort, filectime and ksort would solve my problem but nothing changed.
A part of my code is;
print_r ($images);
//$dirname = dirname(__FILE__);
//filectime($dirname);
//ksort($images);
array_multisort($images, SORT_DESC);
if(isset($image['album'], $image['id'], $image['ext']));
$foto = 'uploads/' . $image['album'] . '/' . $image['id'] . '.' . $image['ext']. '';
$standaardwaarde=isset($_GET['image_id']) ? $_GET['image_id'] :$foto;
echo'<img src="' ,htmlentities($standaardwaarde), '" title="" />';
How can the last uploaded image be shown as main image?

If the $images is a return variable from a DB query, you can change that query by adding an ORDER BY timestamp (I see on your var_dump that there is a timestamp).

If you store it in a mysql database and your image are stored with an auto increment field or a date field you can use the ORDER BY clause to get the last image as the first (inverse order):
ORDER BY id DESC or ORDER BY timestamp DESC.
your main image will be then:
$retrieved_array[0]['field_to_show']

Related

How to show the last uploaded image when refresh page in PHP

I have a application which is generating images with file_get_contents and file_put_contents method from a dynamic image source. After creating the image the image is uploaded to a directory on my server. The problem I face is every time the image is generated and I refresh the page I see the old image appear. It shows the new image when I clear the cache.
How could I solve the issue?
<?php
$imagename = "img".$id.".png";
$host = $_SERVER['DOCUMENT_ROOT'];
$path = $host.'/url/path/img/'.$imagename;
if(file_exists($path)) {
//echo 'File already exists in that directory';
unlink($path);
$filehandler = file_get_contents($imgurl);
file_put_contents($path, $filehandler);
}
else {
$filehandler = file_get_contents($imgurl);
file_put_contents($path, $filehandler);
}
It would always be better to use version as query Param to all images, js and css files. Using timestamp will not be as good since it will not be shown from cache if image is not updated. So in order to avoid additional overhead use version of images
That is,
Initially
<img src="image.png?v1" />
Till now next updation occurs this should be the URL
Once it is updated, it should change to
<img src="image.png?v2" />
As the other answers already stated, simplest solution is to add a parameter to the image URL. To still leverage browser caching when the image is unchanged, you should not use a random value.
My recommendation is to use the modification time of the image file, e.g.
<img src="image.png?<?php filemtime('image.png'); ?>" />
This has the advantage, that you don't need to keep track of versions to increment a number or similar.
This can be solved by adding random parameters to the image src, as shown here. For example, turn this:
<img src="image.png" />
Into this:
<img src="image.jpg?1222259157.415">
In the second src, the numbers after the question mark are the current timestamp. This is demonstrated in this answer, which sets the timestamp in JavaScript and appends it to the src using Date.now().

how to add date with image name during update record in database

I am trying to upload image,rename and add date with image ,after inserting record in database using cakephp.
if (move_uploaded_file($this->request->data['News']['image_url']['tmp_name'], WWW_ROOT. 'media/' . $this->request->data['News']['image_url']['name'])) {
$this->request->data['News']['image_url'] = $this->request->data['News']['image_url']['name']. date('Y-m-d');
}
It didn't upload image,rename image and add date with image,how can i correct my function to insert record ?
According to your problem I understood that you wanted to add date before image name when upload image and save that in database. It's simple. Use below code
if (move_uploaded_file($this->request->data['News']['image_url']['tmp_name'], WWW_ROOT. 'media/' . date('Y-m-d'). $this->request->data['News']['image_url']['name'])) {
$this->request->data['News']['image_url'] = date('Y-m-d') . $this->request->data['News']['image_url']['name'];
}
$this->Modelname->save($this->request->data['News']);
Here date adding before image name after location. In your previous code it was missing. So data was updated but image name were not changed in location. Your problem will solve now.

cycle2 fails with images inside php script

I can't get the cycle2 pager to work and I've checked the various questions and answers on this topic. I'm beginning to think it may have to do with my setup: the user clicks one of several buttons and that brings up one or more thumbnail images from a mysql database. Each thumbnail is related via the database to one or more larger images that I'd like to play in a slideshow. An ajax call sends the info about which thumb was clicked and a server page retrieves the larger images from a directory and displays them all in a div on the calling page as follows:
<?php
$thumb_path = $_POST['thumb_path'];
ob_start();
echo basename($thumb_path, ".png");
$thumb_name = ob_get_contents();
ob_end_clean();
$dir = "pathTo/$thumb_name/*.png";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
So far, so good as all the larger images are displayed (statically, all at once) in the div. The problem comes when I try to wrap the php script above in any of the cycle2 pagers (I'd like to use the one that automatically creates thumbnails for navigating the larger images, but none work). What happens is that all the larger images are loaded on top of each other without any navigation controls (thumbnails, dots, numbers etc. as the case may be). The larger images do play if I set the
data-cycle-timeout
parameter to something other than zero, but I'd like to set it to zero and let the users navigate the larger images themselves.
I'm a total noob at web programming and it's taken a while to get this stuff to work, chiefly by studying code snippets I've found on the web. I hope any solution doesn't involve a major redesign.
Thanks in advance for any help.

uniquely name images that are output to browser with php script

I use wordpress cms. I allow selective people to upload image. I have a script that resizes it and outputs resized image back to the browser where they can right click and save it. Here is the relevant part of the code. If needed I will put up the whole code. The script works and puts the resized image in the browser nicely.
header('Content-type: image/jpeg');
ob_start();
imagejpeg($resized, null, 100);
echo '<img src="data:image/jpeg;base64,' . base64_encode(ob_get_clean()) . '">';
imagedestroy($resized);
ISSUE : The form has just one upload field. I only allow images to be resized and saved one-by-one. Since all these resized images generated by the script has a same name index, an issue arises, which is when the visitor goes to save his image in windows the first time, say in a folder, it is saved as index.jpeg but when he goes to save images after that he is prompted to replace index.jpeg image. Because "these people " are not tech savvy so they usually replace the image end up wasting my time solving the case. So I would like these resized images to have unique name generated either by uniqid() or time().
WHAT I TRIED / AM TRYING : I am confused to what phpfunction I should be using here together with time() so that I could create a new filename each time a resized images is generated. In order to first set a variable, i tried to use basename() function like this, but it wont work.
$new_filename = basename($resized); echo '<br>' .$new_filename. '<br>';
Obviously it throws a warning:basename() expects parameter 1 to be string. I tried that and realized that in this case the variable $resized is a resource not a string. I am still crawling through threads for imagejpeg() at php.net in search of a solution, have not found any resource yet.
Bottomline question : how do I set or get a variable for resized file so
that I can manipulate it alongwith time() to create new names each
time.
FINAL UPDATE : #CBroe, pointed out that this is not possible so I am off to looking for an alternative.

PHP: Find total number of images having same name in a folder

I am creating code to upload images in a folder using PHP, & before upload an image I check that if any image with same name already exists in the folder by using below syntax:
if (file_exists('profilephoto/' . 'frame.gif')) {
}
But actually I don't want to restrict the user to upload more images with same name & obviously it is impossible to save two images with same name , but there is a way to just find total number of images with same name & upload the next image with appending total number + 1 with image name.
Now I just want to know that using PHP how could we find total number of images having same name in a folder?
<?php
$i = '';
while ( file_exists($filename = ('profilephoto/' . "frame$i.gif")) )
++$i;
echo $filename;
?>
Obviously it's subject to "race conditions," but that's probably the least of your concerns.

Categories