【发布时间】:2013-06-11 16:48:58
【问题描述】:
我注意到有一些脚本将尝试执行此操作,但没有一个非常适合我的情况。
我收集了一些我发现的脚本来尝试提出正确的解决方案。
但是我现在遇到了 2 个问题。
- 图像未居中。
- 三角形的边长不相等。
演示在http://owenmelbourne.com/triangle.php
代码是
$src = imagecreatefromjpeg ('http://uat.eruptedevents.secureping.co.uk/media/images/upload/1200/154.jpg');
// Get image width/height
$srcWidth = imagesx ($src);
$srcHeight = imagesy ($src);
// Get centre position
$centreX = floor ($srcWidth / 2);
$centreY = floor ($srcHeight / 2);
// Set new image size and start x/y
$destWidth = $srcWidth;
$destHeight = $srcHeight;
$destSX = 0;
$destSY = $centreY;
// Create the image
$square = 500;
if( $srcWidth >= $srcHeight ){
$square = $srcHeight;
}
else {
$square = $srcWidth;
}
$shift = array ("left" => 0, "top" => 0);
$shift["left"] = ( $srcWidth / 4 ) * -1;
$shift["top"] = ( $srcHeight / 4 ) * -1;
$dest = imagecreatetruecolor ($square, $square);
// Copy from source
imagecopy ($dest, $src, $shift["left"], $shift["top"], 0, 0, $destWidth, $destHeight);
// OK... we now have the correctly sized rectangle copied over from the source image
// Lets cut it down and turn it into the triangle we want by removing the other triangles
// We remove the area by defining another colour as transparent and creating shapes with that colour
$colRed = imagecolorallocatealpha ($dest, 255, 0, 0, 0);
$blue = imagecolorallocatealpha ($dest, 0, 0, 244, 0);
imagecolortransparent ($dest, $colRed);
$sidelength = $square;
imagefilledpolygon ($dest, array (
0, 0,
$square/2, 0,
0, $square
), 3, $colRed);
imagefilledpolygon ($dest, array (
$square, 0,
$square, $square,
$square/2, 0
), 3, $colRed);
$dest2 = imagecreatetruecolor ($square, $square);
// Output new image
header ('Content-Type: image/png');
imagepng ($dest);
// Clean up
imagedestroy ($thumbNail);
imagedestroy ($dest);
我将如何从中间截取一个完美的三角形并将其作为图像返回?
非常感谢
【问题讨论】:
-
设置
$square = 500的目的是什么,因为之后您直接用源的较大尺寸覆盖它。你想要一个 500px 的正方形吗?$shift偏移量计算看起来也很狡猾。 -
是旧代码,我一直在试图弄清楚,所以就留在那里,但不,我希望三角形尽可能大。并且 $shift 计算很可能是狡猾的!我绝不是一个能做比乘法表更多的数学家:P - 这里可能有很多错误,但我以前从未做过,这就是为什么它一团糟!
-
那么您的目标是什么,请确保在问题中非常清楚,因为很难从代码中分辨出它目前的情况。您是否想要一个基于平面的三角形切口,其顶点位于图像的中点,与原始尺寸相同?
-
我想输出一个二维三角形,其填充作为背景图像。如此处所示 - owenmelbourne.com/triangle.php 但是,我希望三角形的所有边都等长,因此三角形的中心点也是原始图像的中心点。我希望三角形与原始图像一样大。这更有意义吗?谢谢你
-
是的,这是有道理的,三角形是否必须向上,即。有特定的方向吗?