PHP Rotate image from request file

Now I am using the following method to store the image to the server from the input file type

$image = $request->file('file');
$filename = $item->itemId . '.png';
Storage::disk('s3')->put('/'.$filename, file_get_contents($image), 'public');

      

and I found a method to rotate an image using PHP

$filename = 'test.jpg';
$degrees = 180;
header('Content-type: image/jpeg');
$source = imagecreatefromjpeg($filename);
$rotate = imagerotate($source, $degrees, 0);
imagejpeg($rotate);

      

but i dont know how to implement the code with $ request-> file ('file')

Thank!

0


source to share


1 answer


I'm not sure where you are getting your functions, imagecreatefromjpeg () and imagerotate (), but if you just use the native PHP functions available functions (provided by Imagick), you can do something much simpler ...

$image = new Imagick();
$image_filehandle = fopen('some/file.jpg', 'a+');
$image->readImageFile($image_filehandle );

$image->rotateImage("FFFFFF", 90);  # Rotate 90 degrees, keep background of "FFFFFF" (white)

$image_icon_filehandle = fopen('some/file-rotated.jpg', 'a+');
$image->writeImageFile($image_icon_filehandle);

      



A background color ("FFFFFF") is applied here if the image is rotated and leaves a certain amount of background (does not occur for rotation of degrees in 90 steps).

0


source







All Articles