1

我正在使用 jQuery-Image-Gallery jQuery 插件。它是一个全屏 jQuery 照片库。我下载了它,并且在它的演示中运行良好。它使用 ajax 请求将图像加载到 flickr。如下;

$.ajax({
    url: 'http://api.flickr.com/services/rest/',
    data: {
        format: 'json',
        method: 'flickr.interestingness.getList',
        api_key: '7617adae70159d09ba78cfec73c13be3'
    },
 dataType: 'jsonp',
    jsonp: 'jsoncallback'
}).done(function (data) {
    var gallery = $('#gallery'),
        url;
    $.each(data.photos.photo, function (index, photo) {
        url = 'http://farm' + photo.farm + '.static.flickr.com/' +
            photo.server + '/' + photo.id + '_' + photo.secret;
        $('<a rel="gallery"/>')
            .append($('<img>').prop('src', url + '_s.jpg'))
            .prop('href', url + '_b.jpg')
            .prop('title', photo.title)
            .appendTo(gallery);
    });
});

这是完美的工作。但我想在images/photos文件夹中显示我的本地文件(在我的本地主机/服务器中)。我有 PHP 服务器。我怎样才能做到这一点?

我想知道 Ajax JSON 回调结构,以便我们可以使用自定义 PHP 文件人为地重新创建它。

4

4 回答 4

2

PHP:

<?php 

    $path = dirname(__FILE__). '/images/photo'; // you may need to change the path

    /**
     Use 'opendir(".")' if the PHP file is in the same folder as your images. 
     Or set a relative path 'opendir("../path/to/folder")'.
    */

    $folder = opendir($path); 

    $pic_types = array("jpg", "jpeg", "gif", "png"); // restrict the extension [optional]

    $index = array();

    // looping over the images and push them into array

    while ($file = readdir ($folder)) {

      if(in_array(substr(strtolower($file), strrpos($file,".") + 1),$pic_types))
        {
            array_push($index,$file);
        }
    }
    closedir($folder); // close the dir opended by opendir()

    echo json_encode(array('images' => $index)); // sending to array of images as JSON
?>

jQuery:

$.ajax({
    url: 'images.php', // assume that you php file name is images.php [change as you need]
    dataType: 'json', // as you send request to same domain, so you don't need jsonp
}).done(function (data) {
    var gallery = $('#gallery'),
        url = '';
    // data.images contain the name of images as array

    $.each(data.image, function (index, photo) {
        url = '/images/photo/' + photo; // photo will contain only image name
        $('<a rel="gallery"/>')
            .append($('<img>').prop('src', url + '_s.jpg'))
            .prop('href', url + '_b.jpg')
            .prop('title', photo.title)
            .appendTo(gallery);
    });
});
于 2012-05-11T17:31:00.590 回答
1

因为数组名称在 PHP 中是 'images'

echo json_encode(array('images' => $index)); // sending to array of images as JSON

jQuery中需要相同的数组名称(图像---->图像)

$.each(data.images, function (index, photo) {
于 2012-06-18T02:34:47.450 回答
0

您需要将此脚本中的 url 更改为本地文件。然后你需要编写一个将json字符串输出到ajax的php脚本。这可能更容易使用http://husbandman.org/husbandman/fotoexpose/

于 2012-05-11T17:26:25.440 回答
0
$.ajax({
            type: 'POST',
            url: 'ajax.php',
            dataType: 'json',
            cache: false,
            success: function(result) {
                $.each(result, function(key,val){


var gallery = $('#gallery'), url;
            url = val;
            $('<a rel="gallery"/>')
                .append($('<img>').prop('src', 'images/thumbs/' + url))
                .prop('href', 'images/photos/' + url)
                .prop('title', url)
                .appendTo(gallery);

                });
           },
    });
于 2012-05-24T13:06:20.330 回答