在本文中,您将会了解到关于php-用户照片库的MySQL设计的新资讯,同时我们还将为您解释php图片库的相关在本文中,我们将带你探索php-用户照片库的MySQL设计的奥秘,分析php图片库的特点,并
在本文中,您将会了解到关于php-用户照片库的MySQL设计的新资讯,同时我们还将为您解释php图片库的相关在本文中,我们将带你探索php-用户照片库的MySQL设计的奥秘,分析php图片库的特点,并给出一些关于django的mysql设置和mysql服务器闲置时间设置、ios – 删除用户照片库中的照片时,NSCocoaErrorDomain Code = -1、iOS中读取照片库及保存图片或视频到照片库的要点解析、Mysql实例mysql设置远程访问数据库的多种方法的实用技巧。
本文目录一览:- php-用户照片库的MySQL设计(php图片库)
- django的mysql设置和mysql服务器闲置时间设置
- ios – 删除用户照片库中的照片时,NSCocoaErrorDomain Code = -1
- iOS中读取照片库及保存图片或视频到照片库的要点解析
- Mysql实例mysql设置远程访问数据库的多种方法
php-用户照片库的MySQL设计(php图片库)
我正在为一个基本的照相馆设计数据库,每个用户都可以上传几个图像.
现在这就是我所拥有的:
照片库
photo_id - image - sort_order - user_id
1 - test.jpg - 1 - 1
2 - another_photo.jpg - 2 - 1
然后在我的文件夹结构上,我将创建一个新文件夹,如下所示:
images / photo-gallery /并将图像存储在其中.现在,我应该为每个user_id创建一个文件夹并将其特定图像存储在该文件夹/
因此,在这种情况下:
images / photo-gallery / 1 / test.jpg和用户1的所有照片都在那里?
另外,为了调整大小,我正在考虑使用smart image resizer,这样我就可以存储原始照片,并且如果要将其调整为特定大小,可以使用以下脚本来调用它:/image.PHP?width= 200& height = 200& image = test.jpg.
我应该对这些文件名进行哈希处理吗?我还有什么想念的吗?关于如何改善这一点的任何建议?
谢谢!
解决方法:
Now, should I create a folder for each user_id and store their specific images in that folder?
是的,以某种方式分隔上载是个好主意,这样您就不会以拥有数万个文件的目录结束.您可以按用户ID,首字母(例如images / t / te / test.jpg)或哈希(例如images / 0e / 0e4fab12.jpg)将它们分开.
Should I be hashing these file names?
这取决于您要完成的工作.由于您打算在URL中引用文件名,因此使用一个已知的“安全”字符集存储文件名可能是一个优点:
image.PHP?image=c/ca/cat%20farting%20On%20a%20lemon.jpg
-- vs --
image.PHP?image=0a/0a1b2c3d.jpg
但是,如果您这样做,我建议您扩展数据库架构以包括原始文件名:
photo_id | image | orig_fn | sort_order | user_id
1 | 0a/0a1b2c3d.jpg | charginLazors.jpg | 1 | 2
您可能还会考虑存储有关图像的其他元数据,例如上载日期,标题等.
关于文件夹结构,可以考虑使用文件名中的任意数量的字符,尽管需要考虑一些事项:
使用创建十六进制文件名的哈希方法意味着子文件夹的最大数量将是16的倍数.
>一个字符-16个子文件夹
>两个字符-256个子文件夹
>三个字符— 4096个子文件夹
如果使用两个以上的字符,则建议进一步嵌套文件夹:0a / 0a12 / 0a12bd31.jpg-或-0a / 12 / 0a12bd31.jpg.这使得导航/管理文件更加易于管理(IMO)
请记住,使用的前缀字符越多,每个文件夹中的文件就越少.如果期望高容量,则可以选择具有更多文件夹,每个文件夹较少文件.
django的mysql设置和mysql服务器闲置时间设置
服务器启动后,每个进程都会主动连接到mysql,要是长时间没有数据交互,mysql会自动断开连接。
show variables like ''%timeout%'';
闲置连接的超时时间由wait_timeout控制,默认8小时。
django的database设置:通过设置CONN_MAX_AGE<8小时,让客户端主动断开闲置的连接,避免客户端因闲置超时发生连接错误
DATABASES = {
''default'': {
# ''ENGINE'': ''django.db.backends.sqlite3'',
# ''NAME'': os.path.join(BASE_DIR, ''db.sqlite3''),
''ENGINE'': ''django.db.backends.mysql'',
''NAME'': conf.get(''mysql'', ''database''),
''USER'': conf.get(''mysql'', ''username''),
''PASSWORD'': conf.get(''mysql'', ''password''), # 密码,
''HOST'': conf.get(''mysql'', ''host''),
''PORT'': conf.get(''mysql'', ''port''),
# mysql服务器的设置是:闲置时间超过8个小时则服务端主动断开连接,因此这里设置客户端最多显示6个小时,就主动断开连接,
# 下次连接时,重新建立新的连接,参考:https://docs.djangoproject.com/en/2.2/ref/databases/ Persistent connections
''CONN_MAX_AGE'': 60*60*6, # 数据库每个连接断开的时间设置
''OPTIONS'': {
# "init_command": "SET sql_mode=''STRICT_TRANS_TABLES''",
# ''init_command'': "SET innodb_strict_mode=1",
# https://django-mysql.readthedocs.io/en/latest/checks.html
''init_command'': "SET sql_mode=''STRICT_TRANS_TABLES'', innodb_strict_mode=1",
''charset'': ''utf8mb4'',
},
# Tell Django to build the test database with the ''utf8mb4'' character set
''TEST'': {
''CHARSET'': ''utf8mb4'',
''COLLATION'': ''utf8mb4_general_ci'',
},
# 使每一个http请求都是事务性的
# ''ATOMIC_REQUESTS'': True
}
}
原文细读:
Persistent connections¶
Persistent connections avoid the overhead of re-establishing a connection to the database in each request. They’re controlled by the CONN_MAX_AGE parameter which defines the maximum lifetime of a connection. It can be set independently for each database.
The default value is 0, preserving the historical behavior of closing the database connection at the end of each request. To enable persistent connections, set CONN_MAX_AGE to a positive number of seconds. For unlimited persistent connections, set it to None.
Connection management¶
Django opens a connection to the database when it first makes a database query. It keeps this connection open and reuses it in subsequent requests. Django closes the connection once it exceeds the maximum age defined by CONN_MAX_AGE or when it isn’t usable any longer.
In detail, Django automatically opens a connection to the database whenever it needs one and doesn’t have one already — either because this is the first connection, or because the previous connection was closed.
At the beginning of each request, Django closes the connection if it has reached its maximum age. If your database terminates idle connections after some time, you should set CONN_MAX_AGE to a lower value, so that Django doesn’t attempt to use a connection that has been terminated by the database server. (This problem may only affect very low traffic sites.)
At the end of each request, Django closes the connection if it has reached its maximum age or if it is in an unrecoverable error state. If any database errors have occurred while processing the requests, Django checks whether the connection still works, and closes it if it doesn’t. Thus, database errors affect at most one request; if the connection becomes unusable, the next request gets a fresh connection.
Caveats¶
Since each thread maintains its own connection, your database must support at least as many simultaneous connections as you have worker threads.
Sometimes a database won’t be accessed by the majority of your views, for example because it’s the database of an external system, or thanks to caching. In such cases, you should set CONN_MAX_AGE to a low value or even 0, because it doesn’t make sense to maintain a connection that’s unlikely to be reused. This will help keep the number of simultaneous connections to this database small.
The development server creates a new thread for each request it handles, negating the effect of persistent connections. Don’t enable them during development.
When Django establishes a connection to the database, it sets up appropriate parameters, depending on the backend being used. If you enable persistent connections, this setup is no longer repeated every request. If you modify parameters such as the connection’s isolation level or time zone, you should either restore Django’s defaults at the end of each request, force an appropriate value at the beginning of each request, or disable persistent connections.
参考:
https://www.cnblogs.com/li1234yun/p/7729800.html
https://docs.djangoproject.com/en/2.2/ref/databases/
ios – 删除用户照片库中的照片时,NSCocoaErrorDomain Code = -1
NSArray *toDeletePhotos = [photos valueForKey:@"asset"]; [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetChangeRequest deleteAssets:toDeletePhotos]; } completionHandler:^(BOOL success,NSError *error) { if (success) { dispatch_async(dispatch_get_main_queue(),^{ [self refreshPhotosAfterDeleting]; }); } }];
我在大约8台设备上进行了测试.其中6个成功删除了所选照片,其中2个返回并出现错误:Error Domain = NSCocoaErrorDomain Code = -1“操作无法完成.(Cocoa error -1.)”我测试的两个设备是6和5s.
我无法弄清楚它是什么错误,并想知道有谁可以帮助我.谢谢!
解决方法
事实证明,当照片从其他设备流式传输/同步时,你无法删除它们而不在iTunes / iCoud上删除它们.所以我添加了一个过滤器,因此没有提取流式/同步的照片.
有关更多信息,请参阅: https://support.apple.com/en-us/HT204120. 希望这可以帮助!
iOS中读取照片库及保存图片或视频到照片库的要点解析
读取照片库PhotoLibrary
iOS中如果我们只有一次读取一张图片或者一个视频(或拍一张照片/视频)的需求,那么我们用 UIImagePickerController 就可以搞定。但是很多时候我们需要一次性从PhotoLibrary读取多个照片或者视频,这时候我们就需要另辟蹊径了,好在apple为我们提供了相应的接口。
在开始coding之前我们想要认识几个类:
ALAssetsLibrary:代表整个PhotoLibrary,我们可以生成一个它的实例对象,这个实例对象就相当于是照片库的句柄。
ALAssetsGroup:照片库的分组,我们可以通过ALAssetsLibrary的实例获取所有的分组的句柄。
ALAsset:一个ALAsset的实例代表一个资产,也就是一个photo或者video,我们可以通过他的实例获取对应的subnail或者原图等等。
还需要了解的一个东东就是blocks,apple在iOS 4.0以后大量出现了这玩意儿,有使用越来越广的意思,不过这玩意儿确实好用。关于这玩意儿的内容我在这里不多讲,关注我的博客我会细讲。
对于本文的需求,我们读取group和每个asset都是异步的,但是我们现在用blocks我们可以在一个函数里面搞定。所以blocks确实很方便。
下面直接看代码吧:
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];//生成整个photolibrary句柄的实例
NSMutableArray *mediaArray = [[NSMutableArray alloc]init];//存放media的数组
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {//获取所有group
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {//从group里面
NSString* assetType = [result valueForProperty:ALAssetPropertyType];
if ([assetType isEqualToString:ALAssetTypePhoto]) {
NSLog(@"Photo");
}else if([assetType isEqualToString:ALAssetTypeVideo]){
NSLog(@"Video");
}else if([assetType isEqualToString:ALAssetTypeUnknown]){
NSLog(@"Unknow AssetType");
}
NSDictionary *assetUrls = [result valueForProperty:ALAssetPropertyURLs];
NSUInteger assetCounter = 0;
for (NSString *assetURLKey in assetUrls) {
NSLog(@"Asset URL %lu = %@",(unsigned long)assetCounter,[assetUrls objectForKey:assetURLKey]);
}
NSLog(@"Representation Size = %lld",[[result defaultRepresentation]size]);
}];
} failureBlock:^(NSError *error) {
NSLog(@"Enumerate the asset groups failed.");
}];
保存图片或视频到PhotoLibrary
时文件然后,然后通过临时文件的路径去转存到photo library。
我们直接来看相应的API:
// These methods can be used to add photos or videos to the saved photos album.
// With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;
// The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1);
// If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten
- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1);
- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock;