Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP实现图像的无损旋转_Php_Image Processing_Gd - Fatal编程技术网

用PHP实现图像的无损旋转

用PHP实现图像的无损旋转,php,image-processing,gd,Php,Image Processing,Gd,我一直在开发一个系统来旋转上传的图像。该算法的工作原理如下: 1) User uploads a jpeg. It gets saved as a PNG 2) Link to the temp png is returned to the user. 3) The user can click 90left,90right, or type in N degrees to rotate 4) The png is opened using $image = imagecre

我一直在开发一个系统来旋转上传的图像。该算法的工作原理如下:

 1) User uploads a jpeg.  It gets saved as a PNG
 2) Link to the temp png is returned to the user.
 3) The user can click 90left,90right, or type in N degrees to rotate
 4) The png is opened using 

   $image = imagecreatefrompng("./fileHERE");

 5) The png is rotated using

   $imageRotated = imagerotate($image,$degrees,0);

 6) The png is saved and the link returned to the user.
 7) If the user wishes to rotate more go back to step 3 operating on the newly
    saved temporary PNG, 
    else the changes are commited and the final image is saved as a jpeg.
当向左和向右旋转90度时,这项功能非常好。用户可以无限多次旋转,而不会损失任何质量。问题是,当用户尝试旋转20度或其他非90度的倍数时。当旋转20度时,图像会稍微旋转,并形成一个黑框来填充需要填充的区域。由于带有黑匣子的图像保存为png,因此下一次旋转20度会将带有黑匣子的图像再旋转20度,并形成另一个黑匣子以填补空白。长话短说如果你这样做360度,你会有一个大的黑框周围一个非常小的剩余图像。即使放大和缩小黑框,质量也会明显下降


我有没有办法避开黑匣子?服务器未安装imagick

始终存储未修改的源文件,并且在旋转时,使用原始源文件旋转度数。所以20度+20度,意味着将光源旋转40度

用户上传一个JPEG。 用户可以单击90 left、90 right或键入N degrees进行旋转。 png是使用

$image = imagecreatefromjpeg("./source.jpg");
png正在旋转

// If this is the first time, there is no rotation data, set it up
if(!isset($_SESSION["degrees"])) $_SESSION["degrees"] = 0;

// Apply the new rotation
$_SESSION["degrees"] += $degrees;

// Rotate the image
$rotated = imagerotate($image, $_SESSION["degrees"], 0);

// Save the image, DO NOT MODIFY THE SOURCE FILE!
imagejpeg($rotated, "./last.jpg");

// Output the image
header("Content-Type: image/jpeg");
imagejpeg($rotated);
如果用户希望旋转更多,请返回步骤3,否则最后一个.jpg将被视为最终值,$\u SESSION[degrees]参数将被销毁


始终存储未修改的源文件,并且在旋转时,使用原始源文件旋转度数。所以20度+20度,意味着将光源旋转40度

用户上传一个JPEG。 用户可以单击90 left、90 right或键入N degrees进行旋转。 png是使用

$image = imagecreatefromjpeg("./source.jpg");
png正在旋转

// If this is the first time, there is no rotation data, set it up
if(!isset($_SESSION["degrees"])) $_SESSION["degrees"] = 0;

// Apply the new rotation
$_SESSION["degrees"] += $degrees;

// Rotate the image
$rotated = imagerotate($image, $_SESSION["degrees"], 0);

// Save the image, DO NOT MODIFY THE SOURCE FILE!
imagejpeg($rotated, "./last.jpg");

// Output the image
header("Content-Type: image/jpeg");
imagejpeg($rotated);
如果用户希望旋转更多,请返回步骤3,否则最后一个.jpg将被视为最终值,$\u SESSION[degrees]参数将被销毁


不,没有。图像必须有方角,因为标量成像就是这样工作的,你必须用一些东西填充它。如果旋转20度,则决定实际要旋转40度,必须再次从基础图像开始,否则图像将不可避免地在后续操作中衰减。实际上你对此无能为力。@hakre-我是在PNG上操作的,不是JPEGreason@DaveRnadom-谢谢。给你一个答案,我就给你投票和积分。为什么旋转90度不会导致黑匣子或损失?@user974896:我不理解你的推理,因为PNG没有无损旋转。有了JPEG,在某些情况下至少会有——至少技术上到目前为止在GD中没有。不,没有。图像必须有方角,因为标量成像就是这样工作的,你必须用一些东西填充它。如果旋转20度,则决定实际要旋转40度,必须再次从基础图像开始,否则图像将不可避免地在后续操作中衰减。实际上你对此无能为力。@hakre-我是在PNG上操作的,不是JPEGreason@DaveRnadom-谢谢。给你一个答案,我就给你投票和积分。为什么旋转90度不会导致黑匣子或损失?@user974896:我不理解你的推理,因为PNG没有无损旋转。有了JPEG,至少在某些情况下会出现这种情况——至少从技术上讲,目前在GD中还没有。