2013-01-12 28 views
0

可能重複:
How to convert all images to JPG format in PHP?外匯牌價用戶圖像,以JPG

我想要的用戶名進入文件名,並有文件,.jpg結尾所以輸出將成爲「user-mycoolnewimage.jpg」。關於如何做到這一點的任何建議都改爲「randnumber-mycoolnewimage.png」,這不是我想要的。隨機數對服務器日誌沒有幫助,並且圖像需要以jpg格式結束,因爲它正在被轉換。

$file = rand(0, 10000000).$_FILES['t1']['name']; 
if (move_uploaded_file($_FILES['t1']['tmp_name'], $file)) { 
    if($fp = fopen($file,"rb", 0)) { 
     $picture = fread($fp,filesize($file)); 
     fclose($fp); 
     $image = imagecreatefrompng($file); 
     imagejpeg($image, $file, 70); 
     imagedestroy($image); 
     $tag1 = '<img src="'.$file.'" alt="" class="default" />'; 
     //unlink($file);   
    } 
} 

回答

1

我想你要找的是basename(記錄here

一個你可以做的事情就是返修有點腳本有,你創建兩個不同的$file變量的東西,一個拿着最初的png和另一個持有jpeg路徑。您還需要unlink您的png完成後:

$source = $username . "-" . $_FILES['t1']['name']; 
$destination = $username . "-" . basename($_FILES['t1']['name']) . ".jpg"; 
if (move_uploaded_file($_FILES['t1']['tmp_name'], $source)) { 
    if($fp = fopen($source,"rb", 0)) { 
     $picture = fread($fp,filesize($source)); 
     fclose($fp); 
     $image = imagecreatefrompng($source); 
     imagejpeg($image, $destination, 70); 
     imagedestroy($image); 
     $tag1 = '<img src="'.$file.'" alt="" class="default" />'; 
     unlink($source);   
    } 
}