PHP Rotate Image from request file

北城余情 提交于 2019-12-12 01:58:09

问题


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

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

and I found method to rotate the image with 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 implement the code with $request->file('file')

Thanks!


回答1:


I am not sure where you are getting your functions from, imagecreatefromjpeg() and imagerotate(), but if you just use PHP's own, 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);

The background color ("FFFFFF") is applied here if the image rotates and leaves a certain amount of background (does not happen for rotate degrees in increments of 90).



来源:https://stackoverflow.com/questions/38734035/php-rotate-image-from-request-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!