如何翻转部分图像水平与PHP GD


How to flip part of an image horizontal with PHP GD

我正在创建一个图像,其中包含来自另一个图像的部分。我使用图像复制,但"部分"在一起。有些部分需要横向翻转。

例如:

imagecopy($output, $input, 0,8, 44,20, 4,12);

我如何翻转这个部分水平??

我已经试过了:

imagecopy($output, $input, (0 - 1),8, 44,20, 4,12);

但是它不起作用。

我已经找了很长时间,但找不到任何工作

我的脚本是这样的:

$input = imagecreatefrompng($image_file);
$output = imagecreatetruecolor(50, 40);
imagecopy($output, $input, 4,8, 20,20, 8,14);
imagecopy($output, $input, 12,8, 44,20, 4,14);
imagecopy($output, $input, 4,20, 4,20, 4,14);
imagecopy($output, $input, 8,28, 4,20, 4,14);
imagecopy($output, $input, 32,8, 52,20, 4,14);
imagecopy($output, $input, 24,20, 12,20, 4,14);
imagecopy($output, $input, 28,28, 12,20, 4,14); 
imagecopy($output, $input, 4,0, 40,8, 8,9);
imagecopy($output, $input, 24,0, 56,8, 8,9);

header('Content-Type: image/png');
imagepng($output);
imagedestroy($output);
imagedestroy($input);

在这里找到:

//**************************************
// Name: Flip Image with GD
// Description:These are two easy to use functions that will flip an image, either horizontally or vertically. Just create an image, then execute one of these functions on the image resource. The image resource is passed by reference to the functions, so no return values are sent from the functions. Example shown in code.
// By: Ryand833
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1876&lngWId=8//for details.//**************************************
<?php
function flipVertical(&$img) {
 $size_x = imagesx($img);
 $size_y = imagesy($img);
 $temp = imagecreatetruecolor($size_x, $size_y);
 $x = imagecopyresampled($temp, $img, 0, 0, 0, ($size_y-1), $size_x, $size_y, $size_x, 0-$size_y);
 if ($x) {
$img = $temp;
 }
 else {
die("Unable to flip image");
 }
}
function flipHorizontal(&$img) {
 $size_x = imagesx($img);
 $size_y = imagesy($img);
 $temp = imagecreatetruecolor($size_x, $size_y);
 $x = imagecopyresampled($temp, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y);
 if ($x) {
$img = $temp;
 }
 else {
die("Unable to flip image");
 }
}
$myimage = imagecreatefromjpeg("psclogo.jpg");
flipHorizontal($myimage);
header("Content-type: image/jpeg");
imagejpeg($myimage);
?>

以这个想法为起点,你要做的是在图像的每个切片上调用imagecopyresample(如上所述),利用高度/宽度来反转切片。

然后在每个"重新采样"的切片上调用imagecopy,将其复制到新的工作图像中。

参见http://php.net/imagecopy

注释中的image_flip函数

编辑这里是确切的链接:http://cz2.php.net/manual/en/function.imagecopy.php#85992

编辑2要在代码中实现它,只需使用

这行
$flippedOutput = ImageFlip($output,IMAGE_FLIP_HORIZONTAL);

我想在几年后分享答案…:)

改变:

imagecopy($output, $input, 8, 20, 4, 20, 4, 12)

:

imagecopyresampled($output, $input, 8, 20, (8 - 1), 20, 4, 12, 0 - 4, 12);

将图像的一部分水平翻转