Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zipping up attached images from taxonomy & date

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?

like image 685
lukeseager Avatar asked Nov 01 '22 15:11

lukeseager


1 Answers

WordPress will return the full URL to each image, therefore you will have to convert those URLs to internal PATHS in order to add them to your ZIP file.

Here's how you would convert WordPress URLs to internal server paths:

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;
}

Now in order to get the actual URL from each attachement image, use the wp_get_attachment_image_src($attachment_id, $size); function.

The first parameter is the attachment ID, and the second (optional) is the image size wanted. It will return the URL to the image.

Here is an example using your $atts_ids variable:

$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');

Note that specifying the $size parameter will not resize the image for you. Instead, it will return the nearest size already available. If you would like to retrieve the original image uploaded in its biggest size just specify 'full'. You can set WordPress to resize uploaded images (done when upload completes) by setting your desired custom size in the WordPress Dashboard > Settings > Media.

like image 79
Community Avatar answered Nov 08 '22 07:11

Community