[英]How to compress image before uploading in Laravel?
I'm making a images gallery website where users can upload any image and they will be displayed on frontend.我正在制作一个图片库网站,用户可以在其中上传任何图片,它们将显示在前端。 I need to compress images without effecting it's quality to reduce there size so that page load speed should not effect that much.
我需要在不影响图像质量的情况下压缩图像以减小尺寸,这样页面加载速度就不会受到太大影响。 I'm using following code to upload image:
我正在使用以下代码上传图片:
$rules = array('file' => 'required');
$destinationPath = 'assets/images/pages'
$validator = Validator::make(array('file' => $file), $rules);
if ($validator->passes()) {
$filename = time() . $uploadcount . '.' . $file->getClientOriginalExtension();
$file->move($destinationPath, $filename);
return $filename;
} else {
return '';
}
The best and easiest way to compress images before uploading to the server, I found here:-在上传到服务器之前压缩图像的最佳和最简单的方法,我在这里找到:-
https://github.com/spatie/laravel-image-optimizer https://github.com/spatie/laravel-image-optimizer
You need to optimize the image for web usage as user may upload images that are way to large (Either in size or resolution).您需要针对 Web 使用优化图像,因为用户可能上传过大的图像(无论是大小还是分辨率)。 You may also want to remove the meta data from the images to decrease the size even more.
您可能还想从图像中删除元数据以进一步减小尺寸。 Intervention Image perfect for resizing/optimizing images for web usage in Laravel.
Intervention Image 非常适合在 Laravel 中为 Web 使用调整大小/优化图像。 You need to optimize the image before it is saved so that the optimized version is used when loading the web page.
您需要在保存图像之前对其进行优化,以便在加载网页时使用优化版本。
https://tinypng.com provides an API service for compressing images. https://tinypng.com提供了用于压缩图像的 API 服务。 All you need to do is install their PHP library in Laravel, get a developer key from their website.
您需要做的就是在 Laravel 中安装他们的 PHP 库,从他们的网站获取开发人员密钥。 After that by the adding the below code, you can compress your uploaded image.
之后通过添加以下代码,您可以压缩上传的图像。 In the code, I am assuming you have stored your file under 'storage' directory.
在代码中,我假设您已将文件存储在“存储”目录下。
$filepath = public_path('storage/profile_images/'.$filename);
\Tinify\setKey("YOUR_API_KEY");
$source = \Tinify\fromFile($filepath);
$source->toFile($filepath);
Here is the link to a blog which explains how to upload and compress images in Laravel http://artisansweb.net/guide-upload-compress-images-laravel这是一个博客的链接,它解释了如何在 Laravel 中上传和压缩图像http://artisansweb.net/guide-upload-compress-images-laravel
**Using core php ** **使用核心php **
function compress($source_image, $compress_image) { $image_info = getimagesize($source_image); if ($image_info['mime'] == 'image/jpeg') { $source_image = imagecreatefromjpeg($source_image); imagejpeg($source_image, $compress_image, 20); //for jpeg or gif, it should be 0-100 } elseif ($image_info['mime'] == 'image/png') { $source_image = imagecreatefrompng($source_image); imagepng($source_image, $compress_image, 3); } return $compress_image; } public function store(Request $request) { $image_name = $_FILES['image']['name']; $tmp_name = $_FILES['image']['tmp_name']; $directory_name = public_path('/upload/image/'); $file_name = $directory_name . $image_name; move_uploaded_file($tmp_name, $file_name); $compress_file = "compress_" . $image_name; $compressed_img = $directory_name . $compress_file; $compress_image = $this->compress($file_name, $compressed_img); unlink($file_name); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.