使用 Javascript 或 PHP 保存网页中的图像

发布于 2024-10-26 18:56:17 字数 423 浏览 5 评论 0原文

我用 jquery 编写了一个小的 javascript 应用程序,它通过 Google Feed API 获取 rss feed 并将其显示为图像网格。

代码如下所示:

<ul>
...
<li><a class="entry" href="LINK_TO_BIG_IMAGE" title="TITLE_OF_ENTRY"><img class="entry-img" src="THUMB_IMG" alt="" /></a></li>
...
</ul>

我想要做的是能够将所有图像一次保存在服务器上的文件夹中。现在,如果它在页面加载时执行此操作,我会很高兴。可以通过 JavaScript 实现吗?你有什么方向可以指点我吗?

提前致谢 !

I wrote a little javascript application with jquery which fetches a rss feed via the Google Feed API and displays it as a grid of images.

The code then looks like :

<ul>
...
<li><a class="entry" href="LINK_TO_BIG_IMAGE" title="TITLE_OF_ENTRY"><img class="entry-img" src="THUMB_IMG" alt="" /></a></li>
...
</ul>

What I want to do is to be able to save the images all at once in a folder on the server. For now I'd be happy if it does it on page load. Is it possible via Javascript ? Do you have some direction to point me to ?

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2024-11-02 18:56:17

您关于将路径提交到服务器上的 php 脚本的评论将起作用(我不建议这样做,但它起作用)。

然后你的 php 可以像这样下载图像(未经测试,当你保存文件时,你需要从 $url 解析文件名)。

<?php

$image_urls = isset($_POST['image_urls']) ? $_POST['image_urls'] : null;//assume it's an array of urls

if (!count($image_urls))//check that our array actually contains 1 or more urls
{
  die('no urls provided');
}

$image_folder_path = '/home/somefolder/';

foreach ($image_paths as $index => $url)
{
  $file_name = $image_folder_path . $index . '.jpg';//you will need to parse the url for the file name and extension for this to be accurate

  $image = file_get_contents($url);//if fopen wrappers is enabled you can do this otherwise use something like cUrl to fetch the file
  file_put_contents($file_name, $image);
}

?>

Your comment about submitting the paths to a php script on the server would work ( I wouldn't recommend doing it that way but it works).

Your php could then download the images like so (untested and you'd need to parse the filename from the $url when you go to save the file).

<?php

$image_urls = isset($_POST['image_urls']) ? $_POST['image_urls'] : null;//assume it's an array of urls

if (!count($image_urls))//check that our array actually contains 1 or more urls
{
  die('no urls provided');
}

$image_folder_path = '/home/somefolder/';

foreach ($image_paths as $index => $url)
{
  $file_name = $image_folder_path . $index . '.jpg';//you will need to parse the url for the file name and extension for this to be accurate

  $image = file_get_contents($url);//if fopen wrappers is enabled you can do this otherwise use something like cUrl to fetch the file
  file_put_contents($file_name, $image);
}

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文