0

I'm trying to create a system which allows users to upload an image, this image is then saved to an /images folder along with a resized thumbnail version.

As an example, a user would visit the upload page, select their file and hit the upload button. The image is then saved and a function is then called to resize the image and save the resized image as th-filename.ext along with filename.ext.

At the moment, I've managed to get the file uploads to work although I can't get the resizing and saving to work. I can't seem to locate the issue as resizing and saving images using PHP is newish to me although I imagine it's nothing to do with the SimpleImage.php. Any help would be greatly appreciated as I'm not getting anywhere :S.

My upload page form:

<form action="assets/includes/upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" id="file"><br><input type="submit" name="submit" value="Submit" class="btn btn-primary"></form>

My uploadImage function (reduced down):

function imageUpload() {
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("../../images/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      $file = $_FILES["file"]["name"];
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../../images/" . $_FILES["file"]["name"]);
          include('SimpleImage.php');
          $image = new SimpleImage();
          $image->load($file);
          $image->resizeToWidth(150);
          $image->save("th-" . $file);
      }
    }
  }
else
  {
  echo "Invalid file";
  }

  header("Location: ../../album.php"); 
  die(""); 
}

As well as the SimpleImage.php:

    <?
class SimpleImage {

   var $image;
   var $image_type;

   function load($filename) {

      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {

         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {

         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {

         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image,$filename);
      }
      if( $permissions != null) {

         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image);
      }
   }
   function getWidth() {

      return imagesx($this->image);
   }
   function getHeight() {

      return imagesy($this->image);
   }
   function resizeToHeight($height) {

      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }

   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }

   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }

   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }      

}
?>
4

1 回答 1

0

尝试使用单个脚本上传、调整大小和保存相同的图像会带来各种问题,尤其是图像文件已经或仍在由应用程序处理时。因此,您至少需要将函数分开,并确保一个函数在让脚本的另一部分再次打开它之前释放了有问题的文件,您需要创建一个时间间隔。

现在时间间隔只有在文件大小最小且不消耗服务器资源的情况下才合适。因此,要确保有足够的时间,最好的方法是实际分离功能并将它们放在单独的页面上。

这些页面可以通过使用脚本重定向链接自动加载下一个页面,或者您可以通过在页面上显示带有一个按钮供用户单击提交的预览来使其看起来有意义。

于 2013-03-16T18:21:54.360 回答