<?php /** * *函數:調整圖片尺寸或生成縮略圖 *修改:2013-2-15 *返回:True/False *參數: * $Image 需要調整的圖片(含路徑) * $Dw=450 調整時最大寬度;縮略圖時的絕對寬度 * $Dh=450 調整時最大高度;縮略圖時的絕對高度 * $Type=1 1,調整尺寸; 2,生成縮略圖 */ $phtypes=array('img/gif', 'img/jpg', 'img/jpeg', 'img/bmp', 'img/pjpeg', 'img/x-png'); function compressImg($Image,$Dw,$Dh,$Type){ echo $Image; IF(!file_exists($Image)){ echo "不存在圖片"; return false; } echo "存在圖片"; // 如果需要生成縮略圖,則將原圖拷貝一下重新給$Image賦值(生成縮略圖操作) // 當Type==1的時候,將不拷貝原圖像文件,而是在原來的圖像文件上重新生成縮小后的圖像(調整尺寸操作) 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; case 2: $Img =@imagecreatefromjpeg($Image); Break; case 3: $Img =@imagecreatefrompng($Image); break; } // 如果對象沒有創建成功,則說明非圖片文件 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,0,$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; } }; ?> <?php //網絡圖片路徑 $imgPath = 'http://www.lesohome.com/phone/compress-img/251139474ba926db3d7850.jpg'; //$imgPath = "http://www.lesohome.com/userfiles/image/20111125/251139474ba926db3d7850.jpg"; $tempPath = str_replace('http://www.lesohome.com/', '', $imgPath);//替換換行字符 $name = strrchr($tempPath, "/"); $path = str_replace($name, '', $tempPath);//替換換行字符 /** *根據路徑path建立多級目錄 *$dir目標目錄 $mode權限,0700表示最高權限 */ function makedir( $dir , $mode = "0700" ) { if(strpos($dir , "/" )){ $dir_path = "" ; $dir_info = explode("/" , $dir ); foreach($dir_info as $key => $value){ $dir_path .= $value ; if (!file_exists($dir_path)){ @mkdir($dir_path, $mode) or die ("建立文件夾時失敗了" ); @chmod($dir_path, $mode); } else { $dir_path .= "/" ; continue ; } $dir_path .= "/" ; } return $dir_path ; } else { @mkdir($dir, $mode) or die( "建立失敗了,請檢查權限" ); @chmod($dir, $mode); return $dir ; } } //end makedir makedir($path); /** *根據url獲取服務器上的圖片 *$url服務器上圖片路徑 $filename文件名 */ function GrabImage($url,$filename="") { if($url=="") return false; if($filename=="") { $ext=strrchr($url,"."); if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false; $filename=date("YmdHis").$ext; } ob_start(); readfile($url); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); $fp2=@fopen($filename, "a"); fwrite($fp2,$img); fclose($fp2); return $filename; } ?> <html> <body> 允許上傳的文件類型為:<?=implode(', ',$phtypes)?><br/> <input type="button" id="getImg" name="getImg" size="10" value="獲取圖片並保存" onclick="alert('12333');" /> <?php echo $path."<br>"; /**/ $bigImg=GrabImage($imgPath, $tempPath); if($bigImg){ echo '<img src="'.$bigImg.'"><br>'; } else { echo "false"; } compressImg($bigImg,100,80,1); ?> </body> </html>