0

嗨,我基于我在网络上找到的那个函数,并尝试为我的 PHP 页面修改它以上传他们的图标,但我想限制用户上传一张只有 100x100 像素大小的图像。好吧,我只是用这个来称呼它:

uploadImage($id,$_FILES['upload']['name'],$_FILES['upload']['tmp_name']);

这是我做的功能:

 function uploadImage($new_name,$imagename,$tmp_name){

 if($tmp_name!=null||$tmp_name!=""){
     list($width, $height, $type, $attr) = getimagesize($tmp_name);
         if($width==100&&$height==100){
          $image1 = $imagename;

          $extension = substr($image1, strrpos($image1, '.') + 1);
          $image = "$new_name.$extension";
          $folder = "Images/"; 
                  if($image) { 
                    $filename = $folder.$image; 

                    $copied =  copy($tmp_name, $filename); 
                  }
                  else echo "image not uploaded.";
          }
          else
            echo "upload only 100x100px image!"; 
    }

}

现在的问题是,即使我上传了超过 100 x 100px 尺寸的图像,它仍然会继续进行而不会返回任何错误,现在我迷失了。

4

3 回答 3

3

您可能需要稍微重构您的代码;具有检查上传的图像是否有效的功能,然后实际进行上传。或者,您可以创建一个类。

<?php

class ImageUpload
{    
    public $tmpImage;
    public $maxWidth = 100;
    public $maxHeight = 100;
    public $errors = [];

    public function __construct($image)
    {
        $this->tmpImage = $image;
    }

    public function upload()
    {
        // Check image is valid; if not throw exception

        // Check image is within desired dimensions
        list($width, $height) = getimagesize($this->tmpImage);

        if ($width > $this->maxWidth || $height > $this->maxHeight) {
            throw new Exception(sprintf('Your image exceeded the maximum dimensions (%d&times;%d)', $this->maxWidth, $this->maxHeight));
        }

        // Create filename
        // Do the upload logic, i.e. move_uploaded_file()
    }
}

然后,您可以按如下方式使用此类:

<?php

$imageUpload = new ImageUpload($_FILES['upload']['tmp_name']);

try {
    $imageUpload->upload();
} catch (Exception $e) {
    echo 'An error occurred: ' . $e->getMessage();
}

这是即兴写的,所以它们可能是错误的。但希望它展示了一种更好的方式来处理文件上传,以及上传过程中可能出现的错误。

于 2012-07-23T10:55:04.720 回答
2

好吧,您也可以在上传后调整图像大小。

function createFixSizeImage( $pathToImages, $pathToFixSizeImages, $Width ) 
{

  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {


  $image_info   = getimagesize( "path/to/images/".$fname );
  $image_width  = $image_info[0];
  $image_height = $image_info[1];
  $image_type   = $image_info[2];


  switch ( $image_type )
  {

    case IMAGETYPE_JPEG:


    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpeg' ) 
    {

      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );

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

      // give the size,u want
      $new_width = 100;
      $new_height = 100;

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image 
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save Fix Size Images into a file

      imagejpeg( $tmp_img, "{$pathToFixSizeImages}{$fname}" );

    }
      break;



     case IMAGETYPE_PNG:
         // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'png' ) 
    {

      // load image and get image size
      $img = imagecreatefrompng( "{$pathToImages}{$fname}" );

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


      $new_width = 100;
      $new_height = 100;

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image 
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save Fix Size Images into a file

      imagejpeg( $tmp_img, "{$pathToFixSizeImages}{$fname}" );

    }
      break;

    case IMAGETYPE_BMP:
      echo "bmp";
      break;



    default:
      break;
  }
}
  }
  // close the directory
  closedir( $dir );
}

createFixSizeImage("path","path/to/images/to/be/saved",100);
于 2012-07-23T11:05:16.157 回答
1

扩展或多或少未知的代码然后对其进行调试,就像您在几周前编写了一些代码并且您不再理解它一样。

在您的情况下,您通过添加检查图像大小的功能来扩展一些现有代码(您没有发布原始代码,但您写道您是这样做的)。

因此,您不需要编辑很多(未知但)工作代码,创建新功能作为它自己的功能:

/**
 * @param string $file
 * @param int $with
 * @param int $height
 * @return bool|null true/false if image has that exact size, null on error.
 */
function image_has_size($file, $width, $height)
{
    $result = getimagesize($file);
    if ($count($result) < 2) {
        return null;
    }

    list($file_width, $file_height) = $result;

    return ($file_width == (int) $width) 
           && ($file_height == (int) $height);
}

您现在在一个函数中拥有了新功能,您可以更轻松地集成到原始(希望其他工作)代码中。

用法:

$imageHasCorrectSize = image_has_size($tmp_name, 100, 100);

So whenever you change code, do it like a surgeon, keeping the cuts as small as possible.

于 2012-07-23T11:12:48.530 回答