我正在编写一个脚本,该脚本允许客户下载附加到特定自定义分类中帖子的所有图像,这些图像在特定日期发布。
我创建了包含 的新管理页面form
,他们可以选择分类和日期,然后提交表单。
然后表单发布到一个脚本,该脚本试图获取url
特定图像大小(1920px)的所有 's。到目前为止,我正在运行一个循环来获取帖子,然后db
调用以获取匹配的附件ID
。
url
但是对于如何将这些图像的 's 放在一个数组中,以便用户可以压缩和下载它们,我有点困惑。
到目前为止,这是脚本的代码:
(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 文件数组中的任何想法?