保存到MySQL数据库之前调整图像大小

时间:2013-05-15 07:15:15

标签: php mysql database image-resizing image-upload

我的问题类似于this one,但我仍然坚持最后一点!

以下代码采用HTML表单发送的文件,并根据两个预定义值$maxwidth$maxheight检查其维度。如果它太宽,它会调整图像大小。

我希望接下来要做的就是将新调整大小的图像重新分配给原始变量$tmpName,以便脚本的其余部分可以处理它。

以下代码返回这些错误:

Warning: fopen() expects parameter 1 to be string, resource given in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 43

Warning: filesize() [function.filesize]: stat failed for Resource id #4 in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 44

Warning: fread(): supplied argument is not a valid stream resource in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 44

Warning: fclose(): supplied argument is not a valid stream resource in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 46

我认为我很接近它,但任何人都可以帮忙吗?感谢。

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  

            list($width,$height)=getimagesize($tmpName);

            if ($width>$height && $width>$maxwidth) {
                $newheight=($height/$width)*$maxwidth;
                $newwidth=$maxwidth;
                $imageResized = imagecreatetruecolor($newwidth, $newheight);
                $imageTmp     = imagecreatefromjpeg ($tmpName);
                imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                $tmpName=$imageResized;

                // My problem lies somewhere here ^^^^
            }

      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);


      // Create the query and insert
      // into our database.
      $query = "INSERT INTO tblPrints ";
      $query .= "(title,full_image) VALUES ('IMG-TEST','$data')";
      $results = mysql_query($query, $link);

      // Print results
      print "Thank you, your file has been uploaded.";

}
else {
   print "No image selected/uploaded";
}

1 个答案:

答案 0 :(得分:0)

第6-17行用图像数据本身替换文件名 - 这就是

的含义
Warning: fopen() expects parameter 1 to be string, resource given

所以在接下来的几行中,

  // Read the file 
  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);

您不需要阅读该文件。如果条件流程已执行,则只需

$data = $tmpname // sounds strange, remember: $tmpname is not the name any more