图片缩放函数
**
<?php
$img="a.jpg";
function thumb($img,$dstx,$dsty,$pre){
// 得到图片信息
$arr=getimagesize($img);
echo '<pre>';
print_r($arr);
echo '</pre>';
//原图资源
$srcimg=imagecreatefromjpeg($img);
//目标资源
$dstimg=imagecreatetruecolor($dstx,$dsty);
//图片缩放
imagecopyresampled($dstimg,$srcimg,0,0,0,0,$dstx,$dsty,$srcx,$srcy);
}
thumb($img,200,200,'t_');
?>
结果是
Array
(
[0] => 500
[1] => 300
[2] => 2
[3] => width="500" height="300"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
图片缩放函数1
<?php
$img="a.jpg";
function thumb($img,$dstx,$dsty,$pre){
// 得到图片信息
$arr=getimagesize($img);
$srcx=$arr[0];
$srcy=$arr[1];
$srctype=$arr[2];
//原图资源
$srcimg=imagecreatefromjpeg($img);
//目标资源
$dstimg=imagecreatetruecolor($dstx,$dsty);
//图片缩放
imagecopyresampled($dstimg,$srcimg,0,0,0,0,$dstx,$dsty,$srcx,$srcy);
//保存路径
$dstfile="t_".$img;
//保存图片
imagejpeg($dstimg,$dstfile);
//关闭资源
imagedestroy($srcimg);
imagedestroy($dstimg);
}
thumb($img,200,200,'t_');
?>
图片缩放函数5
<?php
$img="a.jpg";
function thumb($img,$dstx,$dsty,$pre){
// 得到图片信息
$arr=getimagesize($img);
$srcx=$arr[0];
$srcy=$arr[1];
$srctype=$arr[2];
//图片类型
//变量函数
switch($srctype){
case 1:
$imgfrom='imagecreatefromgif';
$imgout='imagegif';
break;
case 2:
$imgfrom='imagecreatefromjpeg';
$imgout='imagejpeg';
break;
case 3:
$imgfrom='imagecreatefrompng';
$imgout='imagepng';
break;
}
//等比例算法
// 取最大值
$scale=max($srcx/$dstx,$srcy/$dsty);
// floor — 舍去法取整
$dstx=floor($srcx/$scale);
$dsty=floor($srcy/$scale);
//原图资源
$srcimg=imagecreatefromjpeg($img);
//目标资源
$dstimg=imagecreatetruecolor($dstx,$dsty);
//图片缩放
imagecopyresampled($dstimg,$srcimg,0,0,0,0,$dstx,$dsty,$srcx,$srcy);
//保存路径
$dir=dirname($img);
$file=basename($img);
$dstfile=$dir."/".$pre.$file;
//保存图片
imagejpeg($dstimg,$dstfile);
//关闭资源
imagedestroy($srcimg);
imagedestroy($dstimg);
}
thumb($img,1000,1000,'t_1000_');
thumb($img,500,500,'t_500_');
thumb($img,200,200,'t_200_');
thumb($img,100,100,'t_100_');
?>
**
**