【问题标题】:PHP split jpg image into two equal images and savePHP将jpg图像分成两个相等的图像并保存
【发布时间】:2016-05-12 05:13:11
【问题描述】:

我有一张 jpg 图片,我想将其分成两个相等的图像。分割应该发生在图像的水平中心并保存两个部分(左侧部分,右侧部分)。例如,一个 500x300 的图像将被分成两个图像,每个 250x300。我不熟悉正确的图像处理功能,当检查 PHP 的文档时,它清楚地警告说没有记录“imagecrop()”(http://php.net/manual/en/function.imagecrop.php)。 同样在stackoverflow上,我发现的唯一东西是我试图玩弄的这个sn-p:

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

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

非常感谢

【问题讨论】:

    标签: php image save crop


    【解决方案1】:

    函数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。例如:

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

    【讨论】:

    • 没想到需要这么多函数来处理这个。解决了我的问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 2021-06-25
    • 2020-12-23
    相关资源
    最近更新 更多