上传后调整图片大小!

时间:2010-12-20 08:46:35

标签: php html upload imagick

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);
}

这将创建三个图像“原始,调整大小和缩略图”,

  • /上传/ $ IMG
  • /大小/ $ IMG
  • /拇指/ $ IMG

如何使用原始图像创建两个图像(调整大小和缩略图)!

  • /大小/ $ IMG
  • /拇指/ $ IMG

谢谢,

2 个答案:

答案 0 :(得分:1)

您可以在使用

创建调整大小和拇指文件后立即删除上传的文件
unlink($imgDir .'/'. $img);

之后

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

答案 1 :(得分: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);
}