I'm working on a script that allows a client to download all images attached to posts within a specific custom taxonomy, published on a specific date.
I've created the new admin page that includes a form
, they can select the taxonomy and the date, then submit the form.
The form then posts to a script that is trying to get all of the url
's for a specific image size (1920px). So far, I'm running a loop to get the posts, then a db
call to get the attachments with matching ID
's.
But I'm a little stuck as to how I'm going to get the url
's of these images inside an array so that they can be zipped up and downloaded by the user.
Here's the code for the script so far:
(create_zip
function from: 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;
}
}
?>
(code to get the images)
<?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');
?>
Any ideas as to how I'd then get the url to the specific image thumbnail size of 1920px into the zip file array?