php-imagine / Imagine

PHP Object Oriented image manipulation library
https://imagine.readthedocs.io
Other
4.42k stars 530 forks source link

Error when file is coming from temp directory: Creating an image in "C:\Users\...\Temp\phpA6B2.tmp" format is not supported, please use one of the following formats: #849

Closed deanilvincent closed 1 year ago

deanilvincent commented 1 year ago

Issue description

Good day! I get this error when I'm trying to resize the image from temp file. We don't store my image to the local directory because we're storing them to CDN. When I'm trying to resize the image that's coming from temp file before uploading to CDN, then the problem appears: Creating an image in "C:\Users\...\Temp\phpA6B2.tmp" format is not supported, please use one of the following formats: "gif", "jpeg", "png", "wbmp", "xbm", "bmp", "webp", "avif"

It seems that the resize method doesn't recognize the file coming from temp directory. Any ideas? Thank you!

image

What version of Imagine are you using?

^1.2

What's the PHP version you are using?

-PHP 8.2.4

What's the imaging library you are using [gd/imagick/gmagick/any]?

use Imagine\Gd\Imagine;

Minimal PHP code to reproduce the error:


$imagine = new Imagine;
$image   = $imagine->open($file);
$size    = new Box(200, 100);

$resizedImage = $image->resize($size)->get($file);
mlocati commented 1 year ago

Could you share the raw image file that can't be loaded?

deanilvincent commented 1 year ago

Hi, here's the file image sample. priscilla-du-preez-CoqJGsFVJtM-unsplash (2)

deanilvincent commented 1 year ago

Here's the dd for the $file

image
ausi commented 1 year ago

This line is wrong I think as get() wants a format as the parameter, not a path:

$resizedImage = $image->resize($size)->get($file);

Did you instead meant to write something like this?

$resizedImage = $image->resize($size)->save($file, ['format' => 'jpeg']);
deanilvincent commented 1 year ago

Hi @ausi , I tried this $resizedImage = $image->resize($size)->save($file, ['format' => 'jpeg']); but it throws me different error that says,

"imagejpeg(): Argument #2 ($file) must be a file name or a stream resource, Symfony\Component\HttpFoundation\File\UploadedFile given"

I also tried removing the format `['format' => 'jpeg'] and the error is

 `Saving image in "tmp" format is not supported, please use one of the following extensions: "gif", "jpeg", "png", "wbmp", "xbm", "bmp", "webp", "avif"`

Does the package support the temp directory?

ausi commented 1 year ago

You need to pass the file path as a string:

$resizedImage = $image->resize($size)->save($file->getPathname(), ['format' => 'jpeg']);
deanilvincent commented 1 year ago

That solves the problem. Thank you!