我想我所要做的很特别。我正在使用WC供应商PRO插件。卖家通过前端有4个上传区域。他们可以上传个人资料图像、店铺横幅和产品图像。还有一个上传zip文件的地方。
我需要的是上传的图像为每个区域所需的大小。例如:商店横幅需要为800x100。产品图像必须为800x450,配置文件图像必须为200x200。
我想发生的是,当他们试图上传一个不是我指定的大小的文件时,这将根本不允许他们上传并给出一个错误。我能够接近这个脚本(附件),但它会影响上传的所有区域,包括通过前端(包括zip文件)。
我知道实际的网站将通过css显示这些图像,但这不是我想要的。在一个有1000个上传程序的站点上进行此操作时,会有太多的裁剪、扭曲、偏心等,结果会变得一团糟,看起来很糟糕。
这段代码几乎可以正常工作。它执行它应该执行的操作,但它会影响上载的所有区域,包括zip文件。因此,它在zip文件中出错,因为它没有“维度”,而我想要的是200x200图像,它强制它为800x450。如果有人知道我如何解决这个问题,请告诉我。我愿意付钱给能做这件事的人,只是我还没有找到那个人。
// check for file upload size //
{
if( !current_user_can( \'administrator\') )
add_filter( \'wp_handle_upload_prefilter\', \'mdu_validate_image_size\' );
}
add_filter(\'wp_handle_upload_prefilter\',\'mdu_validate_image_size\');
function mdu_validate_image_size( $file ) {
$image = getimagesize($file[\'tmp_name\']);
$minimum = array(
\'width\' => \'800\',
\'height\' => \'450\'
);
$maximum = array(
\'width\' => \'800\',
\'height\' => \'450\'
);
$image_width = $image[0];
$image_height = $image[1];
$too_small = "Image dimensions are too small. Minimum size is {$minimum[\'width\']} by {$minimum[\'height\']} pixels. Uploaded image is $image_width by $image_height pixels.";
$too_large = "Image dimensions are too large. Maximum size is {$maximum[\'width\']} by {$maximum[\'height\']} pixels. Uploaded image is $image_width by $image_height pixels.";
if ( $image_width < $minimum[\'width\'] || $image_height < $minimum[\'height\'] ) {
// add in the field \'error\' of the $file array the message
$file[\'error\'] = $too_small;
return $file;
}
elseif ( $image_width > $maximum[\'width\'] || $image_height > $maximum[\'height\'] ) {
//add in the field \'error\' of the $file array the message
$file[\'error\'] = $too_large;
return $file;
}
else
return $file;
}
SO网友:Nefro
尚未对其进行测试,但这应该可以:
// check for file upload size //
{
if( !current_user_can( \'administrator\') )
add_filter( \'wp_handle_upload_prefilter\', \'mdu_validate_image_size\' );
}
add_filter(\'wp_handle_upload_prefilter\',\'mdu_validate_image_size\');
function mdu_validate_image_size( $file ) {
if ( mime_content_type($file) == \'application/zip\' ) {
mdu_validate_zip_image_size($file);
return $file;
}
$image = getimagesize($file[\'tmp_name\']);
$minimum = array(
\'width\' => \'800\',
\'height\' => \'450\'
);
$maximum = array(
\'width\' => \'800\',
\'height\' => \'450\'
);
$image_width = $image[0];
$image_height = $image[1];
$too_small = "Image dimensions are too small. Minimum size is {$minimum[\'width\']} by {$minimum[\'height\']} pixels. Uploaded image is $image_width by $image_height pixels.";
$too_large = "Image dimensions are too large. Maximum size is {$maximum[\'width\']} by {$maximum[\'height\']} pixels. Uploaded image is $image_width by $image_height pixels.";
if ( $image_width < $minimum[\'width\'] || $image_height < $minimum[\'height\'] ) {
// add in the field \'error\' of the $file array the message
$file[\'error\'] = $too_small;
return $file;
}
elseif ( $image_width > $maximum[\'width\'] || $image_height > $maximum[\'height\'] ) {
//add in the field \'error\' of the $file array the message
$file[\'error\'] = $too_large;
return $file;
}
else
return $file;
}
function mdu_validate_zip_image_size($file) {
$zip = new ZipArchive();
if (true !== $zip->open($file))
{
$file[\'error\'] = \'Could not open ZIP archive\';
}
// Search for the image file.
for($i = 0; $i < $zip->numFiles; $i++)
{
$entry = $zip->statIndex($i);
$ext = substr($entry[\'name\'], -3);
if (in_array($ext, array(\'jpg\', \'png\'))
{
$filename = $entry[\'name\'];
}
}
if (isset($filename) && ($image = $zip->getFromName($filename)))
{
list($image_width, $image_height) = getimagesize($image);
}
else
{
$file[\'error\'] = \'No image found\';
}
$minimum = array(
\'width\' => \'800\',
\'height\' => \'450\'
);
$maximum = array(
\'width\' => \'800\',
\'height\' => \'450\'
);
$too_small = "Image dimensions are too small. Minimum size is {$minimum[\'width\']} by {$minimum[\'height\']} pixels. Uploaded image is $image_width by $image_height pixels.";
$too_large = "Image dimensions are too large. Maximum size is {$maximum[\'width\']} by {$maximum[\'height\']} pixels. Uploaded image is $image_width by $image_height pixels.";
if ( $image_width < $minimum[\'width\'] || $image_height < $minimum[\'height\'] ) {
// add in the field \'error\' of the $file array the message
$file[\'error\'] = $too_small;
return $file;
}
elseif ( $image_width > $maximum[\'width\'] || $image_height > $maximum[\'height\'] ) {
//add in the field \'error\' of the $file array the message
$file[\'error\'] = $too_large;
return $file;
}
else
return $file;
}
}