0

i have a form that create images (using imagejpeg, imagettftext and imagecreatefromjpeg) and save it on a folder in the server. What i need to do is to display ALL created images to another page, the newest on the top and so that the oldest is at the bottom. i have a form called formdisplay.php but it's just displaying a broken image and not newest to oldest. hope you can help me with this. really need to get this working. thanks in advance for your help.

i have read the posts but none of those worked for me.

Pull dedicated images from folder - show image + filename (strip part of filename + file-extension)

How can I display latest uploaded image first? (PHP+CSS)

getting images from the server and display them

Displaying images from folder in php

display image from server embedding php in html

formcreatesave.php

    imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);

$date_created = date("YmdHis");//get date created
$img_name = "-img_entry.jpg"; //the file name of the generated image
$img_newname = $date_created . $img_name; //datecreated+name
$img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save 
imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality

imagedestroy($im);

formdisplay.php

$dir = '/home3/site/public_html/Master/uploader/uploader';
$base_url = 'http://mysite.com/Master/uploader/uploader/20131027024705-img_entry.jpg';
$newest_mtime = 0;
$show_file = 'BROKEN';
if ($handle = opendir($dir)) {
 while (false !== ($file = readdir($handle))) {
    if (($file != '.') && ($file != '..')) {
       $mtime = filemtime("$dir/$file");
       if ($mtime > $newest_mtime) {
          $newest_mtime = $mtime;
          $show_file = "$base_url/$file";
       }
    }
  }
}
print '<img src="' .$show_file. '" alt="Image Title Here">';

please feel free to edit my code. thanks :)

Community
  • 1
  • 1
Jben Kaye
  • 171
  • 1
  • 5
  • 16
  • place `print 'Image Title Here';` below `$show_file = "$base_url/$file";` – el Dude Oct 27 '13 at 09:42
  • hi @E L i have tried your suggestion. but its still returning broken img. this time, 3 broken images. please take note that the image i was trying to display was created and saved using imagejpeg and imagecreatefromjpeg. hope you can give me further help. thanks – Jben Kaye Oct 27 '13 at 09:49
  • That's because your `$base_url` has tailing filename. Make it `$base_url = 'http://birdieonawire.com/Amadeus/uploader/uploader/';` (is it really `uploader/uploader/` dirs there?) – el Dude Oct 27 '13 at 10:05
  • Yes it's there im sure. i tried using $base_url = 'http://birdieonawire.com/Amadeus/uploader/uploader/'; but its giving me 2 broken images. One linked in an errorlog the other in the font i used to write something in the created image – Jben Kaye Oct 27 '13 at 10:10
  • then it's `$dir` problem. Try to use relative path or just place `echo $show_file,'
    ';` below `$show_file = "$base_url/$file";` and see what it outputs.
    – el Dude Oct 27 '13 at 10:55
  • i tried it but it's printing links of the formdisplay.php file, error log and font used – Jben Kaye Oct 27 '13 at 10:59
  • then its wrong `$dir` path. Use relative – el Dude Oct 27 '13 at 12:26
  • its returning abroken image still. but now the link of the broken image is http://birdieonawire.com/Amadeus/uploader/uploader/BROKEN so is it the $show_file? – Jben Kaye Oct 27 '13 at 12:41
  • and that's because it's empty dir in your `$dir` var. =) I don't know where's php file is, but I think if you set `$dir` var as `$dir='Master/uploader/uploader';` then it should dig the right dir )) – el Dude Oct 27 '13 at 13:55
  • the php file is in the 'Master/uploader/uploader'. the same folder where the images i want to display is. – Jben Kaye Oct 27 '13 at 14:05
  • maybe problem is in `$newest_mtime = $mtime;`. that depends on the order of files in this dir. Just comment it `//` for testing – el Dude Oct 27 '13 at 23:02
  • ok. i'll do that. i'll let you know what's the result. thanks – Jben Kaye Oct 28 '13 at 05:43

2 Answers2

0
  1. Read images from folder.
  2. Loop through them and insert into array with the key filetime of image.
  3. Use krsort function to sort them according to date.
  4. Loop and display them.

I think this link will help you: http://forums.phpfreaks.com/topic/219466-php-displaying-images-from-folder-with-most-recent-first/

ritesh
  • 2,245
  • 2
  • 25
  • 37
Hany HarbY
  • 68
  • 7
  • Hi @Hanny Harby! I saw the code on the link you gave. it's using array. my images are not saved in an array. what can i do about this? save my image as an array? and retrieve it as an array also? thanks – Jben Kaye Oct 27 '13 at 09:53
  • Yes @Jben Kaye After you get images from server . loop through them and save them into array by filetime . then sort array by time – Hany HarbY Oct 27 '13 at 09:56
  • can you give me some sample code to do this? im not familiar with using array. thanks! – Jben Kaye Oct 27 '13 at 10:00
0

This goes after the while loop that creates the $images array (list of image filenames

 foreach ($images as $image)

 {

 $filetime = filemtime("images1/$image");

 $sortedimages[]$filetime => $image;

 }

 krsort($sortedimages);


 echo "<pre>";

 print_r($sortedimages);

 echo "</pre>"

 ?>
Hany HarbY
  • 68
  • 7