PHP将jpg图像分成两个相等的图像并保存 [英] PHP split jpg image into two equal images and save

查看:71
本文介绍了PHP将jpg图像分成两个相等的图像并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张jpg图片,我想将其分成两个相等的图像.分割应在图像的水平中心进行,并保存两个部分(左侧部分,右侧部分).例如,一个500x300的图像将被分成两个图像,每个图像分别为250x300.我不熟悉正确的图像处理功能,在检查PHP的文档时,它会明确警告未记录"imagecrop()"( http://php.net/manual/en/function.imagecrop.php ).同样在stackoverflow上,我发现的唯一片段是我尝试使用的片段:

I have a jpg picture which I want to split into two equal images. The split should happen in the horizontal center of the image and save both parts (left part, right part). For instance an image of 500x300 would be split into two images each 250x300. I am not familiar with the right image processing function, and when checking PHP's docs it clearly warns that 'imagecrop()' is not documented (http://php.net/manual/en/function.imagecrop.php). Also on stackoverflow, the only thing which I found is this snippet which I tried to play around with:

// 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);

也许您可以为我指明正确的方向.

Maybe you could point me to the right direction.

非常感谢

推荐答案

函数 imagecopy()有据可查&完全可以做您想要的.例如:

Function imagecopy() is well documented & can do exactly what you want. For example:

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

当然,首先您需要使用类似 $ orig 中写入图像.="noreferrer"> imagecreatefrompng imagecreatefromgif 等.例如:

Ofcourse first you need to write your image in variable $orig with functions like: imagecreatefrompng, imagecreatefromgif, etc. EG:

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

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

Then you need to create new empty image variables for both image sides: with imagecreatetruecolor, eg:

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

然后使用所需扩展名的函数将这两个变量保存到新文件中,例如 imagejpeg.EG:

And then just save those two variables to new files using functions by needed extension, like imagejpeg. EG:

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

这篇关于PHP将jpg图像分成两个相等的图像并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆