2012-08-10 32 views
0

我有一個腳本,可以完美調整.JPG圖片的大小,它完全按照它應該做的。 但是,我想爲用戶提供添加.PNG en .GIF圖像的選項。有人可以幫助我擴展下面的腳本,所以它也支持這些格式?提前致謝!展開圖片調整大小腳本爲.PNG和.GIF

$image = $_POST['bedrijfslogo']; 
$new_image = "../subdomains/{$gemeente7}/httpdocs/{$plaatsnaam7}/{$bedrijfsnaam7}/bedrijfslogo.jpg"; 
copy($_POST['bedrijfslogo'],"copylogo.jpg"); 
$width=250; //*** Fix Width & Heigh (Autu caculate) ***// 
$size=GetimageSize($image); 
$height=round($width*$size[1]/$size[0]); 
$images_orig = ImageCreateFromJPEG($image); 
$photoX = ImagesX($images_orig); 
$photoY = ImagesY($images_orig); 
$images_fin = ImageCreateTrueColor($width, $height); 
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY); 
ImageJPEG($images_fin, $new_image); 
ImageDestroy($images_orig); 
ImageDestroy($images_fin); 
unlink($image); 
+0

使用['finfo_file()'](http://php.net/manual/en/function.finfo-file.php)獲取MIME類型上傳文件的,'切換「並根據MIME類型適當地使用'imagecreatefrom *()'爲'$ images_orig'賦值。或者,如果你感到懶惰,你可以安全地(但不太可靠)只使用文件擴展名來確定使用哪個函數。除此之外,腳本的其餘部分可以保持不變。 – DaveRandom 2012-08-10 12:09:11

+0

感謝您的提示,但是這個腳本不是我的。我在互聯網上找到它並可以編輯它以適應我的需求。我不是PHP專業人員,所以只需要多一點幫助:我該如何處理腳本最後一位的ImageJPEG? – user1555076 2012-08-10 12:11:45

回答

0

取決於OUTPUTFILE你想要的類型,添加和選擇:

imagecreatefrompng 

imagecreatefromgif 

imagepng 

imagegif 
0

繼我的評論後,我意識到你可以從getimagesize()返回的數據中提取圖像的類型。

試試這個,它應該接受任何類型的圖像,你的PHP實例能夠理解(未經測試):

// Get file paths 
// Taking a local file path direct from the user? This is a *big* security risk... 
$srcPath = $_POST['bedrijfslogo']; 
$dstPath = "../subdomains/{$gemeente7}/httpdocs/{$plaatsnaam7}/{$bedrijfsnaam7}/bedrijfslogo.jpg"; 

// Not really sure why this is here, you don't seem to use the copy of the file 
copy($srcPath, "copylogo.jpg"); 

// Get information about source image 
$info = getimagesize($srcPath); 
$srcWidth = $info[0]; 
$srcHeight = $info[1]; 
$srcType = $info[2]; 

// Calculate new width and height 
// Width is fixed, height calculated from width 
$dstWidth = 250; 
$dstHeight = round($dstWidth * $srcHeight/$srcWidth); 

// Get the image types supported by this instance of PHP 
$supportedTypes = imagetypes(); 

// Create the source image resource based on it's type 
switch ($srcType) { 
    case IMG_GIF: 
    if ($supportedTypes & IMG_GIF) { 
     $srcImage = imagecreatefromgif($srcPath); 
    } else { 
     // GIF support not enabled on your PHP instance. Handle error here. 
     exit('GIF support not enabled on this PHP instance'); 
    } 
    break; 
    case IMG_JPG: case IMG_JPEG: // Think these might have the same value? 
    if ($supportedTypes & IMG_JPG) { 
     $srcImage = imagecreatefromjpeg($srcPath); 
    } else { 
     // JPEG support not enabled on your PHP instance. Handle error here. 
     exit('JPEG support not enabled on this PHP instance'); 
    } 
    break; 
    case IMG_PNG: 
    if ($supportedTypes & IMG_PNG) { 
     $srcImage = imagecreatefrompng($srcPath); 
    } else { 
     // PNG support not enabled on your PHP instance. Handle error here. 
     exit('PNG support not enabled on this PHP instance'); 
    } 
    break; 
    case IMG_WBMP: 
    if ($supportedTypes & IMG_WBMP) { 
     $srcImage = imagecreatefromwbmp($srcPath); 
    } else { 
     // WBMP support not enabled on your PHP instance. Handle error here. 
     exit('WBMP support not enabled on this PHP instance'); 
    } 
    break; 
    case IMG_XPM: 
    if ($supportedTypes & IMG_XPM) { 
     $srcImage = imagecreatefromxpm($srcPath); 
    } else { 
     // XPM support not enabled on your PHP instance. Handle error here. 
     exit('XPM support not enabled on this PHP instance'); 
    } 
    break; 
    default: 
    // Unknown input file type. Handle error here. Currently prints some debugging info 
    echo 'Unknown input file type'; 
    var_dump($info); 
    exit; 
} 

// Create the destination image resource 
$dstImage = imagecreatetruecolor($dstWidth, $dstHeight); 

// Resample source image onto destination image 
imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $dstWidth + 1, $dstHeight + 1, $srcWidth, $srcHeight); 

// Save new image to disk 
imagejpeg($dstImage, $dstPath); 

// Destroy resources 
imagedestroy($srcImage); 
imagedestroy($dstImage); 
$srcImage = $dstImage = NULL; 

// Remove source image - are you sure you want to do this? 
unlink($srcPath); 

現在,這個代碼似乎被設計成一個本地源圖像工作,但是您使用它的方式表明您可能會從用戶那裏獲取URL並下載遠程圖像。如果是這種情況,請告訴我,我會稍微修改以達到更好的目的。

+0

這應該像一個魅力,今天會測試它。非常感謝DaveRandom。用戶可以將照片添加到其公司配置文件中,如果圖片符合要求,則會將其上傳到臨時文件夾。我們(adminds)必須在處理之前批准他們的配置文件,因此在我們的批准頁面中,我有幾個輸入字段以'temp-folder-filepaths'作爲值。所以我正在使用本地文件,而不是直接來自用戶。十分感謝! – user1555076 2012-08-12 11:39:15

+0

不幸的是,它並不像我們希望的那樣工作。當我使用JPG時,我沒有收到錯誤信息,但是它會刪除src文件,但不會在指定的文件夾中創建新文件。如果我上傳一個。PNG它只是表示默認的「未知輸入文件類型」。當我用舊腳本做這兩件事情時,它確實爲.JPG工作,但如果我使用.PNG工作,我只會得到一個黑色圖像。謝謝! – user1555076 2012-08-12 13:24:29

+0

@ user1555076',但不會在指定的文件夾中創建新文件。「 - 這是我的錯誤,現在已在上面的代碼中進行了更正。 '如果我上傳.PNG,它只是表示默認的「未知輸入文件類型」 - 我已經編輯了上面的代碼,所以它會顯示一些更有用的調試信息 - 你可以再試一次並粘貼你得到的輸出嗎? – DaveRandom 2012-08-12 13:45:36