imagesize()函数获取图片信息

imagesize()

imagesize()函数:用于获取图像大小及相关信息,成功返回一个数组,失败则返回 FALSE 并产生一条 E_WARNING 级的错误信息。

For example:

    $filename="20151126.jpg";
    print_r(getimagesize($filename));

output:

Array ( 
[0] => 562 
[1] => 351 
[2] => 2 
[3] => width="562" height="351" 
[bits] => 8 
[channels] => 3 
[mime] => image/jpeg 
) 

1:索引 0 给出的是图像宽度的像素值
2:索引 1 给出的是图像高度的像素值
3:索引 2 给出的是图像的类型,返回的是数字,其中1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
4:索引 3 给出的是一个宽度和高度的字符串,可以直接用于 HTML 的 标签
5:索引 bits 给出的是图像的每种颜色的位数,二进制格式
6:索引 channels 给出的是图像的通道值,RGB 图像默认是 3
7:索引 mime 给出的是图像的 MIME 信息,此信息可以用来在 HTTP Content-type 头信息中发送正确的信息,如:
header(“Content-type: image/jpeg”);

imagecreatetruecolor(x,y)

用imagecreatetruecolor(int x,int y)建立的是一幅大小为 x和 y的黑色图像(默认为黑色),如想改变背景颜色则需要用填充颜色函数imagefill( img,0,0, color);

imagecreate()

imagecreate()新建一个空白图像资源,用imagecolorAllocate()添加背景色

<?php
$img = imagecreatetruecolor(100,100);    //创建真彩图像资源
$color = imagecolorAllocate($img,200,200,200);   //分配一个灰色
imagefill($img,0,0,$color);                 // 从左上角开始填充灰色
header('content-type:image/jpeg');   //jpg格式
imagejpeg($img);                              //显示灰色的方块
?>

<?php
$img = imagecreate(100,100);
$color = imagecolorallocate($img,200,200,200);
header('content-type:image/jpeg');   //jpg格式
imagejpeg($img);                              //显示灰色的方块
?>
<?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);
?>
// 文件路径
$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)
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值