Laravel图像介入压缩 [英] Laravel image intervention compression
问题描述
我有一个脚本,可以通过干预来保存和缓存图像,并且可以100%工作
I have a script which saves and caches images with intervention, and it's working 100%
但是我试图弄清楚如何将75%的压缩率添加到jpg& png文件,但我不知道我会在这种情况下使用它。
However i am trying to work out how i can add 75% compression to jpg & png files, but i don't know i would apply it in this situation.
我不认为PNG文件可以与执行该操作的软件分开压缩,因此我不是很确定是否相同吗?
I didn't think PNG files could be compressed apart from software which does it, so im not really sure if its the same thing?
这里有一个压缩示例: http://image.intervention.io/api/save
There is an example of compression here: http://image.intervention.io/api/save
/* ////////////////////// IMAGES //////////////////////// */
Route::get( '/media/{size}/{crop}/{name}', function ( $size = null, $crop = null, $name = null ) {
if ( ! is_null( $size ) and ! is_null( $name ) and ! is_null( $crop ) ) {
$size = explode( 'x', $size );
$hours = 48;
$cache_length = $hours * 60;
switch ( $crop ) {
/*///////////////////////// no crop and change ratio */
case "0":
$cache_image = Image::cache( function ( $image ) use ( $size, $name ) {
return $image->make( url( '/uploads/' . $name ) )->resize( $size[0], $size[1] )->sharpen(5);
}, $cache_length);
break;
/*///////////////////////// crop - NO upsize */
default:
case "1":
$cache_image = Image::cache( function ( $image ) use ( $size, $name ) {
return $image->make( url( '/uploads/' . $name ) )->fit( $size[0], $size[1], function ( $constraint ) {
$constraint->upsize();
} )->sharpen(5);
}, $cache_length );
break;
/*///////////////////////// crop - WITH upsize */
case "2":
$cache_image = Image::cache( function ( $image ) use ( $size, $name ) {
return $image->make( url( '/uploads/' . $name ) )->fit( $size[0], $size[1], function ( $constraint ) {
//$constraint->upsize();
} )->sharpen(5);
}, $cache_length );
break;
/*///////////////////////// No crop & add borders */
case "3":
$cache_image = Image::cache( function ( $image ) use ( $size, $name ) {
$image->make( url( '/uploads/' . $name ) )->resize( $size[0], $size[1], function ( $constraint ) {
$constraint->aspectRatio();
$constraint->upsize();
} )->sharpen(5);
$image->resizeCanvas($size[0], $size[1], 'center', false, array(255, 255, 255, 0.0));
return $image;
}, $cache_length );
break;
/*///////////////////////// No crop */
case "4":
$cache_image = Image::cache( function ( $image ) use ( $size, $name ) {
$image->make( url( '/uploads/' . $name ) )->resize( $size[0], $size[1], function ( $constraint ) {
$constraint->aspectRatio();
$constraint->upsize();
} )->sharpen(5);
//$image->resizeCanvas($size[0], $size[1], 'center', false, array(255, 255, 255, 0.0));
return $image;
}, $cache_length );
break;
}
return Response::make( $cache_image, 200, [ 'Content-Type' => 'image' ] )->setMaxAge(604800)->setPublic();
} else {
abort( 404 );
}
} );
推荐答案
尝试 encode()
方法,您可以在其中指定格式
和 quality
(对于 jpg
)。因此,每次使用缓存时,请尝试执行以下操作:
Try the encode()
method, where you can specify the format
and the quality
(for jpg
). So, everytime you use the cache, try to do this:
$cache_image = Image::cache(function ($image) use ($size, $name) {
$image
->make(...)
->... // any other call to image manipulation methods
->encode('jpg', 75);
// ...
return $image;
});
这篇关于Laravel图像介入压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!