客户端一直在上传huuuge图像,然后抱怨服务器内存不足。
当然,人们可以增加内存量,但这只是引入了一场军备竞赛。
我可以添加哪些挂钩来对上载的文件施加最大图像大小(尺寸,而不是文件大小),例如,不,你不能上载800万像素的快照,请先将其大小调整为<;200万像素。
要清楚,我说的是图像大小,而不是文件大小,也就是图像高度和图像宽度
客户端一直在上传huuuge图像,然后抱怨服务器内存不足。
当然,人们可以增加内存量,但这只是引入了一场军备竞赛。
我可以添加哪些挂钩来对上载的文件施加最大图像大小(尺寸,而不是文件大小),例如,不,你不能上载800万像素的快照,请先将其大小调整为<;200万像素。
要清楚,我说的是图像大小,而不是文件大小,也就是图像高度和图像宽度
基本上你只需通过getimagesize()
, 一个基本的PHP函数,然后用notes处理错误。
<?php
/** Plugin Name: (#67107) »kaiser« Restrict file upload via image dimensions */
function wpse67107_restrict_upload( $file )
{
$file_data = getimagesize( $file );
// Handle cases where we can\'t get any info:
if ( ! $file_data )
return $file;
list( $width, $height, $type, $hwstring, $mime, $rgb_r_cmyk, $bit ) = $file_data;
// Add conditions when to abort
if ( 3200728 < $width * $height )
{
// I added 100k as sometimes, there are more rows/columns
// than visible pixels, depending on the format
$file[\'error\'] = \'This image is too large, resize it prior to uploading, ideally below 3.2MP or 2048x1536 px.\';
}
return $file;
}
add_filter( \'wp_handle_upload_prefilter\', \'wpse67107_restrict_upload\' );
这是我的看法
<?php
/**
* Plugin Name: Deny Giant Image Uploads
* Description: Prevents Uploads of images greater than 3.2MP
*/
function tomjn_deny_giant_images($file){
$type = explode(\'/\',$file[\'type\']);
if($type[0] == \'image\'){
list( $width, $height, $imagetype, $hwstring, $mime, $rgb_r_cmyk, $bit ) = getimagesize( $file[\'tmp_name\'] );
if($width * $height > 3200728){ // I added 100,000 as sometimes there are more rows/columns than visible pixels depending on the format
$file[\'error\'] = \'This image is too large, resize it prior to uploading, ideally below 3.2MP or 2048x1536\';
}
}
return $file;
}
add_filter(\'wp_handle_upload_prefilter\',\'tomjn_deny_giant_images\');
我知道您只需要根据Image Dimension. 但我只想添加一个注释,这不会阻止您的服务器Out of Memory. Image dimensions
一旦数据完全上传到服务器(如Apache)上,就可以使用,因此会消耗服务器内存。
您应该考虑在Apache和PHP级别限制上传大小,如果需要,还可以在Wordpress级别限制图像维度。要在Apache中设置最大上载大小,
<Directory "/var/www/wordpress-site/wp-uploads">
LimitRequestBody 10485760
</Directory>
在php中。ini,设置:memory_limit = 64M
upload_max_filesize = 10M
post_max_size = 20M
上载的文件无法移动到/home/reascom/public\\u html/wp content/uploads/2012/07。这是我尝试将任何内容加载到现有网站时收到的消息。图像和主题会收到此消息。我该怎么办?