在水印PHP中获得黑线。我正在使用PHP版本7.2.19。
public function Watermark($input, $output, $watermark, $opacity = 50) {
try {
$image = $this->imagecreatefrom($input);
$stamp = $this->imagecreatefrom($watermark);
$watermark_width = imagesx($stamp);
$watermark_height = imagesy($stamp);
$percent = 0.5;
$newwidth = $watermark_width * $percent;
$newheight = $watermark_height * $percent;
$stamp_new = imagecreatetruecolor($newwidth, $newheight);
imagecolortransparent($stamp_new, imagecolorallocatealpha($stamp_new, 0, 0, 0, 127));
imagealphablending($stamp_new, false);
imagesavealpha($stamp_new, true);
imagecopyresampled($stamp_new, $stamp, 0, 0, 0, 0, $newwidth, $newheight, imagesx($stamp), imagesy($stamp));
$margin = ['right' => 20, 'bottom' => 20];
imagecopymerge($image, $stamp_new, imagesx($image) - imagesx($stamp_new) - $margin['right'], imagesy($image) - imagesy($stamp_new) - $margin['bottom'], 0, 0, imagesx($stamp_new), imagesy($stamp_new), $opacity);
imagejpeg($image, $output);
ImageDestroy($image);
ImageDestroy($stamp);
return true;
}
catch(\Exception $e) {
print_r($e->getMessage());
die;
Yii::$app->api->sendFailedResponse("Something went wrong please try again later");
}
}
private function imagecreatefrom($input) {
$size = getimagesize($input);
if ($size) {
switch ($size['mime']) {
case 'image/jpeg':
$base = imagecreatefromjpeg($input);
imagealphablending($base, false);
imagesavealpha($base, true);
return $base;
break;
case 'image/png':
$base = imagecreatefrompng($input);
imagealphablending($base, false);
imagesavealpha($base, true);
return $base;
break;
case 'image/gif':
$base = imagecreatefromgif($input);
imagealphablending($base, false);
imagesavealpha($base, true);
return $base;
break;
case 'image/vnd.wap.wbmp':
$base = imagecreatefromwbmp($input);
imagealphablending($base, false);
imagesavealpha($base, true);
return $base;
break;
case '':
default:
return false;
break;
}
}
return false;
}
PHP版本7.2.19