我有一张 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);
也许你可以给我指出正确的方向。
非常感谢
最佳答案
函数 imagecopy()
有详细记录并且可以完全满足您的要求。例如:
imagecopy($leftSide, $orig, 0, 0, 0, 0, $width/2, $height);
imagecopy($rightSide, $orig, 0, 0, $width/2, 0, $width/2, $height);
当然,首先您需要将图像写入变量 $orig
中,其函数如下: imagecreatefrompng , imagecreatefromgif等。EG:
$orig= imagecreatefromjpeg('php.jpg');
然后您需要为图像两侧创建新的空图像变量:使用 imagecreatetruecolor ,例如:
$leftSide = imagecreatetruecolor($width/2, $height);
$rightSide = imagecreatetruecolor($width/2, $height);
然后只需使用所需扩展名的函数将这两个变量保存到新文件中,例如 imagejpeg 。例如:
imagejpeg($leftSide, 'leftSide.jpg');
imagejpeg($rightSide, 'rightSide.jpg');
关于PHP将jpg图像分割成两个相等的图像并保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37177801/