Intervention / image

PHP Image Processing
https://image.intervention.io
MIT License
13.84k stars 1.5k forks source link

compress image file which is private in AWS s3 #1311

Closed sajadshouli closed 6 months ago

sajadshouli commented 6 months ago

before I change my filesystem from local to s3 I have a command which compresses the image size like this

           $image = Image::make($media->file);

            $fullPath = $media->file; // in local filesystem was like this. https://domain.com/storage/foobar.png

            if ($media->disk != 's3') {
                $filePath = str_replace('/storage', 'public', parse_url($media->file, PHP_URL_PATH));
                $fullPath = storage_path('app/' . $filePath);
            }

           $image->save($fullPath, 70);

in local or public filesystems the code works fine but when I changed the filesystem to s3, It could not compress the file since the file path will be something like this

https://{domain}/materials/1709755329jhgqfomuio-zorat.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=99120f29-11e6-4c3a-aa0a-e62e5241025e%2F20240306%2Fsimin%2Fs3%2Faws4_request&X-Amz-Date=20240306T203008Z&X-Amz-SignedHeaders=host&X-Amz-Expires=1860&X-Amz-Signature=d3e5f4b11d06d6503ae8e5f17cfdcf22e55a515c06462c4f971b5a443c653dc0

and the error is like this

  Encoding format (png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=99120f29-11e6-4c3a-aa0a-e62e5241025e%2F20240306%2Fsimin%2Fs3%2Faws4_request&X-Amz-Date=20240306T204408Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3360&X-Amz-Signature=b10e0ce0908d6c8944217caa971355b84d807c44ce0a9f75b881d903f197984b) is not supported.

in this situation does the package can compress the file with other method or something else

olivervogel commented 6 months ago

I would not use the save() method here to transfer files to remote data storage, as it is primarily designed for the local file system.

I would use one of the encoder methods to get the raw image data. Then you can do what you want with it: See https://github.com/Intervention/image/issues/431#issuecomment-144108588. Data compression and transfer to S3 is not part of this library.