gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:31:18 27 4
gpt4 key购买 nike

我正在制作一个电子商务门户网站。我正在尝试以不同的分辨率上传图像,但效果很好。

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

这是它的 Controller 方法:

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');
}

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

请帮助我。提前致谢。

最佳答案

当前代码的问题是正在处理同一个图像文件。这导致原始文件被调整为“缩略图”,然后该缩略图被调整大小导致“ block 状”输出。

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

代码也可以简化,因为生成输出文件名的所有信息都在 $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;
}

如果需要,我会展示它的工作情况。

关于php - Laravel 5 - 上传后图像模糊 - 干预,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29771678/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com