我尝试调整大小并使用PHP(GD)旋转图像,但当图像旋转时,它会在右侧添加黑色边框。GD PHP旋转图像黑色边框
为例图像(以前调整&旋转):
为例图像(后调整&旋转):
这里是我的代码:
$image = $_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['file']['name']);
$i = strrpos($filename,".");
$l = strlen($filename) - $i;
$ext = substr($filename,$i+1,$l);
$extension = strtolower($ext);
if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif")) {
echo ' Unknown Image extension ';
$errors=1;
}
else {
$size=filesize($_FILES['file']['tmp_name']);
if($extension=="jpg" || $extension=="jpeg") {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png") {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else {
$src = imagecreatefromgif($uploadedfile);
}
$max_width = 175;
$max_height = 100;
$size=GetImageSize($uploadedfile);
$width_ratio = ($size[0]/$max_width);
$height_ratio = ($size[1]/$max_height);
if($width_ratio >=$height_ratio)
{
$ratio = $width_ratio;
}
else
{
$ratio = $height_ratio;
}
$new_width = ($size[0]/$ratio);
$new_height = ($size[1]/$ratio);
$tmp=imagecreatetruecolor($new_width,$new_height);
if(function_exists("exif_read_data")){
$exif = @exif_read_data($uploadedfile);
$rotateImg = imagerotate($src,0,0);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$rotateImg = imagerotate($src,90,0);
break;
case 3:
$rotateImg = imagerotate($src,180,0);
break;
case 6:
$rotateImg = imagerotate($src,-90,0);
break;
}
}
}
imagecopyresampled($tmp,$rotateImg,0,0,0,0,($new_width-1),($new_height-1),$size[0],$size[1]);
$filename = sha1(basename($_FILES['file']['name']));
$path = dirname(__FILE__)."/../files/pictures/".$filename;
imagejpeg($tmp,$path,100);
imagedestroy($src);
imagedestroy($rotateImg);
imagedestroy($tmp);
}
}
但没有什么工作。
请大家帮帮我。如果EXIF数据指示旋转是必要的,因为在旋转后的高度和宽度的值仍然视为相同发生
谢谢你
+0
请在编辑您的问题以在调整大小和旋转之前包含图像,以便可以测试您的代码。 –
+1
调整大小和旋转之前的图像添加;) –
+0
您的代码[适用于我](https://i.stack.imgur.com/KRFQo.jpg),您使用的是哪个版本的PHP? –