donglue1886 2016-05-12 05:13
浏览 62
已采纳

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

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.

Thanks a lot

  • 写回答

1条回答 默认 最新

  • dsf4354353452 2016-05-12 06:30
    关注

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

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

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

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

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据