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

标签 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);

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

非常感谢

最佳答案

函数 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/

相关文章:

c# - 如何在 WPF 中使用 OpenDialog 将文件复制到特定目录并设置文件名、扩展名?

php - Laravel 4 Eloquent 选择

php - 我正在尝试用 PHP 为服务器/InFrame 支付编写自己的类

image - FFMPEG视频到图像批量转换

javascript - 使用 CSS 将图像垂直和水平居中

ruby-on-rails - 带有嵌套模型的 rails 和回形针 : uploads from form not working

php - 来自 SQL 表的 AJAX 自动完成?

php cron 替换

java - 从 GUI 读取数据

c# - DataGrid 保存按钮和 CanExecute