dongqieli4164 2014-08-24 19:32
浏览 29
已采纳

通过php文件裁剪图像

so i have an image that is originally 1696x1696. i want to use a php file to MIME a png file. what i want the php to do is crop the original file and produce a quarter of the image. Later i plan to use $_GET variables to return which quadrant i want, but for testing/debugging, im just aiming to get the top left quadrant.

here's the code:

Header("Content-type:image/png");
$newImg =imagecreatefrompng('test.png'); 

//manually entered half height and width
$img=imagecreatetruecolor(848,848);
//here is where the bugs keep flawing the image
imagecopyresampled($img,$newImg,0,0,0,0,1696,1696,1696,1696);
imagepng($img); 
imagedestroy($img);

this will produce the image (top, left) like it's supposed to, however it adds several smaller resampled images on top of it. no matter how i toy with it, i cant get it right. i've also tried imagecopy() and cant get it right as well. looked up tutorials and i cant seem to find on that helps.

  • 写回答

2条回答 默认 最新

  • dp0518 2014-08-24 20:14
    关注

    Your code looks fine. The only thing I would change is to use imagecopyresized() instead of imagecopyresampled() in this use case.

    Header("Content-type:image/png");
    $source = imagecreatefrompng('images/test.png'); 
    
    // manually entered half height and width
    $thumb  = imagecreatetruecolor(848,848);
    
    imagecopyresized($thumb, $source, 0, 0, 0, 0, 1696, 1696, 1696, 1696);
    imagepng($thumb); 
    imagedestroy($thumb);
    

    I am guessing that earlier in your tests, you were overwriting your original image. That would explain the...

    however it adds several smaller resampled images on top of it...

    ...part of your experience. Each time you ran the code, you picked up the previously modified file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据