PHP截取图片的某个区域

这一技客 2009-11-19 23:19:41 累计浏览 3,981 次

解释一下主要参数:
imagecopyresampled($newim, $im, 0, 0, 7, 174, 120, 42, $new_img_width, $new_img_height); // 原始尺寸 120 x 42
imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width, $new_img_height); // 截取出来后放到 500 x 500
imagecopyresampled($newim, $im, 0, 0, 100, 30, 10, 10, $new_img_width, $new_img_height); // 截取出来后缩小到 10 x 10

原始代码如下:

$filename = ’D:/ex.jpg’; 
/*读取图片 */ 
$im = imagecreatefromjpeg($filename); 
/* 图片要截多少, 长/宽 */ 
$new_img_width  = 140; 
$new_img_height = 20; 
/* 先建立一个 新的空白图片档 */ 
$newim = imagecreatetruecolor($new_img_width, $new_img_height); 
// 输出图要从哪边开始x, y , 原始图要从哪边开始 x, y , 要输多大 x, y(resize) , 要抓多大 x, y 
imagecopyresampled($newim, $im, 0, 0, 329, 182, 140, 20, $new_img_width, $new_img_height); 
/* 放大 成 500 x 500 的图 */ 
//imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width, $new_img_height); 
/* 把图印出来 */ 
//$file = imagejpeg($newim,’’,100); 
/* 保存图片 */ 
$to_File = ’D:/AppServ/www/ceshi/lin.jpeg’; 
ImageJpeg($newim,$to_File,100); 
/* 资源回收 */ 
imagedestroy($newim); 
imagedestroy($im);

同分类推荐文章

  1. NGINX 1.31.7: Native GeoIP2 support, HTTP/2 priorities, and QUIC improvements (2026-11-13 23:40:02)
  2. 说说广场 API 0.31.0 版的调用代码说明 (2026-11-13 19:10:02)
  3. NGINX Routing on HTTP Request Body (2026-10-27 14:10:42)

查看更多 后端 文章 →

建议继续学习

  1. 使用gettext来支持PHP的多语言 (累计阅读 39,702)
  2. WordPress插件开发 -- 在插件使用数据库存储数据 (累计阅读 29,568)
  3. Paypal接口详细代码(PHP版,非API接口) (累计阅读 19,821)
  4. 我的PHP,Python和Ruby之路 (累计阅读 13,719)
  5. include(“./file.php”)和include(“file.php”)区别 (累计阅读 13,274)
  6. 15个最好的免费开源电子商务平台 (累计阅读 12,949)
  7. Redis消息队列的若干实现方式 (累计阅读 12,536)
  8. 到底什么是MVC? (累计阅读 12,384)
  9. 整理了一份招PHP高级工程师的面试题 (累计阅读 12,221)
  10. Rolling cURL: PHP并发最佳实践 (累计阅读 11,972)