有人能帮我编辑限制图像尺寸的代码吗

时间:2015-07-16 作者:pryde1919

有人能帮我编辑代码吗。代码似乎正常工作,但有一个问题,当我在wordpress中添加新插件时,它会提示错误。此代码警报/错误应仅存在于媒体库中,但它似乎也会影响插件页(管理员)。谢谢你

<?php 
/* Marc Dingena Utilities
 * Test image resolution before image crunch
 */
add_filter(\'wp_handle_upload_prefilter\',\'mdu_validate_image_size\');
function mdu_validate_image_size( $file ) {
    $image = getimagesize($file[\'tmp_name\']);
    $minimum = array(
        \'width\' => \'400\',
        \'height\' => \'400\'
    );
    $maximum = array(
        \'width\' => \'2000\',
        \'height\' => \'2000\'
    );
    $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_image_size( $file ) {
    $image = getimagesize($file[\'tmp_name\']);
    if(@!is_array(getimagesize($image))){
     $image = true;
    }
    else {
    $image = false;
    }

    $minimum = array(
        \'width\' => \'250\',
        \'height\' => \'180\'
    );
    $maximum = array(
        \'width\' => \'250\',
        \'height\' => \'180\'
    );
    $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;
}

1 个回复
SO网友:Trang

添加新插件可能会被视为上传文件。处理前检查上传的文件是否为图像

if(@!is_array(getimagesize($mediapath))){
    $image = true;
} 
else {
    $image = false;
}
感谢:https://stackoverflow.com/questions/15408125/php-check-if-file-is-an-image因此,请将代码修改为

function mdu_validate_image_size( $file ) {
   $image = getimagesize($file[\'tmp_name\']);
   if(@!is_array(getimagesize($image))){//check if file is an image
     //do stuff
   }
}

结束

相关推荐

在页面模板中,而不是在unctions.php中,早期将javascrip文件入队

是否可以在页面模板文件的早期将javascript文件排队?我想使用这种包含脚本的方式来保持结构的整洁(因此每个页面模板都有自己的样式和脚本)。我不想在函数中使用条件。php。我不想改变功能。php。我只想修改模板文件。我可以将脚本排队,没有问题,但它总是添加在页面的末尾,我需要该脚本在前面可用(在显示某些HTML之前,最好是在显示任何HTML之前)。