繁体   English   中英

Laravel 5 - 上传后的模糊图像 - 干预

[英]Laravel 5 - blurry image after upload - intervention

我正在建立一个电子商务门户网站。 我试图以不同的分辨率上传图像,这是完全正常的。

但问题是,如果我在上传后在浏览器中查看它,我会看到模糊的图像,这不是我想要的。 我希望在上传后,图像应该清晰可见,不应该模糊。

这是它的控制器方法:

public function store( AddProductRequest $request ) {
    if(\Auth::guest()) {
        return redirect('/admin');
    }

    $imageType = [
        'thumbnail' => [
            'height' => 75,
            'width' => 75
        ],
        'product' => [
            'height' => 450,
            'width' => 450
        ],
        'cart' => [
            'height' => 200,
            'width' => 200
        ]
    ];

    if ( $request->file( 'image_file' ) ) {
        $fileName = $request->file( 'image_file' )->getClientOriginalName();

        $fileName = explode( '.', $fileName );
        $image = \Image::make( $request->file( 'image_file' ) );
        $path = public_path( 'images/uploads/' );

        foreach ($imageType as $key => $value) {
            if ($key == 'thumbnail') {
                $image->resize( $value['width'], $value['height'], function( $constraint ) {
                    $constraint->aspectRatio();
                });
                $image->save( $path.$fileName[0]."-thumbnail-". $value['width'] ."-".$value['height'] .".jpg", 100 );
            } elseif ( $key == 'product' ) {
                $image->resize( $value['width'], $value['height'], function( $constraint ) {
                    $constraint->aspectRatio();
                });
                $image->save( $path.$fileName[0]."-product-". $value['width'] ."-".$value['height'] .".jpg", 100 );
            } elseif ( $key == 'cart' ) {
                $image->resize( $value['width'], $value['height'], function( $constraint ) {
                    $constraint->aspectRatio();
                });
                $image->save( $path.$fileName[0]."-cart-". $value['width'] ."-".$value['height'] .".jpg", 100 );
            }
        }
    }       

    $product = Product::create( $request->all() );

    $product->tags()->attach( $request->input('tags') );

    \Session::flash('product_added', 'Product has been successfully added in the database.');
    return redirect('admin/products');
}

上传后如何避免显示模糊图像?

请帮助我。 提前致谢。

当前代码的问题是正在操纵相同的图像文件。 这会导致原始文件被调整为“缩略图”,然后调整此缩略图的大小,从而产生“块状”输出。

答案是为每个“调整大小”操作创建原始文件的新“图像”文件。

此外,代码可以简化,因为生成输出文件名的所有信息都在$imageType数组中。

我没有'框架'所以我改变了函数以获取文件名和输出目录。 实际上输入文件名放在SplFileInfo结构中,这样我就可以轻松访问文件名的各个部分,但这只是一个细节。

这是功能,处理的唯一真正改变是每次从原始图像重新创建“图像”。

<?php
function store(SplFileInfo $imageFile,
                $outDirectory) {

    $imageType = array(
        'thumbnail' => array(
            'height' => 75,
            'width' => 75
        ),
        'product' => array(
            'height' => 450,
            'width' => 450
        ),
        'cart' => array(
            'height' => 200,
            'width' => 200
        )
    );

    if (file_exists($imageFile->getRealPath())) {

        $fileName = explode('.', $imageFile->getFilename());
        $path = $imageFile->getPath();

        foreach ($imageType as $key => $value) {
            $image = Image::make($imageFile->getRealPath());
            $image->resize($value['width'], $value['height'],
                    function($constraint) {
                               $constraint->aspectRatio();
                    });
            $outFilename = $fileName[0] ."-{$key}-".
                           $value['width'] ."-". $value['height']
                            .".". $imageFile->getExtension();
            $image->save($outDirectory .'/'. $outFilename, 100);
        }
    }

    return true;
}

如果需要,我会显示它的工作原理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM