“无法将图像数据写入路径” - Laravel 图像干预

Posted

技术标签:

【中文标题】“无法将图像数据写入路径” - Laravel 图像干预【英文标题】:"Can't write image data to path" - Laravel Image Intervention 【发布时间】:2020-02-06 23:18:36 【问题描述】:

无法将图像数据写入路径(Laravel)

我无法将任何内容保存到 Laravel 项目中的存储目录并收到以下错误:

过去三周我一直在试图找出为什么我无法将图像保存到 LaravelStorage 文件夹中,但没有运气。我搜索了 *** 并得出结论,这可能是由于我的 Docker 映像。如果可能的话,我很想使用我的自定义图像,但让它们正常工作。这是我的设置:

Dockerfile(基础镜像)

ARG VERSION=7.4
FROM php:$VERSION-fpm-alpine

# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS"

# Install zip for csv stuff
# hadolint ignore=DL3018
RUN apk add --no-cache \
    libzip-dev \
    zip \
&& docker-php-ext-install zip \
&& apk del libzip-dev

# Install gd for image stuff
# hadolint ignore=DL3018
RUN apk add --no-cache libpng libpng-dev libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev \
    && docker-php-ext-install gd \
    && apk del libpng-dev libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev

# Install nginx & PHP packages and extensions
# hadolint ignore=DL3018
RUN apk add --no-cache \
    # for PHP/Laravel
    git \
    icu-dev \
    msmtp \
    nginx \
    unzip \
    # zip \
    && mkdir -p /run/nginx \
    && docker-php-ext-install \
        pdo_mysql \
        opcache \
    &&  \
        echo 'opcache.memory_consumption=128'; \
        echo 'opcache.interned_strings_buffer=8'; \
        echo 'opcache.max_accelerated_files=4000'; \
        echo 'opcache.revalidate_freq=2'; \
        echo 'opcache.fast_shutdown=1'; \
        echo 'opcache.enable_cli=1'; \
     > /usr/local/etc/php/conf.d/php-opocache-cfg.ini \
    && apk del icu-dev

COPY /config/nginx.conf /etc/nginx/conf.d/default.conf
COPY /config/msmtprc /etc/msmtprc
COPY /scripts/start.sh /etc/start.sh
COPY --chown=www-data:www-data src/ /var/www/html

WORKDIR /var/www/html

EXPOSE 80 443

ENTRYPOINT ["/etc/start.sh"]

Dockerfile(项目,参考上面的Dockerfile^)

FROM justintime50/nginx-php:dev # the dockerfile above

COPY --chown=www-data:www-data ./src /var/www/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
RUN php composer.phar install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist \
    && chmod -R 775 storage \
    && php artisan storage:link \
    && chmod -R 775 bootstrap/cache

Docker 编写

version: "3.7"
services:

    laraview:
        build: .
        restart: always
        container_name: laraview
        volumes: 
            - ./src/storage:/var/www/html/storage
        networks:
            - traefik
            - laraview
        labels:
            - traefik.enable=true
            - traefik.docker.network=traefik
            - traefik.frontend.rule=Host:laraview.localhost
            - traefik.port=80
        env_file:
            - init-db.env
        depends_on:
            - laraview-db

    laraview-db:
        image: mysql:8.0.18
        restart: always
        container_name: laraview-db
        env_file:
            - init-db.env
        volumes:
            - ./db:/var/lib/mysql
        networks:
            - laraview
        ports:
            - "3306:3306"
        labels:
            - traefik.enable=false

networks:
    traefik:
        external:
            name: traefik
    laraview:
        name: laraview

上传图片文件(Laravel)

    public function updateProfilePic(Request $request)
    
        $request->validate([
            'upload_profile_pic' => 'required|image|mimes:jpeg,jpg,png|max:2048',
        ]);
        $id = request()->get('id');

        $file = $request->file('upload_profile_pic');
        $img = Image::make($file)
            ->resize(320, 240)
            ->save(storage_path(), $file->getClientOriginalName());

        // Upload Profil Pic (IMAGE INTERVENTION - LARAVEL)
        //Image::make($request->file('upload_profile_pic'))->fit(150, 150)->save(storage_path('avatars/'.$id.'.png'));

        session()->flash("message", "Profile picture updated successfully.");
        return redirect()->back();
    

参考资料:

Nginx/PHP 镜像:https://github.com/Justintime50/nginx-php-docker/tree/develop Laravel 项目:https://github.com/Justintime50/laraview/tree/develop

【问题讨论】:

可能是权限设置? RUN chown -R admin:admin /appRUN chmod 755 /app 它是否在正确的目录上运行? 是的,除了基本映像之外,这是与另一个项目相同的设置。另一个项目有效,我不明白为什么这个带有这张图片的项目没有。 磁盘空间如何? 【参考方案1】:

@leo 我很抱歉我再也没有回来回答这个问题。我不记得确切的修复方法,但我相信我误用了storage_path。这是更正后的 PHP 代码:

    public function uploadPostImage(Request $request)
    
        $request->validate([
            'upload_image' => 'required|image|mimes:jpeg,jpg,png|max:2048',
        ]);
        $id = mt_rand(100000000000, 999999999999); # TODO: This is hacky, fix down the road

        if (!is_dir(storage_path("app/public/post-images"))) 
            mkdir(storage_path("app/public/post-images"), 0775, true);
        

        // Upload Avatar (IMAGE INTERVENTION - LARAVEL)
        Image::make($request->file("upload_image"))->save(storage_path("app/public/post-images/".$id.".png"));

        session()->flash("message", "Image uploaded successfully.");
        return redirect()->back();
    

我使用了相同的 Docker 映像和 docker-compose 文件,这意味着它必须已通过正确使用 storage_path 进行修复。

【讨论】:

以上是关于“无法将图像数据写入路径” - Laravel 图像干预的主要内容,如果未能解决你的问题,请参考以下文章

laravel 架构图?

laravel 5.1中的PDF图像缩略图预览?

Laravel5多图上传和Laravel5单图上传的功能实现

使用 Laravel 为社交网站创建缩略图

laravel 做图片的缩略图

在 laravel 中添加多个图层以搜索模型