gpt4 book ai didi

php - imagecopyresampled 旋转图像后

转载 作者:搜寻专家 更新时间:2023-10-31 21:29:54 26 4
gpt4 key购买 nike

我试图在旋转图像(许多图像$im)后复制图像(到$img),但我得到了奇怪的行为。一旦我取消注释 //$img = 行,我只会在最终输出中获得旋转图像。我可以旋转内部 $im 并将其复制到最终图像 $img 吗?

<?php

$height = 80;
$width = 300;

$img = imagecreate($width, $height);
$c = imagecolorallocate ($img , 135, 135, 135);
imagefill($img, 0, 0, imagecolorallocate($img, 255, 255, 255));

for($i=0; $i<=5; $i++){

$im = imagecreatetruecolor(35, 35);
$gry = imagecolorallocate($im, 135, 135, 135);
$wht = imagecolorallocate($im, 255, 255, 255);

$j = mt_rand(0, 1);

$ch = mt_rand(0,1)?chr(rand(65, 90)):chr(rand(97, 122));

if($j == 0){
imagefill($im, 0, 0, $wht);
imagefttext($im, 20, 0, 3, 21, $gry, 'AHGBold.ttf', $ch);
//$img = imagerotate($im, mt_rand(0,10)-5, $wht);
}else{
imagefill($im, 0, 0, $gry);
imagefttext($im, 20, 0, 3, 21, $wht, 'AHGBold.ttf', $ch);
//$img = imagerotate($im, mt_rand(0,10)-5, $gry);
}
imagecopyresampled($img, $im, 5 + $i*42, $height/2 - 12, 0, 0, 40, 40, 25, 25);
}
header('Content-type: image/png');
imagepng($img);

最佳答案

改变

 $img = imagerotate($im, mt_rand(0,10)-5, $wht);

 $img = imagerotate($im, mt_rand(0,10)-5, $gry);

 $im = imagerotate($im, mt_rand(0,10)-5, $wht);

 $im = imagerotate($im, mt_rand(0,10)-5, $gry);

imagerotes 不起作用的情况下,您可以使用以下函数来旋转图像:

function imagerotateEquivalent(&$srcImg, $angle, $bgcolor, $ignore_transparent = 0) 
{
$srcw = imagesx($srcImg);
$srch = imagesy($srcImg);

if($angle == 0) return $srcImg;

// Convert the angle to radians
$theta = deg2rad ($angle);


// Calculate the width of the destination image.
$temp = array ( rotateX(0, 0, 0-$theta),
rotateX($srcw, 0, 0-$theta),
rotateX(0, $srch, 0-$theta),
rotateX($srcw, $srch, 0-$theta)
);
$minX = floor(min($temp));
$maxX = ceil(max($temp));
$width = $maxX - $minX;

// Calculate the height of the destination image.
$temp = array ( rotateY(0, 0, 0-$theta),
rotateY($srcw, 0, 0-$theta),
rotateY(0, $srch, 0-$theta),
rotateY($srcw, $srch, 0-$theta)
);
$minY = floor(min($temp));
$maxY = ceil(max($temp));
$height = $maxY - $minY;

$destimg = imagecreatetruecolor($width, $height);
imagefill($destimg, 0, 0, imagecolorallocate($destimg, 0,255, 0));

// sets all pixels in the new image
for($x=$minX;$x<$maxX;$x++) {
for($y=$minY;$y<$maxY;$y++)
{
// fetch corresponding pixel from the source image
$srcX = round(rotateX($x, $y, $theta));
$srcY = round(rotateY($x, $y, $theta));
if($srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY < $srch)
{
$color = imagecolorat($srcImg, $srcX, $srcY );
}
else
{
$color = $bgcolor;
}
imagesetpixel($destimg, $x-$minX, $y-$minY, $color);
}
}

return $destimg;
}

function rotateX($x, $y, $theta){
return $x * cos($theta) - $y * sin($theta);
}
function rotateY($x, $y, $theta){
return $x * sin($theta) + $y * cos($theta);
}

我从 a note in php.net 得到上面的代码

关于php - imagecopyresampled 旋转图像后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31109771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com