【问题标题】:Dart/Flutter Alternative to PHP's resize_crop_image?Dart/Flutter 替代 PHP 调整裁剪图像大小?
【发布时间】:2021-07-04 04:30:06
【问题描述】:

目前,我正在使用 PHP 的 resize_crop_image 在我的服务器上调整头像的大小。下面的代码拍摄一张图片,裁剪并调整其大小,使其正好是 500px X 500px 正方形。

resize_crop_image(500, 500, $filename, $filename);

但我想将此过程从服务器转移到颤振应用程序。我在 pub.dev 上看到的大多数插件都过度使用用户使用裁剪工具裁剪图像。但我希望这个函数能够像我目前在 PHP 中那样自动发生。

这里是 resize_crop_image 代码;

function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 100){
        $imgsize = getimagesize($source_file);
        $width = $imgsize[0];
        $height = $imgsize[1];
        $mime = $imgsize['mime'];
     
        switch($mime){
            case 'image/gif':
                $image_create = "imagecreatefromgif";
                $image = "imagegif";
                break;
     
            case 'image/png':
                $image_create = "imagecreatefrompng";
                $image = "imagepng";
                $quality = 10;
                break;
     
            case 'image/jpeg':
                $image_create = "imagecreatefromjpeg";
                $image = "imagejpeg";
                $quality = 100;
                break;
     
            default:
                return false;
                break;
        }
         
        $dst_img = imagecreatetruecolor($max_width, $max_height);
        $src_img = $image_create($source_file);
         
        $width_new = $height * $max_width / $max_height;
        $height_new = $width * $max_height / $max_width;
        //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
        if($width_new > $width){
            //cut point by height
            $h_point = (($height - $height_new) / 2);
            //copy image
            imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
        }else{
            //cut point by width
            $w_point = (($width - $width_new) / 2);
            imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
        }
         
        $image($dst_img, $dst_dir, $quality);
     
        if($dst_img)imagedestroy($dst_img);
        if($src_img)imagedestroy($src_img);
    }      
?>

【问题讨论】:

    标签: php image flutter dart image-processing


    【解决方案1】:

    您可以使用这个库:flutter_native_image 来调整/裁剪您的图像。

    重要的是看看这个函数的其他参数,你可能可以自定义图像的裁剪方式。

    类似的东西:

    ImageProperties properties = await FlutterNativeImage.getImageProperties(file.path);
    File croppedFile = await FlutterNativeImage.cropImage(file.path, originX, originY, 500, 500);
    

    originX/originY:剪切的 x/y 位置。

    【讨论】:

    • 如果我希望从中心精确裁剪,我应该为 originX、originY 使用什么?
    • 我认为裁剪功能用于从具有该宽度和高度的 originX/originY 剪切图像。 FlutterNativeImage.compressImage 不起作用?
    • 我不知道为什么要压缩图像来进行裁剪?我猜我可以像这样找到中心; int originX=properties.width ~/2; int originY = properties.height ~/ 2;
    • 但是我对传递给裁剪函数的参数有点困惑。我正在从互联网上截取一张图片进行裁剪,所以我有一个 URL/文件路径,但不确定文件图片要使用什么;这是我到目前为止所得到的; cropAvatar(文件图像,文件路径)异步{ ImageProperties 属性 = 等待 FlutterNativeImage.getImageProperties(文件路径); int originX=properties.width ~/2; int originY = properties.height ~/ 2;文件croppedFile = await FlutterNativeImage.cropImage(filepath, originX, originY, 500, 500);返回裁剪文件; }
    • 我认为如果你使用裁剪,它会从原始图像中剪掉一部分,失去一部分,这就是我认为你需要调整图像大小的原因。但是如果你只是想切一部分,原来的X/Y就是你想切的起始点,可能是0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    相关资源
    最近更新 更多