如何使用干预图像和Laravel 5.2创建和使用图像模板

时间:2016-05-23 14:24:53

标签: laravel laravel-5.2 intervention

我在Laravel 5.2应用中使用Intervention Image library以及Image Cache plugin

我一直在使用预定义的模板,没有类似的问题:

{{ route('imagecache', ['template' => 'medium', 'filename' => 'image.jpg']) }}"

我在文档中看到,除了小,中,大的默认大小,你可以创建图像过滤器来创建自定义操作并在配置文件中将它们定义为模板,这样我就可以通过我的模板而不是媒介名称。文档引用图像过滤器作为一种方法来做到这一点,但它有点粗略,以确切如何做到这一点。有谁知道你是怎么做到的?

1 个答案:

答案 0 :(得分:2)

// Here is where I refresh the data and sort it based on last name - (void)refreshData { [[PCMSSessionManager sharedSession] refreshPCMSDataWithCompletion:^(BOOL success, NSString *errorMessage, id resultObject) { if (success) { NSLog(@"yay!"); self.membersArray = [[PCMSSessionManager sharedSession] memberArr]; // Let's sort the array self.sortedArray = [self.membersArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { NSString *first = [(PCMSMember*)a lastName]; NSString *second = [(PCMSMember*)b lastName]; return [first compare:second]; }]; [self.tableView reloadData]; } else { NSLog(@"boooo!!!!"); } }]; } - (NSDictionary *)indexedMembers { NSMutableDictionary *indexedContacts = [NSMutableDictionary new]; for (PCMSMember *member in self.sortedArray) { NSString *sortString = member.lastName; NSString *sortLetter = [sortString substringToIndex:1]; /* see if that letter already exists as an index */ BOOL foundKey = NO; for (NSString *key in [indexedContacts allKeys]) { if ([key isEqualToString:sortLetter]) { foundKey = YES; } } NSMutableArray *valueArray; if (foundKey) { valueArray = [((NSArray *)indexedContacts[sortLetter]) mutableCopy]; } else { valueArray = [NSMutableArray new]; } [valueArray addObject:member]; indexedContacts[sortLetter] = [valueArray copy]; } return [indexedContacts copy]; } // Here's my table data - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[[self indexedMembers] allKeys] count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSDictionary *indexedContacts = [self indexedMembers]; NSArray *myKeys = [indexedContacts allKeys]; NSString *key = myKeys[section]; return [((NSArray *)[self indexedMembers][key]) count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; // Configure the cell... if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } if (self.isPhysician == YES) { NSString *key = [[self indexedMembers] allKeys][indexPath.section]; PCMSMember *currentMember = ((NSArray *)[self indexedMembers][key])[indexPath.row]; cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", currentMember.firstName, currentMember.lastName]; } return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [[self indexedMembers] allKeys][section]; } 内有一个config/imagecache.php键,您可以在这里添加自己的密钥。

例如:

templates

然后你只需要创建类'templates' => [ // ... 'x-large' => 'App\Filters\ExtraLarge', // ... ],

App\Fitlers\ExtraLarge方法中,您可以根据documentation调用applyFilter()属性上的任何方法。

$image

然后在<?php namespace App\Filters; use Intervention\Image\Image; use Intervention\Image\Filters\FilterInterface; class ExtraLarge implements FilterInterface { public function applyFilter(Image $image) { return $image->fit(1300, 1000); } } 帮助器中将模板的值设置为route

x-large