Laravel上传时旋转图像 [英] Laravel is rotating the image when uploaded

查看:53
本文介绍了Laravel上传时旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,其中涉及将图像上传到服务器的功能.我正在使用Laravel.问题是当我上传照片时,Laravel正在旋转图像.

这是我上传图片文件的代码.

$request->file('image_file')->store('images');

只需一行代码即可上传图片.

我上传了这张图片.

然后,照片在服务器上旋转并变成这样.我在HTML标签中显示图像.

那么,怎么了.如何阻止照片旋转?

解决方案

使用移动相机捕获图像时会发生这种情况.

您可以使用 exif_read_data()查看图像数据

但是,如果您想以原始方式存储它,则可以使用intervention/image 程序包.. >

并使用 orientate()进行更改.这是一个例子

$img = \Image::make($request->file('image_file')->getRealpath());
$img->orientate();

但是如果您不想使用Package,可以尝试

$exif = exif_read_data($request->file('image_file'));
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($image,90,0);
            break;
        case 3:
            $image = imagerotate($image,180,0);
            break;
        case 6:
            $image = imagerotate($image,-90,0);
            break;
    }
}

希望这会有所帮助.

I am developing a web application that involves the feature of uploading image to server. I am using Laravel. The problem is when I upload a photo, Laravel is rotating the image.

This is my code to upload image file.

$request->file('image_file')->store('images');

Just one line of code for uploading the image.

I uploaded this image.

Then, the photo is rotated on the server and becomes like this. I display the image in the HTML tag.

So, what is wrong. How can I stop the photo from being rotated?

解决方案

This happen when you capture the image with Mobile camera.

you can see the image data using exif_read_data()

But if you want to store it in an orignal way you can use intervention/image package.

and use orientate() to change it. Here is an example

$img = \Image::make($request->file('image_file')->getRealpath());
$img->orientate();

But if you dont want to use the Package you can try

$exif = exif_read_data($request->file('image_file'));
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($image,90,0);
            break;
        case 3:
            $image = imagerotate($image,180,0);
            break;
        case 6:
            $image = imagerotate($image,-90,0);
            break;
    }
}

Hope this helps.

这篇关于Laravel上传时旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆