<?
//自動縮圖$srcfile原文件,大圖;$photo_small目標文件,小圖;$dstw,$dsth是小圖的寬,高。
function makethumb($srcfile,$photo_small,$dstw,$dsth) {
$data = getimagesize($srcfile);
switch ($data[2]) {
case 1: //圖片類型,1是gif圖
$im = @imagecreatefromgif($srcfile);
break;
case 2: //圖片類型,2是jpg圖
$im = @imagecreatefromjpeg($srcfile);
break;
case 3: //圖片類型,3是png圖
$im = @imagecreatefrompng($srcfile);
break;
}
$srcw=imagesx($im);//原始圖片的寬度,也可以使用$data[0]
$srch=imagesy($im);//原始圖片的高度,也可以使用$data[1]
$srcx=0;//來源圖的坐標x,y
$srcy=0;
if(($srcw/$dstw)>($srch/$dsth)){//得出要生成圖片的長寬
$dstw2=$dstw;//輸出圖片的寬度、高度
$dsth2=$srch*$dstw/$srcw;
$dstx=0;//輸出圖形的坐標x,y
$dsty=($dsth-$dsth2)/2;
}
else{
$dsth2=$dsth;//輸出圖片的寬度、高度
$dstw2=$srcw*$dstw/$srch;
$dstx=($dstw-$dstw2)/2;//輸出圖形的坐標x,y
$dsty=0;
}
$ni=imagecreatetruecolor($dstw,$dsth);//imagecreate($dstw,$dsth);畫出空白花布的大小
$colorbody=imagecolorallocate($ni,235,234,233);//定義背景顏色
imagefill($ni,0,0,$colorbody);//填充背景顏色
imagecopyresized($ni,$im,$dstx,$dsty,$srcx,$srcy,$dstw2,$dsth2,$srcw,$srch);
imagejpeg($ni,$photo_small);
//imagejpeg($ni); //在顯示圖片時用,把注釋取消,可以直接在頁面顯示出圖片。
}
//生成帶有版權信息的圖片
function makecopyright($srcfile,$dstfile,$dstw,$dsth){
$data = getimagesize($srcfile);
switch ($data[2]) {
case 1: //圖片類型,1是gif圖
$srcimg = @imagecreatefromgif($srcfile);
break;
case 2: //圖片類型,2是jpg圖
$srcimg = @imagecreatefromjpeg($srcfile);
break;
case 3: //圖片類型,3是png圖
$srcimg = @imagecreatefrompng($srcfile);
break;
}
$srcw=imagesx($srcimg);//原始圖片的寬度,也可以使用$data[0]
$srch=imagesy($srcimg);//原始圖片的高度,也可以使用$data[1]
if(($srcw/$dstw)>($srch/$dsth)){//得出要生成圖片的長寬
$dstw2=$dstw;//輸出圖片的寬度、高度
$dsth2=$srch*$dstw/$srcw;
}
else{
$dsth2=$dsth;//輸出圖片的寬度、高度
$dstw2=$srcw*$dstw/$srch;
}
$dstimg=imagecreatetruecolor($dstw2,$dsth2);//畫出空白花布的大小
imagecopyresized($dstimg,$srcimg,0,0,0,0,$dstw2,$dsth2,$srcw,$srch);
//定義要寫入的文字
$word="http://www.survivalescaperooms.com";//文字
$font=5;//字體
$wordcolor=imagecolorallocate($dstimg,0x08,0x00,0x00);//顏色
$wordx=$dstw2-200;//x坐標
$wordy=$dsth2-20;//y坐標
imagestring($dstimg,$font,$wordx,$wordy,$word,$wordcolor);//寫入文字
imagejpeg($dstimg,$dstfile);
//imagejpeg($ni); //在顯示圖片時用,把注釋取消,可以直接在頁面顯示出圖片。
}
//調用
$srcfile=$pimg_name;//取得文件擴展名:
$type=substr(strrchr($pimg_name,"."),1);
$photo_id=date("ymdhis");
$dst_small=$photo_id."_s.".$type; //小圖
$dst_big=$photo_id."_b.".$type; //大圖
makethumb($pimg,"../cpzs/".$dst_small,120,90);//120,90為小圖寬,高
makecopyright($pimg,"../cpzs/".$dst_big,600,480);
//if(copy($pimg,"../cpzs/".$dst_big))
// echo "文件上傳成功<br>";
//else
// echo "文件上傳失敗<br>";
?>
新聞熱點
疑難解答