下面提供三款生成縮代碼,兼容性都相當的不錯,也可以自定高度與寬度哦。
function imageresize($srcfile,$tow,$toh,$tofile="")
{
if($tofile==""){ $tofile = $srcfile; }
$info = "";
$data = getimagesize($srcfile,$info);
switch ($data[2])
{
case 1:
if(!function_exists("imagecreatefromgif")){
echo "你的gd庫不能使用gif格式的圖片,請使用jpeg或png格式!<a href='網頁特效:go(-1);'>返回</a>";
exit();
}
$im = imagecreatefromgif($srcfile);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的gd庫不能使用jpeg格式的圖片,請使用其它格式的圖片!<a href='javascript:go(-1);'>返回</a>";
exit();
}
$im = imagecreatefromjpeg($srcfile);
break;
case 3:
$im = imagecreatefrompng($srcfile);
break;
}
$srcw=imagesx($im);
$srch=imagesy($im);
$towh=$tow/$toh;
$srcwh=$srcw/$srch;
if($towh<=$srcwh){
$ftow=$tow;
$ftoh=$ftow*($srch/$srcw);
}
else{
$ftoh=$toh;
$ftow=$ftoh*($srcw/$srch);
}
if($srcw>$tow||$srch>$toh)
{
if(function_exists("imagecreatetruecolor")){
@$ni = imagecreatetruecolor($ftow,$ftoh);
if($ni) imagecopyresampled($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
else{
$ni=imagecreate($ftow,$ftoh);
imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
}
}else{
$ni=imagecreate($ftow,$ftoh);
imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
}
if(function_exists('imagejpeg')) imagejpeg($ni,$tofile);
else imagepng($ni,$tofile);
imagedestroy($ni);
}
imagedestroy($im);
}
實例代碼二
<?php教程
/*構造函數-生成縮略圖+水印,參數說明:
$srcfile-圖片文件名,
$dstfile-另存文件名,
$markwords-水印文字,
$markimage-水印圖片,
$dstw-圖片保存寬度,
$dsth-圖片保存高度,
$rate-圖片保存品質*/
makethumb("a.jpg","b.jpg","50","50");
function makethumb($srcfile,$dstfile,$dstw,$dsth,$rate=100,$markwords=null,$markimage=null)
{
$data = getimagesize($srcfile);
switch($data[2])
{
case 1:
$im=@imagecreatefromgif($srcfile);
break;
case 2:
$im=@imagecreatefromjpeg($srcfile);
break;
case 3:
$im=@imagecreatefrompng($srcfile);
break;
}
if(!$im) return false;
$srcw=imagesx($im);
$srch=imagesy($im);
$dstx=0;
$dsty=0;
if ($srcw*$dsth>$srch*$dstw)
{
$fdsth = round($srch*$dstw/$srcw);
$dsty = floor(($dsth-$fdsth)/2);
$fdstw = $dstw;
}
else
{
$fdstw = round($srcw*$dsth/$srch);
$dstx = floor(($dstw-$fdstw)/2);
$fdsth = $dsth;
}
$ni=imagecreatetruecolor($dstw,$dsth);
$dstx=($dstx<0)?0:$dstx;
$dsty=($dstx<0)?0:$dsty;
$dstx=($dstx>($dstw/2))?floor($dstw/2):$dstx;
$dsty=($dsty>($dsth/2))?floor($dsth/s):$dsty;
$white = imagecolorallocate($ni,255,255,255);
$black = imagecolorallocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstw,$dsth,$white);// 填充背景色
imagecopyresized($ni,$im,$dstx,$dsty,0,0,$fdstw,$fdsth,$srcw,$srch);
if($markwords!=null)
{
$markwords=iconv("gb2312","utf-8",$markwords);
//轉換文字編碼
imagettftext($ni,20,30,450,560,$black,"simhei.ttf",$markwords); //寫入文字水印
//參數依次為,文字大小|偏轉度|橫坐標|縱坐標|文字顏色|文字類型|文字內容
}
elseif($markimage!=null)
{
$wimage_data = getimagesize($markimage);
switch($wimage_data[2])
{
case 1:
$wimage=@imagecreatefromgif($markimage);
break;
case 2:
$wimage=@imagecreatefromjpeg($markimage);
break;
case 3:
$wimage=@imagecreatefrompng($markimage);
break;
}
imagecopy($ni,$wimage,500,560,0,0,88,31); //寫入圖片水印,水印圖片大小默認為88*31
imagedestroy($wimage);
}
imagejpeg($ni,$dstfile,$rate);
imagejpeg($ni,$srcfile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
?>
支持圖片上傳代碼
<?php
$pic_name=date("dmyhis");
// 生成圖片的寬度
$pic_width=$_post['width'];// 生成圖片的高度
$pic_height=$_post['length'];function resizeimage($im,$maxwidth,$maxheight,$name){
//取得當前圖片大小
$width = imagesx($im);
$height = imagesy($im);
//生成縮略圖的大小
if(($width > $maxwidth) || ($height > $maxheight)){
$widthratio = $maxwidth/$width;
$heightratio = $maxheight/$height;
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
imagejpeg ($newim,$name . ".jpg");
imagedestroy ($newim);
}else{
imagejpeg ($im,$name . ".jpg");
}
}if($_files['image']['size']){
//echo $_files['image']['type'];
if($_files['image']['type'] == "image/pjpeg"||$_files['image']['type'] == "image/jpg"||$_files['image']['type'] == "image/jpeg"){
$im = imagecreatefromjpeg($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_files['image']['tmp_name']);
}
if($im){
if(file_exists($pic_name.'.jpg')){
unlink($pic_name.'.jpg');
}
resizeimage($im,$pic_width,$pic_height,$pic_name);
imagedestroy ($im);
}
}
?><img src="<? echo $pic_name.'.jpg'; ?>"><br><br>
<form enctype="multipart/form-data" method="post" action="small_picture.php">
<br>
<input type="file" name="image" size="50" value="浏覽"><p>
生成縮略圖寬度:<input type="text" name="width" size="5"><p>
生成縮略圖長度:<input type="text" name="length" size="5"><p>
<input type="submit" value="上傳圖片">
</form>