我正在尝试在 php 手册中实现用户提供的示例代码,但它没有按预期运行
I'm trying to implement user-supplied example code in the php manual but it doesn't behave as expected
我正在尝试在 PHP 手册的这一页上实现 killing_wombles0000(8 年前)提供的代码:https://www.php.net/manual/en/function.imagecopymerge.php
问题是代码中存在错误/意外行为(例如,$a 未定义)
它有点工作,但是镜像图像逐渐变成白色,我希望它逐渐变成黑色。当我做出我认为需要的改变时,我无法按照我的意愿去做。
代码中有太多我不熟悉的图像函数,我无法理解它们的协同工作。
我花了一整天的时间阅读每个函数,尝试按逻辑处理代码、注释掉部分内容、更改透明度颜色以及通常抓紧稻草。
我认为将出现的 255,255,255 更改为 0,0,0 会使镜像图像淡化为黑色。我真天真 ;)
这是用户在 PHP 手册的该页上提供的代码:
$in = imagecreatefromjpeg('C:\test.jpg');
$reflection_strength = 120; // starting transparency (0-127, 0 being opaque)
$reflection_height = 40; // height of reflection in pixels
$gap = 10; // gap between image and reflection
$orig_height = imagesy($in); // store height of original image
$orig_width = imagesx($in); // store height of original image
$output_height = $orig_height + $reflection_height + $gap; // calculate height of output image
// create new image to use for output. fill with transparency. ALPHA BLENDING MUST BE FALSE
$out = imagecreatetruecolor($orig_width, $output_height);
imagealphablending($out, false);
$bg = imagecolortransparent($out, imagecolorallocatealpha($out, 255, 255, 255, 127));
imagefill($out, 0, 0, $bg);
imagefilledrectangle($out, 0, 0, imagesx($in), imagesy($in), $bg1);
// copy original image onto new one, leaving space underneath for reflection and 'gap'
imagecopyresampled ( $out , $in , 0, 0, 0, 0, imagesx($in), imagesy($in), imagesx($in), imagesy($in));
// create new single-line image to act as buffer while applying transparency
$reflection_section = imagecreatetruecolor(imagesx($in), 1);
imagealphablending($reflection_section, false);
$bg1 = imagecolortransparent($reflection_section, imagecolorallocatealpha($reflection_section, 255, 255, 255, 127));
imagefill($reflection_section, 0, 0, $bg1);
// 1. copy each line individually, starting at the 'bottom' of the image, working upwards.
// 2. set transparency to vary between reflection_strength and 127
// 3. copy line back to mirrored position in original
for ($y = 0; $y<$reflection_height;$y++)
{
$t = ((127-$reflection_strength) + ($reflection_strength*($y/$reflection_height)));
imagecopy($reflection_section, $out, 0, 0, 0, imagesy($in) - $y, imagesx($in), 1);
imagefilter($reflection_section, IMG_FILTER_COLORIZE, 0, 0, 0, $t);
imagecopyresized($out, $reflection_section, $a, imagesy($in) + $y + $gap, 0, 0, imagesx($in) - (2*$a), 1, imagesx($in), 1);
}
// output image to view
header('Content-type: image/png');
imagesavealpha($out,true);
imagepng($out);
在循环进入之前将 $a 设置为 0 可消除错误。
您发现的代码中似乎存在一些错误。
$bg1
没有为主输出图像定义。我在 imagefilledrectangle
. 中将 $bg1
更改为 $bg
$a
未定义。我将对 $a
的引用更改为零,因为它引用了一个 "x" 坐标,我们想要图像的整个宽度,从零到它的全宽度。
反射部分逐渐变透明。由于页面的颜色是白色,图像看起来像是褪色成白色。如果你想淡化成黑色而不是透明,我建议保留默认的混合模式,这样背景颜色就会透出来,并将背景颜色设置为黑色:
//imagealphablending($out, false);
//$bg = imagecolortransparent($out, imagecolorallocatealpha($out, 255, 255, 255, 127));
$bg = imagecolorallocatealpha($out, 0,0,0,0);
这是一个例子:
$in = imagecreatefromjpeg('https://picsum.photos/id/1084/536/354?grayscale');
$reflection_strength = 127; // starting transparency (0-127, 0 being opaque)
$reflection_height = 40; // height of reflection in pixels
$gap = 0; // gap between image and reflection
$orig_height = imagesy($in); // store height of original image
$orig_width = imagesx($in); // store height of original image
$output_height = $orig_height + $reflection_height + $gap; // calculate height of output image
// create new image to use for output. fill with BLACK.
$out = imagecreatetruecolor($orig_width, $output_height);
//imagealphablending($out, false);
//$bg = imagecolortransparent($out, imagecolorallocatealpha($out, 255, 255, 255, 127));
$bg = imagecolorallocatealpha($out, 0,0,0,0);
imagefill($out, 0, 0, $bg);
imagefilledrectangle($out, 0, 0, imagesx($in), imagesy($in), $bg);
// copy original image onto new one, leaving space underneath for reflection and 'gap'
imagecopyresampled ( $out , $in , 0, 0, 0, 0, imagesx($in), imagesy($in), imagesx($in), imagesy($in));
// create new single-line image to act as buffer while applying transparency
$reflection_section = imagecreatetruecolor(imagesx($in), 1);
imagealphablending($reflection_section, false);
$bg1 = imagecolortransparent($reflection_section, imagecolorallocatealpha($reflection_section, 255, 255, 255, 127));
imagefill($reflection_section, 0, 0, $bg1);
// 1. copy each line individually, starting at the 'bottom' of the image, working upwards.
// 2. set transparency to vary between reflection_strength and 127
// 3. copy line back to mirrored position in original
for ($y = 0; $y<$reflection_height;$y++)
{
$t = ((127-$reflection_strength) + ($reflection_strength*($y/$reflection_height)));
imagecopy($reflection_section, $out, 0, 0, 0, imagesy($in) - $y, imagesx($in), 1);
imagefilter($reflection_section, IMG_FILTER_COLORIZE, 0, 0, 0, $t);
imagecopyresized($out, $reflection_section, 0, imagesy($in) + $y + $gap, 0, 0, imagesx($in) - 0, 1, imagesx($in), 1);
}
// output image to view
header('Content-type: image/png');
imagesavealpha($out,true);
imagepng($out);
我正在尝试在 PHP 手册的这一页上实现 killing_wombles0000(8 年前)提供的代码:https://www.php.net/manual/en/function.imagecopymerge.php 问题是代码中存在错误/意外行为(例如,$a 未定义) 它有点工作,但是镜像图像逐渐变成白色,我希望它逐渐变成黑色。当我做出我认为需要的改变时,我无法按照我的意愿去做。 代码中有太多我不熟悉的图像函数,我无法理解它们的协同工作。
我花了一整天的时间阅读每个函数,尝试按逻辑处理代码、注释掉部分内容、更改透明度颜色以及通常抓紧稻草。 我认为将出现的 255,255,255 更改为 0,0,0 会使镜像图像淡化为黑色。我真天真 ;)
这是用户在 PHP 手册的该页上提供的代码:
$in = imagecreatefromjpeg('C:\test.jpg');
$reflection_strength = 120; // starting transparency (0-127, 0 being opaque)
$reflection_height = 40; // height of reflection in pixels
$gap = 10; // gap between image and reflection
$orig_height = imagesy($in); // store height of original image
$orig_width = imagesx($in); // store height of original image
$output_height = $orig_height + $reflection_height + $gap; // calculate height of output image
// create new image to use for output. fill with transparency. ALPHA BLENDING MUST BE FALSE
$out = imagecreatetruecolor($orig_width, $output_height);
imagealphablending($out, false);
$bg = imagecolortransparent($out, imagecolorallocatealpha($out, 255, 255, 255, 127));
imagefill($out, 0, 0, $bg);
imagefilledrectangle($out, 0, 0, imagesx($in), imagesy($in), $bg1);
// copy original image onto new one, leaving space underneath for reflection and 'gap'
imagecopyresampled ( $out , $in , 0, 0, 0, 0, imagesx($in), imagesy($in), imagesx($in), imagesy($in));
// create new single-line image to act as buffer while applying transparency
$reflection_section = imagecreatetruecolor(imagesx($in), 1);
imagealphablending($reflection_section, false);
$bg1 = imagecolortransparent($reflection_section, imagecolorallocatealpha($reflection_section, 255, 255, 255, 127));
imagefill($reflection_section, 0, 0, $bg1);
// 1. copy each line individually, starting at the 'bottom' of the image, working upwards.
// 2. set transparency to vary between reflection_strength and 127
// 3. copy line back to mirrored position in original
for ($y = 0; $y<$reflection_height;$y++)
{
$t = ((127-$reflection_strength) + ($reflection_strength*($y/$reflection_height)));
imagecopy($reflection_section, $out, 0, 0, 0, imagesy($in) - $y, imagesx($in), 1);
imagefilter($reflection_section, IMG_FILTER_COLORIZE, 0, 0, 0, $t);
imagecopyresized($out, $reflection_section, $a, imagesy($in) + $y + $gap, 0, 0, imagesx($in) - (2*$a), 1, imagesx($in), 1);
}
// output image to view
header('Content-type: image/png');
imagesavealpha($out,true);
imagepng($out);
在循环进入之前将 $a 设置为 0 可消除错误。
您发现的代码中似乎存在一些错误。
$bg1
没有为主输出图像定义。我在imagefilledrectangle
. 中将 $a
未定义。我将对$a
的引用更改为零,因为它引用了一个 "x" 坐标,我们想要图像的整个宽度,从零到它的全宽度。
$bg1
更改为 $bg
反射部分逐渐变透明。由于页面的颜色是白色,图像看起来像是褪色成白色。如果你想淡化成黑色而不是透明,我建议保留默认的混合模式,这样背景颜色就会透出来,并将背景颜色设置为黑色:
//imagealphablending($out, false);
//$bg = imagecolortransparent($out, imagecolorallocatealpha($out, 255, 255, 255, 127));
$bg = imagecolorallocatealpha($out, 0,0,0,0);
这是一个例子:
$in = imagecreatefromjpeg('https://picsum.photos/id/1084/536/354?grayscale');
$reflection_strength = 127; // starting transparency (0-127, 0 being opaque)
$reflection_height = 40; // height of reflection in pixels
$gap = 0; // gap between image and reflection
$orig_height = imagesy($in); // store height of original image
$orig_width = imagesx($in); // store height of original image
$output_height = $orig_height + $reflection_height + $gap; // calculate height of output image
// create new image to use for output. fill with BLACK.
$out = imagecreatetruecolor($orig_width, $output_height);
//imagealphablending($out, false);
//$bg = imagecolortransparent($out, imagecolorallocatealpha($out, 255, 255, 255, 127));
$bg = imagecolorallocatealpha($out, 0,0,0,0);
imagefill($out, 0, 0, $bg);
imagefilledrectangle($out, 0, 0, imagesx($in), imagesy($in), $bg);
// copy original image onto new one, leaving space underneath for reflection and 'gap'
imagecopyresampled ( $out , $in , 0, 0, 0, 0, imagesx($in), imagesy($in), imagesx($in), imagesy($in));
// create new single-line image to act as buffer while applying transparency
$reflection_section = imagecreatetruecolor(imagesx($in), 1);
imagealphablending($reflection_section, false);
$bg1 = imagecolortransparent($reflection_section, imagecolorallocatealpha($reflection_section, 255, 255, 255, 127));
imagefill($reflection_section, 0, 0, $bg1);
// 1. copy each line individually, starting at the 'bottom' of the image, working upwards.
// 2. set transparency to vary between reflection_strength and 127
// 3. copy line back to mirrored position in original
for ($y = 0; $y<$reflection_height;$y++)
{
$t = ((127-$reflection_strength) + ($reflection_strength*($y/$reflection_height)));
imagecopy($reflection_section, $out, 0, 0, 0, imagesy($in) - $y, imagesx($in), 1);
imagefilter($reflection_section, IMG_FILTER_COLORIZE, 0, 0, 0, $t);
imagecopyresized($out, $reflection_section, 0, imagesy($in) + $y + $gap, 0, 0, imagesx($in) - 0, 1, imagesx($in), 1);
}
// output image to view
header('Content-type: image/png');
imagesavealpha($out,true);
imagepng($out);