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

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?