PHP将jpg图像分成两个相等的图像并保存

时间:2016-05-12 05:13:11

标签: php image save crop

我有一张jpg图片,我想分成两个相同的图像。分割应该发生在图像的水平中心并保存两个部分(左侧部分,右侧部分)。例如,500x300的图像将被分成两个图像,每个图像250x300。我不熟悉正确的图像处理功能,当检查PHP的文档时,它清楚地警告' imagecrop()'未记录(http://php.net/manual/en/function.imagecrop.php)。 另外在stackoverflow上,我发现的唯一一件事是我尝试使用的代码片段:

// copy left third to output image
imagecopy($output, $orig,$padding,$padding,0, 0,$width/3,$height);
// copy central third to output image
imagecopy($output, $orig,2*$padding+$width/3,$padding,$width/3, 0,$width/3,$height);

也许你可以指出我正确的方向。

非常感谢

1 个答案:

答案 0 :(得分:4)

功能imagecopy()已有详细记录。可以做你想要的。例如:

imagecopy($leftSide, $orig, 0, 0, 0, 0, $width/2, $height);
imagecopy($rightSide, $orig, 0, 0, $width/2, 0, $width/2, $height);

首先,您需要在变量$orig中使用以下函数编写图像:imagecreatefrompngimagecreatefromgif等.EG:

$orig= imagecreatefromjpeg('php.jpg');

然后你需要为两个图像边创建新的空图像变量:使用imagecreatetruecolor,例如:

$leftSide = imagecreatetruecolor($width/2, $height);
$rightSide = imagecreatetruecolor($width/2, $height);

然后使用所需扩展功能将这两个变量保存到新文件中,例如imagejpeg。 EG:

imagejpeg($leftSide, 'leftSide.jpg');
imagejpeg($rightSide, 'rightSide.jpg');