我正在使用 PHP 中的 glob 函数来浏览目录并尝试仅匹配图像文件。这是可行的,但是当我尝试计算每个目录中的图像时,它会为其他目录重复第一个目录的计数。例如,目录 1 有 6 张图像,目录 2 有 4 张图像,但是当我尝试显示每个目录的计数时,它显示目录 2 的 6 张图像,依此类推。
这是我的代码:
public function viewphotoalbumsAction()
{
$identity = $this->identity();
$dirname = array();
$files = array();
foreach (glob(getcwd() . '/public/images/profile/' . $identity . '/albums/*', GLOB_ONLYDIR) as $dir) {
$dirname[] = basename($dir);
foreach (glob($dir . '/*.{jpg,png,gif}', GLOB_BRACE) as $images) {
$files[] = $images;
}
}
//var_dump($files); exit;
$data = array(
'albums' => array_values($dirname),
'files' => $files,
);
return new ViewModel(array('album' => $data['albums'], 'files' => $data['files']));
}
var_dump()的结果
array(9) { [0]=> string(96) "C:\xampp\htdocs/public/images/profile/fooboy/albums/bone mom's album_2017-07-03/massive snow.jpg"
[1]=> string(91) "C:\xampp\htdocs/public/images/profile/fooboy/albums/bone mom's album_2017-07-03/mom-jon.jpg"
[2]=> string(90) "C:\xampp\htdocs/public/images/profile/fooboy/albums/bone mom's album_2017-07-03/sunset.jpg"
[3]=> string(85) "C:\xampp\htdocs/public/images/profile/fooboy/albums/random photos_2017-07-02/cref.jpg"
[4]=> string(88) "C:\xampp\htdocs/public/images/profile/fooboy/albums/random photos_2017-07-02/diploma.jpg"
[5]=> string(86) "C:\xampp\htdocs/public/images/profile/fooboy/albums/random photos_2017-07-02/eeyor.jpg"
[6]=> string(93) "C:\xampp\htdocs/public/images/profile/fooboy/albums/random photos_2017-07-02/frother-jaws.jpg"
[7]=> string(88) "C:\xampp\htdocs/public/images/profile/fooboy/albums/random photos_2017-07-02/frother.jpg"
[8]=> string(93) "C:\xampp\htdocs/public/images/profile/fooboy/albums/random photos_2017-07-02/goat_singing.jpg" }
它正在按预期从每个目录获取所有图像,但我真正需要做的是将图像分开,这样我就可以对每个目录进行准确计数,而不仅仅是让它显示整个计数 (9)
查看代码:
<div class="w3-col m7">
<div class="w3-row-padding">
<div class="w3-col m12">
<div class="w3-card-2 w3-round w3-white">
<div class="w3-container w3-padding" id="view-photo-albums">
<p class="w3-center">Current Albums</p>
<br>
<?php
foreach ($this->album as $albums):
?>
<p>
<?php echo $albums; ?> - Number of images: <?php echo $this->files; ?>
</p>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
如有任何帮助,我们将不胜感激。
更新:
我不能像对$data['albums']那样对$data['files']使用array_values给出数组到字符串转换的警告。
最佳答案
这段代码有问题:
foreach (glob($dir . '/*.{jpg,png,gif}', GLOB_BRACE) as $images) {
$files[] = $images;
}
变量 $dir 用于迭代第一次调用 glob() 返回的数组:
foreach (glob(...) as $dir) {
...
}
它在第一个 foreach 循环结束后的值是在 foreach 循环中分配给它的最后一个值。
最后,$data['albums'] 包含所有目录名称,$data['files'] 包含最后一个目录中的文件名称$data['albums'] 中列出的目录。
I cannot use array_values for $data['files'] as I did for $data['albums'] as it is giving a warning of array to string conversion.
array_values()不会为 $dirname 和 $files 生成任何新内容。如果它们被创建(!),这两个变量包含由以 0 开头的序列号索引的值;这正是 array_values() 返回的内容。
您发布的代码的意图对我来说不是很清楚。我假设您想显示相册列表以及每个相册包含多少张图像。
我会这样做:
public function viewphotoalbumsAction()
{
$identity = $this->identity();
// Always initialize the arrays before putting values into them.
// Without this, if the first glob() returns an empty array, the outer
// foreach loop never runs and both $dirname and $files end up being undefined
// and this produces trouble in the code that uses these variables later.
$dirname = array();
// This will be a two dimensional array. It is indexed by directory
// names and contains the lists of files for each directory.
$files = array();
// The outer loop enumerates the albums
foreach (glob(getcwd() . '/public/images/profile/' . $identity . '/albums/*', GLOB_ONLYDIR) as $dir) {
// The directory name is the album name
$albumName = basename($dir);
// Put the album name in $dirname[]
// This is not really needed as we also have the album name
// as key in $files but can be useful if you want to keep more
// information about each album
$dirname[] = $albumName;
// Initialize the list of images of this album
$files[$albumName] = array();
// The inner loop enumerates the images of this directory
foreach (glob($dir . '/*.{jpg,png,gif}', GLOB_BRACE) as $images) {
$files[] = $images;
}
}
// Prepare the data for display
$data = array(
'albums' => $dirname,
'files' => $files,
);
return new ViewModel($data);
}
View (忽略了 HTML 包装器,就这样没问题):
<?php foreach ($this->files as $albumName => $listFiles): ?>
<p>
<?php echo $albumName; ?> - Number of images: <?php echo count($listFiles); ?>
</p>
<?php endforeach; ?>
需要注意的是,内部的 foreach 循环甚至都不需要,因为它所做的只是将 glob() 返回的数组中的值一个一个地复制到一个新数组。
将 glob() 返回的值存储到 $files[$albumName] 中会更快、更容易阅读和理解。由于存储在 $dirname 中的值从未被使用过,因此可以完全省略该变量并且函数变得更短(省略注释):
public function viewphotoalbumsAction()
{
$identity = $this->identity();
$files = array();
foreach (glob(getcwd().'/public/images/profile/'.$identity.'/albums/*', GLOB_ONLYDIR) as $dir) {
$albumName = basename($dir);
$files[$albumName] = glob($dir . '/*.{jpg,png,gif}', GLOB_BRACE);
}
return new ViewModel(array('files' => $files));
}
关于php - 对两个目录重复图像计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44893586/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我正在阅读一本关于Ruby的书,作者在编写类初始化定义时使用的形式与他在本书前几节中使用的形式略有不同。它看起来像这样:classTicketattr_accessor:venue,:datedefinitialize(venue,date)self.venue=venueself.date=dateendend在本书的前几节中,它的定义如下:classTicketattr_accessor:venue,:datedefinitialize(venue,date)@venue=venue@date=dateendend在第一个示例中使用setter方法与在第二个示例中使用实例变量之间是
尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot
我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
是否可以在应用程序中包含的gem代码中知道应用程序的Rails文件系统根目录?这是gem来源的示例:moduleMyGemdefself.included(base)putsRails.root#returnnilendendActionController::Base.send:include,MyGem谢谢,抱歉我的英语不好 最佳答案 我发现解决类似问题的解决方案是使用railtie初始化程序包含我的模块。所以,在你的/lib/mygem/railtie.rbmoduleMyGemclassRailtie使用此代码,您的模块将在
我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p
在我让另一个人重做我的前端UI之前,我的Rails应用程序运行平稳。我已经尝试解决此错误3天了。这是错误:Nosuchfileordirectory-identifyExtractedsource(aroundline#59):575859606162@post=Post.find(params[:id])authorize@postif@post.update_attributes(post_params)flash[:notice]="Postwasupdated."redirect_to[@topic,@post]else{"utf8"=>"✓","_method"=>"patc