请输入您要查询的百科知识:

 

词条 ImageCopyResampled
释义

PHP中缩放图像.

有两种改变图像大小的方法.

(1):ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙.

(2):ImageCopyResampled(),其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比 ImageCopyResized() 慢).

两个函数的参数是一样的.如下:

ImageCopyResampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);

ImageCopyResized(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);

它们两个都是从原图像(source)中抓取特定位置(sx,sy)复制图像qu区域到目标t图像(destination)的特定位置(dx,dy)。另外dw,dh指定复制的图像区域在目标图像上的大小,sw,sh指定从原图像复制的图像区域的大小。如果有ps经验的话,就相当于在原图像选择一块区域,剪切移动到目的图像上,同时有拉伸或缩小的操作。

本例将以原来的四分之一大小显示图像。

<?php
// 指定文件路径和缩放比例
$filename = 'test.jpg';
$percent = 0.5;
// 指定头文件Content typezhi值
header('Content-type: image/jpeg');
// 获取图片的宽高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 创建一个图片。接收参数分别为宽高,返回生成的资源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//获取源文件资源句柄。接收参数为图片路径,返回句柄
$source = imagecreatefromjpeg($filename);
// 将源文件剪切全部域并缩小放到目标图片上。前两个为资源句柄
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// 输出给浏览器
imagejpeg($thumb);
?>  本例将把一幅图像按最宽或最高 200 像素来显示。

<?php
// 文件路径
$filename = 'test.jpg';
// 最大宽高
$width = 200;
$height = 200;
// 设置http头Content type值
header('Content-type: image/jpeg');
// 获取图片宽高
list($width_orig, $height_orig) = getimagesize($filename);
if ($width && ($width_orig < $height_orig))
{ //高比宽大,高为200,kuan宽按比例缩小
$width = ($height / $height_orig) * $width_orig;
}else {
$height = ($width / $width_orig) * $height_orig;
}
// 改变大小。和上例一样。
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null,100)
?>

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/3/2 1:04:44