dongwei4444 2010-10-27 06:13
浏览 84
已采纳

在PHP中裁剪图像的一部分并调整大小

I have a source image (can be any image with and have different dimensions). I want to be able to tell php an area from the image to crop out and resize. For example:

  • The source image is 800x800 pixels.
  • PHP selects a 50x50 pixel region of the image with the selection area starting 80 pixels from the top edge of the source image, and 100 pixels from the left edge of the source image
  • The selected region is then resized to 37x37 pixels.

I tried using the functions imagecopyresampled() and imagecopyresized() but they both gave me a weird problem: imagecopyresampled() outputs the selection image but is surrounded by a black area equivalent to the dimensions of the source image. imagecopyresized() also selects the selected region, but it is just overlayed on the source image. The end goal here is to make thumbnails from the source image.

An example of the code I am already using is:

list($width_orig, $height_orig) = getimagesize($filename);
imagecopyresized($image_final, $image, 0, 0, 0, 0, 37, 37, $width_orig, $height_orig);

The above does not do exactly as the task describes, but it demonstrates the problem that I am running into.

Note:

I understand that doing a search on this topic leads to a lot of results but they mostly reference libraries and classes that do a lot more than I actually need. I'd rather have an understanding of the solution and implement something more efficient. Hence, I am not looking to be pointed to an exhaustive script that accomplishes the above task and 50 other tasks. I'd rather understand the above so I can create an optimal, custom solution.

  • 写回答

2条回答 默认 最新

  • dtysql0586 2010-10-27 06:19
    关注

    Try my image manipulator class, it does exactly what you want - http://github.com/philBrown/Tolerable/blob/master/library/Tolerable/Image/Manipulator.php

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

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输