PHP 将 jpg 图片拆分为两个相等的图片并保存
PHP split jpg image into two equal images and save
我有一张 jpg 图片,我想将其分成两个相等的图像。拆分应该发生在图像的水平中心并保存两个部分(左部分,右部分)。例如,一张 500x300 的图像将被拆分为两张 250x300 的图像。我不熟悉正确的图像处理功能,在检查 PHP 的文档时,它清楚地警告说 'imagecrop()' 没有记录(http://php.net/manual/en/function.imagecrop.php)。
同样在 Whosebug 上,我发现的唯一一件事就是我尝试使用的这个片段:
// 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,等等。例如:
$orig= imagecreatefromjpeg('php.jpg');
然后你需要为图像的两面创建新的空图像变量:使用imagecreatetruecolor,例如:
$leftSide = imagecreatetruecolor($width/2, $height);
$rightSide = imagecreatetruecolor($width/2, $height);
然后使用需要扩展的函数将这两个变量保存到新文件中,例如 imagejpeg。例如:
imagejpeg($leftSide, 'leftSide.jpg');
imagejpeg($rightSide, 'rightSide.jpg');
我有一张 jpg 图片,我想将其分成两个相等的图像。拆分应该发生在图像的水平中心并保存两个部分(左部分,右部分)。例如,一张 500x300 的图像将被拆分为两张 250x300 的图像。我不熟悉正确的图像处理功能,在检查 PHP 的文档时,它清楚地警告说 'imagecrop()' 没有记录(http://php.net/manual/en/function.imagecrop.php)。 同样在 Whosebug 上,我发现的唯一一件事就是我尝试使用的这个片段:
// 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,等等。例如:
$orig= imagecreatefromjpeg('php.jpg');
然后你需要为图像的两面创建新的空图像变量:使用imagecreatetruecolor,例如:
$leftSide = imagecreatetruecolor($width/2, $height);
$rightSide = imagecreatetruecolor($width/2, $height);
然后使用需要扩展的函数将这两个变量保存到新文件中,例如 imagejpeg。例如:
imagejpeg($leftSide, 'leftSide.jpg');
imagejpeg($rightSide, 'rightSide.jpg');