微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP中改变图片的尺寸大小的代码

先介绍一个自己写的函数
<div class="codetitle"><a style="CURSOR: pointer" data="51813" class="copybut" id="copybut51813" onclick="doCopy('code51813')"> 代码如下:

<div class="codebody" id="code51813">
<?PHP
$imgsrc = "http://www.Nowamagic.net/images/3.jpg";
$width = 780;
$height = 420;
resizejpg($imgsrc,$imgdst,$width,$height);
function resizejpg($imgsrc,$imgwidth,$imgheight)
{
//$imgsrc jpg格式图像路径 $imgdst jpg格式图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度
//取得图片的宽度,高度值
$arr = getimagesize($imgsrc);
header("Content-type: image/jpg");
$imgWidth = $imgwidth;
$imgHeight = $imgheight;
// Create image and define colors
$imgsrc = imagecreatefromjpeg($imgsrc);
$image = imagecreatetruecolor($imgWidth,$imgHeight); //创建一个彩色的底图
imagecopyresampled($image,$imgsrc,$imgWidth,$imgHeight,$arr[0],$arr[1]);
imagepng($image);
imagedestroy($image);
}
?>

imagecopyresampled
imagecopyresampled -- 重采样拷贝部分图像并调整大小。
int imagecopyresampled ( resource dst_im,resource src_im,int dstX,int dstY,int srcX,int srcY,int dstW,int dstH,int srcW,int srcH)
imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。dst_im 和 src_im 分别是目标图像和源图像的标识符。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果 dst_im 和 src_im 相同的话)区域,但如果区域交迭的话则结果不可预知。
注: 因为调色板图像限制(255+1 种颜色)有个问题。重采样或过滤图像通常需要多于 255 种颜色,计算新的被重采样的像素及其颜色时采用了一种近似值。对调色板图像尝试分配一个新颜色时,如果失败我们选择了计算结果最接近(理论上)的颜色。这并不总是视觉上最接近的颜色。这可能会产生怪异的结果,例如空白(或者视觉上是空白)的图像。要跳过这个问题,请使用真彩色图像作为目标图像,例如用 imagecreatetruecolor() 创建的。
注: imagecopyresampled() 需要 GD 2.0.l 或更高版本。
一个简单的示例:
<div class="codetitle"><a style="CURSOR: pointer" data="43527" class="copybut" id="copybut43527" onclick="doCopy('code43527')"> 代码如下:
<div class="codebody" id="code43527">
<?PHP
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width,$height) = getimagesize($filename);
$new_width = $width $percent;
$new_height = $height
$percent;
// Resample
$image_p = imagecreatetruecolor($new_width,$new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p,$image,$new_width,$new_height,$height);
// Output
imagejpeg($image_p,null,100);
?>

示例2:
<div class="codetitle"><a style="CURSOR: pointer" data="21501" class="copybut" id="copybut21501" onclick="doCopy('code21501')"> 代码如下:
<div class="codebody" id="code21501">
<?PHP
// The file
$filename = 'test.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
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;
}
// Resample
$image_p = imagecreatetruecolor($width,$height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p,$height,$width_orig,$height_orig);
// Output
imagejpeg($image_p,100);
?>

有两种改变图像大小的方法
ImagecopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。
ImagecopyResamples(),其像素插值算法得到的图像边缘比较平滑。(但该函数的速度比 ImagecopyResized() 慢)。
两个函数的参数是一样的,如下:
<div class="codetitle"><a style="CURSOR: pointer" data="61022" class="copybut" id="copybut61022" onclick="doCopy('code61022')"> 代码如下:
<div class="codebody" id="code61022">
imagecopyResampled(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);
imagecopyResized(dest,sh);

例子:
<div class="codetitle"><a style="CURSOR: pointer" data="42459" class="copybut" id="copybut42459" onclick="doCopy('code42459')"> 代码如下:<div class="codebody" id="code42459">
<?PHP
$src = ImageCreateFromJPEG('PHP.jpg');
$width = ImageSx($src);
$height = ImageSy($src);
$x = $widht/2;
$y = $height/2;
$dst = ImageCreateTrueColor($x,$y);
ImagecopyResampled($dst,$src,$x,$y,$widht,$height);
header('Content-Type : image/png');
ImagePNG($det);
?>

PHP中如何改变jpg图像文件的尺寸大小

<div class="codetitle"><a style="CURSOR: pointer" data="24706" class="copybut" id="copybut24706" onclick="doCopy('code24706')"> 代码如下:<div class="codebody" id="code24706">
<?
function resize_jpg($img,$w,$h){
// $thumb = imagecreate ($w,$h);
$image = imagecreatefromjpeg($img);
$imagedata = getimagesize($img);
if ($h = "auto") $h = $w/($imagedata[0]/$imagedata[1]);//根据原图的纵横比得出高度!
$thumb = imagecreatetruecolor ($w,$h);
imagecopyresized ($thumb,$h,$imagedata[0],$imagedata[1]);
imagejpeg($thumb);
}
//resize_jpg($file,$h);
resize_jpg("images/dsc01244.jpg",100,100);
imagedestory($thumb);
imagedestory($image);
?>

函数代码
<div class="codetitle"><a style="CURSOR: pointer" data="51409" class="copybut" id="copybut51409" onclick="doCopy('code51409')"> 代码如下:<div class="codebody" id="code51409">
<?PHP
/

图片缩略图
$srcfile 来源图片
$rate 缩放比,认为缩小一半,或者具体宽度象素值
$filename 输出图片文件名jpg
例如: resizeimage("zt32.gif",0.1);
例如: resizeimage("zt32.gif",250 );
说明:调用时直接把函数的结果放在HTML文件img标签中的SRC属性
/
function resizeimage($srcfile,$rate=.5,$filename = "" ){
$size=getimagesize($srcfile);
switch($size[2]){
case 1:
$img=imagecreatefromgif($srcfile);
break;
case 2:
$img=imagecreatefromjpeg($srcfile);
break;
case 3:
$img=imagecreatefrompng($srcfile);
break;
default:
exit;
}
//源图片的宽度和高度
$srcw=imagesx($img);
$srch=imagesy($img);
//目的图片的宽度和高度
if($size[0] <= $rate || $size[1] <= $rate){
$dstw=$srcw;
$dsth=$srch;
}else{
if($rate <= 1){
$dstw=floor($srcw$rate);
$dsth=floor($srch
$rate);
}else {
$dstw=$rate;
$rate = $rate/$srcw;
$dsth=floor($srch*$rate);
}
}
//echo "$dstw,$dsth,$srcw,$srch ";
//新建一个彩色图像
$im=imagecreatetruecolor($dstw,$dsth);
$black=imagecolorallocate($im,255,255);
imagefilledrectangle($im,$dstw,$black);
imagecopyresized($im,$img,$srch);
// 以 JPEG 格式将图像输出到浏览器或文件
if( $filename ) {
//图片保存输出
imagejpeg($im,$filename );
}else {
//图片输出到浏览器
imagejpeg($im);
}
//释放图片
imagedestroy($im);
imagedestroy($img);
}
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

图片图片图片尺寸大小

相关推荐