以下内容可能会有用。它将图像中最大的中心圆提取到指定大小的正方形中,并可以选择旋转。正方形的其余部分将设置为透明,因此可用于在其他图像上绘制。我将其命名为“双筒望远镜效果”,因为在一些老式电视节目中,每当他们展示有人通过双筒望远镜看东西时,屏幕上就会显示一个带有黑色边缘的大圆形图像。
<?php
function image_binocular_effect($src, $bearing, $out_square) {
$out = imagecreatetruecolor($out_square, $out_square);
$width=imagesx($src);
$height=imagesy($src);
$square=min($width, $height);
imagecopyresampled($out, $src, 0, 0, ($width - $square)/2 , ($height - $square)/2, $out_square, $out_square, $square, $square);
$mask = imagecreatetruecolor($out_square, $out_square);
$black = ImageColorAllocate ($mask, 0, 0, 0);
$white = ImageColorAllocate ($mask, 255, 255, 255);
imagefilledrectangle($mask , 0, 0, $out_square, $out_square, $white);
$centrexy=$out_square / 2;
imagefilledellipse($mask, $centrexy, $centrexy, $out_square, $out_square, $black);
ImageColorTransparent($mask, $black);
imagecopymerge($out, $mask, 0, 0, 0, 0, $out_square, $out_square, 100);
if ($bearing != 0) {
$rotated_img=imagerotate($out , 360-$bearing, $white);
$rotated_map_width = imagesx($rotated_img);
$rotated_map_height = imagesy($rotated_img);
imagecopy($out, $rotated_img, 0, 0, ($rotated_map_width - $out_square) / 2, ($rotated_map_height - $out_square) / 2, $out_square, $out_square);
}
ImageColorTransparent($out, $white);
return $out;
}
$src = imagecreatetruecolor(200, 50);
imagefilledrectangle($src, 0, 0, 200, 50, imagecolorallocate($src, 255, 255, 255));
ImageString($src, 3, 10, 10, "This is a sample image to illustrate the binocular effect", imagecolorallocate($im, 192, 0, 0));
$img=image_binocular_effect($src, 72, 50);
ImagePNG($img,"test.png");
?>