2009-08-07 107 views
1

我一直在尋找move_upload_files功能,但我不認爲這就是我需要的。php上傳圖像,重命名,縮略圖,並保存兩個

我要上傳圖片(最大尺寸爲2MB PNG,JPG,GIF只),然後將其重命名,然後創建它的縮略圖,既保存在同一目錄中。我想我不會上傳/重命名主文件,然後採取並創建縮略圖。但是我應該注意什麼功能來做到這一點?

回答

5

你至少需要看PHP's GD functions,或更好的Imagick創建拇指。

有這個教程不計其數,這裏有一對夫婦:

或者你可以只用一個現成的解決方案,例如, :

+0

我希望我能找到這麼多越快。我沒有檢查其他兩個,但在http://www.webcheatsheet.com/php/create_thumbnail_images.php描述的方法行之有效我。 – reubenCanowski 2013-03-21 14:34:22

1

PHPThumb是你需要的...只需在api中搜索允許你保存圖像的方法。與上傳圖像..在這裏你有一個很好的tutorial about it

7
if(isset($_FILES['p1']) && $_FILES['p1']['tmp_name'] != ''){ 

    $sizes = array(); 
    $sizes['50'] = 50; 
    $sizes['150'] = 150; 
    $sizes['500'] = 500; 

    $prefix = time(); 
    list(,,$type) = getimagesize($_FILES['p1']['tmp_name']); 
    $type = image_type_to_extension($type); 

    move_uploaded_file($_FILES['p1']['tmp_name'], 'uploads/'.$prefix.$type); 

    $t = 'imagecreatefrom'.$type; 
    $t = str_replace('.','',$t); 
    $img = $t('uploads/'.$prefix.$type); 

    foreach($sizes as $k=>$v){ 

     $width = imagesx($img); 
     $height = imagesy($img); 

     $new_width = $v; 
     $new_height = floor($height * ($v/$width)); 

     $tmp_img = imagecreatetruecolor($new_width, $new_height); 
     imagealphablending($tmp_img, false); 
     imagesavealpha($tmp_img, true); 
     imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

     $c = 'image'.$type; 
     $c = str_replace('.','',$c); 
     $c($tmp_img, 'uploads/'.$k.'_'.$prefix.$type); 

    }// 


}// 

我使用它來上傳,重命名和創建3個不同的大拇指,希望這可以幫助別人了。可以通過以下方式獲取

0

@Ian威爾金森php.net

更好的質量imagecopyresampled()