2009-08-07 100 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()