我正在编写一个脚本,该脚本允许客户端下载在特定日期发布的特定自定义分类中的帖子所附的所有图像。
我创建了包含表单的新管理页面,他们可以选择分类法和日期,然后提交表单。
然后表单会发送到一个脚本,该脚本试图获取特定图像大小 (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/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s