我正在编写一个脚本,该脚本允许客户端下载在特定日期发布的特定自定义分类中的帖子所附的所有图像。
我创建了包含表单
的新管理页面,他们可以选择分类法和日期,然后提交表单。
然后表单会发送到一个脚本,该脚本试图获取特定图像大小 (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');
?>
关于如何将 1920 像素的特定图像缩略图大小的 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 设置为调整上传图像的大小(在上传完成时完成)。
关于php - 从分类法和日期压缩附加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18486641/