【问题标题】:How to display image from server (newest-oldest) to a new php page?如何将图像从服务器(最新最旧)显示到新的 php 页面?
【发布时间】:2013-10-27 09:37:17
【问题描述】:

我有一个创建图像的表单(使用 imagejpeg、imagettftext 和 imagecreatefromjpeg)并将其保存在服务器的文件夹中。我需要做的是将所有创建的图像显示到另一个页面,最新的在顶部,最旧的在底部。我有一个名为 formdisplay.php 的表单,但它只是显示损坏的图像,而不是最新到最旧的。希望你能帮助我。真的需要让这个工作。提前感谢您的帮助。

我已经阅读了这些帖子,但没有一个对我有用。

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">';

请随时编辑我的代码。谢谢:)

【问题讨论】:

  • print '&lt;img src="' .$show_file. '" alt="Image Title Here"&gt;';置于$show_file = "$base_url/$file";下方
  • 嗨@E L 我已经尝试过你的建议。但它仍然返回破碎的img。这一次,3张破碎的图像。请注意,我尝试显示的图像是使用 imagejpeg 和 imagecreatefromjpeg 创建和保存的。希望你能给我进一步的帮助。谢谢
  • 那是因为你的$base_url 有尾文件名。让它$base_url = 'http://birdieonawire.com/Amadeus/uploader/uploader/';(真的是uploader/uploader/ dirs 那里吗?)
  • 是的,我确定。我尝试使用 $base_url = 'birdieonawire.com/Amadeus/uploader/uploader';但它给了我 2 张破碎的图像。一个链接在错误日志中,另一个链接在我用来在创建的图像中写东西的字体中
  • 那么这是$dir 的问题。尝试使用相对路径或将echo $show_file,'&lt;br&gt;'; 放在$show_file = "$base_url/$file"; 下方并查看它的输出。

标签: php


【解决方案1】:

这是在创建 $images 数组(图像文件名列表)的 while 循环之后进行的

 foreach ($images as $image)

 {

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

 $sortedimages[]$filetime => $image;

 }

 krsort($sortedimages);


 echo "<pre>";

 print_r($sortedimages);

 echo "</pre>"

 ?>

【讨论】:

  • 您好,$sortedimages[]$filetime => $image; 行中显示了一个 sysntax 错误
  • 谢谢@JbenKaye 正确的代码是 $sortedimages[$filetime] = $image;
【解决方案2】:
  1. 从文件夹中读取图像。
  2. 循环遍历它们并使用图像的关键文件时间插入到数组中。
  3. 使用 krsort 函数按日期对它们进行排序。
  4. 循环显示它们。

我认为此链接将对您有所帮助: http://forums.phpfreaks.com/topic/219466-php-displaying-images-from-folder-with-most-recent-first/

【讨论】:

  • 嗨@Hanny Harby!我在您提供的链接上看到了代码。它正在使用数组。我的图像没有保存在数组中。我该怎么办?将我的图像保存为数组?并将其作为数组检索?谢谢
  • 是的@Jben Kaye 从服务器获取图像后。遍历它们并按 filetime 将它们保存到数组中。然后按时间排序数组
  • 你能给我一些示例代码吗?我不熟悉使用数组。谢谢!
猜你喜欢
  • 2019-04-10
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-11
  • 2021-07-20
  • 1970-01-01
相关资源
最近更新 更多