-
-
/**
- * desription 壓縮圖片
- * @param sting $imgsrc 圖片路徑
- * @param string $imgdst 壓縮後儲存路徑
- */
- function image_png_size_add($imgsrc,$imgdst){ $new_width = ($width>600?600:$width)*0.9;
- $new_height =($height>600?600:$height)*0.9;
- switch($type){
- 情況 1:
- $giftype=check_gifcartoon($imgsrc);
- if($giftype){
- header('Content-Type:image/gif');
- $image_wp=imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefromgif($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- imagejpeg($image_wp, $imgdst,75);
- imagedestroy($image_wp);
- }
- 休息;
- 情況2:
- header('Content-Type:image/jpeg');
- $image_wp=imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefromjpeg($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- imagejpeg($image_wp, $imgdst,75);
- imagedestroy($image_wp);
- 休息;
- 情況3:
- header('Content-Type:image/png');
- $image_wp=imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefrompng($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- imagejpeg($image_wp, $imgdst,75);
- imagedestroy($image_wp);
- 休息;
- } // bbs.it-home.org
- }
- /**
- * desription 判斷是否gif動畫
- * @param sting $image_file圖片路徑
- * @return boolean t 是 f 否
- */
- function check_gifcartoon($image_file){
- $fp = fopenp = fopen ($image_file,'rb');
- $image_head = fread($fp,1024);
- fclose($fp);
- return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true;
- }
- ?>
-
複製程式碼 例2:
-
-
/*
- 函數:調整圖片尺寸或產生縮圖
- 回傳:True/False
- 參數:
- $Image 需要調整的圖片(含路徑)
- $Dw=450 調整時最大寬度;縮圖時的絕對寬度
- $Dh=450 調整時最大高度;
- $Type=1 1,調整尺寸; 2,產生縮圖
- $path='img/';//路徑
- $phtypes=array(
- 'img/gif',
- 'img/jpg',
- 'img/jpeg',
- 'img/bmp',
- 'img/pjpeg',
- 'img/x-png'
- );
- Function Img($Image,$Dw=450,$Dh=450,$Type=1){
- IF(!File_Exists($Image)){
- Return False;
- }
- }
- //若需要產生縮圖,則將原圖拷貝一下重新給$Image賦值
- IF($Type!=1){
- Copy($Image,Str_Replace(".","_x.", $Image));
- $Image=Str_Replace(".","_x.",$Image);
- }
- //取得檔案的型別,依照不同的型別建立不同的物件
- $ImgInfo=GetImageSize($Image);
- Switch($ImgInfo[2]){
- Case 1:
- $Img = @ImageCreateFromGIF($Image);
- Break; 2:
- $Img = @ImageCreateFromJPEG($Image);
- Break;
- Case 3:
- $Img = @ImageCreateFromPNG($Image);
- }
- }
- }
- }
- }
- }
- }
- }
- } //如果物件沒有建立成功,則說明非圖片檔案
- IF(Empty($Img)){
- //如果是產生縮圖的時候出錯,則需要刪除已經複製的檔案
- IF($Type!=1){Unlink($Image);}
- Return False;
- }
- //如果是執行調整尺寸操作則
- IF($Type==1) {
- $w=ImagesX($Img);
- $h=ImagesY($Img);
- $width = $w;
- $height = $h;
- IF($width >$Dw){
- $Par=$Dw/$width;
- $width=$Dw;
- $height=$height*$Par;
- IF($height>$Dh){
- $Par=$Dh/$height;
- $height=$Dh;
- $width=$width*$Par;
- }
- }ElseIF($height>$Dh){
- $Par=$Dh/$height;
- $height=$Dh;
- $width=$width*$Par;
- IF($width>$Dw){
- $Par =$Dw/$width;
- $width=$Dw;
- $height=$height*$Par;
- }
- }Else{
- $width=$width;
- $height=$height;
- }
- $nImg = ImageCreateTrueColor($width,$height); //新建一個真彩色畫布
- ImageCopyReSampled($nImg,$Img,0,0,0,00 ,$width,$height,$w,$h);//重採樣拷貝部分影像並調整大小
- ImageJpeg ($nImg,$Image); //以JPEG格式將影像輸出到瀏覽器或檔案
- Return True;
- //如果是執行生成縮圖操作則
- }Else{
- $w=ImagesX($Img);
- $h=ImagesY($Img);
- $width = $w;
- $height = $h;
- $nImg = ImageCreateTrueColor($Dw,$Dh);
- IF($h/$w>$Dh/$Dw){ / /高比較大
- $width=$Dw;
- $height=$h*$Dw/$w;
- $IntNH=$height-$Dh;
- ImageCopyReSampled($nImg, $Img , 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
- }Else{ //寬比較大
- $height=$Dh;
- $ width=$w*$Dh/$h;
- $IntNW=$width-$Dw;
- ImageCopyReSampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $ Dh, $w, $h);
- }
- ImageJpeg ($nImg,$Image);
- Return True;
- }
- }
- ?>
-
-
if($_SERVER['REQUEST_METHOD']=='POST'){
- if (!is_uploaded_file($_FILES["photo"][tmp_name])) {
- echo "圖片不存在";
- exit();
- }
- if(!is_dir('img')){//路徑若不存在則創建
- mkdir(' img');
- }
- $upfile=$_FILES["photo"];
- $pinfo=pathinfo($upfile["name"]);
- $name=$pinfo['basename '];//檔案名稱
- $tmp_name=$upfile["tmp_name"];
- $file_type=$pinfo['extension'];//取得檔案類型
- $showphpath=$path.$ name;
-
- if(in_array($upfile["type"],$phtypes)){
- echo "檔案類型不符! ";
- exit();
- }
- if(move_uploaded_file($tmp_name,$path.$name)){
- echo "成功! ";
- Img($showphpath,100,800,2);
} echo " "; }?>
|