2010-12-20 149 views
2

HTML表单上传后调整图像大小!

<form action="upload.php" method="POST" enctype="multipart/form-data"> 
<input type="file" name="file" /><p /><input type="submit" value="Uplaod" /> 
</form> 

PHP函数

function createResizedIMK($img, $imgPath, $thumbDir, $suffix, $by) { 
    // add in the suffix after the '.' dot. 
    $newNameE = explode(".", $img); 
    $newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .''; 

    // ImageMagicK doesnt like '/' and 'x' characters in the command line call. 
    // And workout the size based on '$by'. 
    $uploadedImg = ''. $imgPath .'/'. $img .''; 
    $newResized = ''. $reduceDir .'/'. $newName .''; 
    list($width, $height, $type, $attr) = getimagesize("$imgPath/$img"); 
    $newWidth = ($width/$by); 
    $newHeight = ($height/$by); 
    $newRes = ''. $newWidth .'x'. $newHeight .''; 

    // This makes a command line call to ImageMagicK. 
    // My path to ImageMagicK Convert is '/usr/lib/php/bin/convert' 
    // 'convert' is a program (UNIX) so no forward slash. 
    $cr = system("/usr/lib/php/bin/convert -resize $newRes $uploadedImg $newResized", $retval); 

    return $cr; 
} 

upload.php的

$imgDir ="uploads"; 
$resDir ="resized"; 
$thumbDir="thumbs"; 

$img  = $_FILES['file']['name']; 
$tmpPath = $_FILES['file']['tmp_name']; 

if (move_uploaded_file($tmpPath,"$imgDir/$img")) 
{ 
$resize = createResizedIMK($img, $imgDir, $resDir, "resized-", 2); 
$thumb = createThumbIMK($img, $imgDir, $thumbDir, "thumb-", 150, 150); 
} 

这将创建三个影像 “原来,一个大小和缩略图”,

  • /uploads/$ img
  • /调整/ $ IMG
  • /大拇指/ $ IMG

我怎样才能让这个只创建两个图像(一个大小和缩略图),与出原始图像!

  • /调整/ $ IMG
  • /大拇指/ $ IMG

谢谢你,

回答

1

您可以创建调整和拇指文件与

后立即删除上传的文件
unlink($imgDir .'/'. $img); 

right after

$thumb = createThumbIMK($img, $imgDir, $thumbDir, "thumb-", 150, 150); 
+0

谢谢你,它的工作 – Swell 2010-12-20 09:44:24

0

取消与原

if (move_uploaded_file($tmpPath,"$imgDir/$img")) 
{ 
$resize = createResizedIMK($img, $imgDir, $resDir, "resized-", 2); 
$thumb = createThumbIMK($img, $imgDir, $thumbDir, "thumb-", 150, 150); 
unlink($imgDir.'/'.$img); 
} 
+0

谢谢你,它的工作 – Swell 2010-12-20 09:43:52