libvips / php-vips

php binding for libvips
MIT License
618 stars 25 forks source link

response cropped image without saving image #154

Closed Mohammad-Khazaee closed 2 years ago

Mohammad-Khazaee commented 2 years ago

Hi i created a laravel app for image api like this https://imgix.com
but here is a problem i wanna response the file without saving it in my for example: my current laravel app

$image = Image::newFromFile(storage_path('image.png') ', ['access' => 'sequential']);
$image = $image->crop(0,0,100,100);
$image = $image->writeToFile(storage_path('new-image.png') ,['Q' => 100]);
return response()->file(storage_path('new-image.png'));

instead i wanna do :

$image = Image::newFromFile(storage_path('image.png') ', ['access' => 'sequential']);
$image = $image->crop(0,0,100,100);
return response()->file($image->path());

this is a serious problem for me and my app because of performance stuff is there any way to do that or similar things?

Mohammad-Khazaee commented 2 years ago

seems there is a solution in sharp node js https://stackoverflow.com/questions/65740056/how-to-send-the-resized-image-from-sharp-back-to-user

jcupitt commented 2 years ago

Hi @Mohammad-Khazaee,

You can use writeToBuffer to write to a string, for example (untested):

$data = $image->writeToBuffer(".jpg");
echo "Content-length: {strlen($data)}";
echo "Content-type: image/jpeg";
echo "";
echo $data;
jcupitt commented 2 years ago

See https://libvips.github.io/php-vips/classes/Jcupitt-Vips-Image.html#method_writeToBuffer

Mohammad-Khazaee commented 2 years ago

thanks for quick response