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

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services