Laravel 5 - blurry image after loading - intrusion
I am making an e-commerce web portal. I am trying to upload an image in different resolutions which are working completely fine.
But the problem is, if I view it in a browser after loading, I get to see a blurry image, which is not what I want. I want the image to be clearly visible after loading and not blurry.
Here's the controller method used:
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');
}
How do I avoid showing a blurry image after loading?
Please help me. Thanks in advance.
source to share
The problem with the current code is that the same image file is being processed. This changes the size of the original file to a "thumbnail". This thumbnail will then be resized, resulting in "blocky" output.
The answer is to create a new "Image" of the source file for each "resize" operation.
Also, the code can be simplified, since all information for generating output filenames is in an array $imageType
.
I don't have a "frame" so I changed the function to take the filename and output directory. In fact, the input filename is put into a structure SplFileInfo
, so I can easily access the different parts of the filename, but that's just a detail.
Here's the function, the only real change in handling is recreating the "Image" from the original every time.
<?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;
}
I'll show it works if needed.
source to share