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 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改