【问题标题】:Resize the images after uploading!上传后调整图片大小!
【发布时间】:2011-05-28 03:34:01
【问题描述】:

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

上传.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
  • /thumbs/$img

我怎样才能在没有原始图像的情况下只创建两张图像(调整大小的一张和缩略图)!

  • /调整大小/$img
  • /thumbs/$img

谢谢你,

【问题讨论】:

    标签: php html upload imagick


    【解决方案1】:

    您可以在创建调整大小和拇指文件后立即删除上传的文件

    unlink($imgDir .'/'. $img);
    

    紧接着

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

    【讨论】:

      【解决方案2】:

      取消原来的链接

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

      【讨论】:

        猜你喜欢
        • 2011-08-25
        • 1970-01-01
        • 1970-01-01
        • 2014-08-26
        • 1970-01-01
        • 2014-11-29
        • 2016-10-11
        相关资源
        最近更新 更多