1

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

 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)

4

1 回答 1

5

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

  1. 用户上传 JPEG。
  2. 用户可以点击“左90”、“右90”,或者输入N度旋转。
  3. png是使用打开的

    $image = imagecreatefromjpeg("./source.jpg");
    
  4. 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);
    
  5. 如果用户希望旋转更多,则返回步骤 3,否则将 last.jpg 作为最终文件并$_SESSION["degrees"]销毁该参数。

于 2012-10-15T13:56:45.053 回答