压缩来自分类法的附加图像&;日期


Zipping up attached images from taxonomy & date

我正在编写一个脚本,该脚本允许客户端下载在特定日期发布的特定自定义分类法中附加到帖子的所有图像。

我已经创建了一个新的管理页面,其中包括一个form,他们可以选择分类法和日期,然后提交表单

然后,表单发布到一个脚本,该脚本试图获取特定图像大小(1920px)的所有url。到目前为止,我正在运行一个循环来获取帖子,然后调用db来获取具有匹配ID的附件。

但我有点纠结于如何在数组中获取这些图像的url,以便用户可以对它们进行压缩和下载。

以下是到目前为止脚本的代码:

create_zip函数来自:http://davidwalsh.name/create-zip-php)

/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $zip->addFile($file,$file);
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        //close the zip -- done!
        $zip->close();
        //check to make sure the file exists
        return file_exists($destination);
    } else {
        return false;
    }
}
?>

(获取图像的代码)

<?php
    // get things from the form
    $club = $_POST['club'];
    $day = $_POST['day'];
    $month = $_POST['month'];
    $year = $_POST['year']; 

    // run the loop 
    $loop = new WP_Query( array( 
        'post_type' => 'sell_media_item',
        'collection' => $club,
        'include_children' => false,
        'year' => $year,
        'monthnum' => $month,
        'day' => $day,
        'fields' => 'ids',
    ) );
    if ( $post_ids = $loop->get_posts() ) {
        $post_ids = implode( ',', $post_ids );
        $atts_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_parent IN($post_ids) AND post_type = 'attachment'" );   
        $images->query( array(
            'post_mime_type' =>'image',
            'post_status' => 'published',
            'post_type' => 'attachment',
            'post__in' => $atts_ids,
        ));
    }

    //////////////////////////////////////////////
    // something here to get the image url's?
    /////////////////////////////////////////////

    // prep the files to zip
    $files_to_zip = array(
        'src/to/files.jpg'
    );
    // zip 'em!
    $result = create_zip($files_to_zip,'vip-download.zip');

?>

关于如何将特定图像缩略图大小为1920px的url放入zip文件数组,有什么想法吗?

WordPress将返回每个图像的完整URL,因此您必须将这些URL转换为内部PATHS才能将它们添加到您的ZIP文件中。

以下是如何将WordPress URL转换为内部服务器路径:

function convert_upload_url_to_path($url) {
    $path = $url;
    $upload_dir_infos = wp_upload_dir();
    $upload_url = $upload_dir_infos['baseurl']; // http://example.com/wp-content/uploads 
    $upload_path = $upload_dir_infos['basedir']; // C:'path'to'wordpress'wp-content'uploads 
    // Step 1: remove $upload_url
    $path = str_replace($upload_url, '', $path);
    // Step 2: prepend $upload_path
    $path = $upload_path.$path;
    return $path;
}

现在,为了从每个附加图像中获得实际的URL,请使用wp_get_attachment_image_src($attachment_id, $size);函数。

第一个参数是附件ID,第二个(可选)是所需的图像大小。它将返回图像的URL。

以下是使用$atts_ids变量的示例:

$files_to_zip = array();
foreach ($atts_ids as $attachement_id) {
    $image_info = wp_get_attachment_image_src($attachement_id, 'full');
    $image_url = $image_info[0];
    $image_path = convert_upload_url_to_path($image_url);
    $files_to_zip[] = $image_path;
}
// zip 'em!
$result = create_zip($files_to_zip,'vip-download.zip');

请注意,指定$size参数不会调整图像的大小。相反,它将返回最接近的现有尺寸。如果您想检索以最大大小上传的原始图像,只需指定'full'即可。您可以通过在WordPress控制面板>设置>媒体中设置所需的自定义大小,将WordPress设置为调整上传图像的大小(在上传完成时完成)。