我注意到有一些脚本要尝试执行此操作,但没有一个非常适合我的情况。
我拼凑了一些我发现尝试提出正确解决方案的脚本。
但是我现在遇到了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);
我将如何从中间进行完美的三角形裁剪,并将其作为图像返回?
非常感谢