php图像处理函数imagecopyresampled用法详解

yipeiwu_com6年前PHP代码库

本文实例讲述了php图像处理函数imagecopyresampled用法。分享给大家供大家参考,具体如下:

语法

复制代码 代码如下:
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

参数

dst_image 目标图象连接资源。
src_image 源图象连接资源。
dst_x 目标 X 坐标点。
dst_y 目标 Y 坐标点。
src_x 源的 X 坐标点。
src_y 源的 Y 坐标点。
dst_w 目标宽度。
dst_h 目标高度。
src_w 源图象的宽度。
src_h 源图象的高度。

成功时返回 TRUE, 或者在失败时返回 FALSE。

案例

案例(图像裁减):

<?php
  $targ_w = $targ_h = 150; // 设置目标宽度与高度
  $jpeg_quality = 90; // 图片质量90,满分为100
  $src = 'demo_files/pool.jpg'; // 被处理的图片
  $img_r = imagecreatefromjpeg($src); // 获取原图
  $dst_r = ImageCreateTrueColor( $targ_w, $targ_h ); // 获取新图
  imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
  $targ_w,$targ_h,$_POST['w'],$_POST['h']); // 目标图 源图 目标X坐标点 目标Y坐标点 源的X坐标点 源的Y坐标点 目标宽度 目标高度 源图宽度 源图高度
  header('Content-type: image/jpeg');
  imagejpeg($dst_r,null,$jpeg_quality); // 输出图象到浏览器或文件
?>

案例二(重新取样):

<?php
// 源文件
$filename = '1.jpg';
// 设置最大宽高
$width = 400;
$height = 400;
// Content type
header('Content-Type: image/jpeg');
// 获取新尺寸
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
  $width = $height*$ratio_orig;
} else {
  $height = $width/$ratio_orig;
}
// 重新取样
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// 输出
imagejpeg($image_p, null, 100);
?>

附上上传图片的三种思路

1.选择图片,提交表单,服务器统一处理上传,保存路径

2.选择图片,上传,获取路径,提交表单,保存路径

3.选择图片,上传到服务器,通过某种途径获取到服务器的图片,保存到本地

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php文件管理基本功能简单操作

php文件管理基本功能简单操作

(1)先要想好要操作哪个文件? (2)确定文件的路径? (3)要有什么文件管理功能? 一、先做一下简单的查看文件功能,文件中的文件和文件夹都显示,但是双击文件夹可以显示下一级子目录,双击...

PHP页面转UTF-8中文编码乱码的解决办法

对于乱码这个问题php开发者几乎都会有碰到过,我们下面主要是介绍了php文件乱码和页面乱码。 PHP页面转UTF-8编码问题 1.在代码开始出加入一行: header("Content...

php下使用strpos需要注意 === 运算符

复制代码 代码如下:<?php /* 判断字符串是否存在的函数 */ function strexists($haystack, $needle) { return !(strpo...

php地址引用(php地址引用的效率问题)

复制代码 代码如下: <?php echo 'begin time:'.$begin=microtime(false).'<br/>';//begin to count...

说明的比较细的php 正则学习实例

"^The": 匹配以 "The"开头的字符串;    "of despair$": 匹配以 "of despair...