PHP,GD库,图像旋转


PHP, GD library, Image rotation

我是GD的新手,我正在尝试构建一个简单的脚本来旋转图像,但我得到了以下错误:

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in C:'wamp'www'uploading.php on line 6

这是我的php:

$imgsrc = 'uploaded/Tulips.jpg';
$img = imagecreatefromjpeg($imgsrc);
$imgRotated = imagerotate($img,45,-1);
imagejpeg($imgRotated,"uploaded/myPicRotated.jpg",100);

这个有什么问题吗?

更新:我在imagerotate($img,45,0)中将-1替换为0,它运行良好。然而,我得到了一个黑色的背景色。如何将其更改为白色?

imagerotate返回false,因为发生了错误(可能$imgsrc不存在?)

尝试在轮换前测试$img:

$imgsrc = 'uploaded/Tulips.jpg';
if (file_exists($imgsrc)) {
  $img = imagecreatefromjpeg($imgsrc);
  if ($img !== false) {
    imagealphablending($img, true); 
    imagesavealpha($img, true); 
    $imgRotated = imagerotate($img,45,-1);
    if ($imgRotated !== false) {
      imagejpeg($imgRotated,"uploaded/myPicRotated.jpg",100);
    }
  }
}

如果-1不起作用,输入0,您将得到一个黑色背景。为了将背景更改为任何其他颜色,您所要做的就是使用imagecolorallocate()和imagefill()。在我的情况下,我希望背景是白色的,这是对我有效的最后一个代码:

$imgsrc = 'uploaded/Tulips.jpg';
    if (file_exists($imgsrc)) {
    $img = imagecreatefromjpeg($imgsrc);
    if ($img !== false) {
    $imgRotated = imagerotate($img,45,0);
    $backgroundcolor = imagecolorallocate($imgRotated, 255, 255, 255);
    imagefill($imgRotated, 0, 0, $backgroundcolor);
    if ($imgRotated !== false) {
      imagejpeg($imgRotated,"uploaded/myPicRotated.jpg",100);
        }
    }
}

我要感谢@Paul Giragossian的帮助。