上传时调整图像大小

时间:2015-12-15 11:14:55

标签: php image resize

我想知道我要做什么。如果我想在将数据插入数据库时​​调整图像大小...我知道不要将图像保存到数据库中更好..但我必须这样做才能在我的小服务器上节省空间..目前我使用此代码保存图像:

if (isset($_FILES['immagine']) && $_FILES['immagine']['size'] > 0)
{
  $imageName = $_FILES["immagine"]["name"];
  $imageData = file_get_contents($_FILES["immagine"]["tmp_name"]);
  $imageType = $_FILES["immagine"]["type"];
  if(substr($imageType,0,5)=="image")
     {
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

         $stmt = $dbh->prepare("UPDATE `".$_SESSION['id']."`  SET immagine = ?, type = ?, profilo = 1 WHERE profilo = 1");
         $stmt->bindParam(1,$imageData,PDO::PARAM_LOB);
         $stmt->bindParam(2,$imageType,PDO::PARAM_STR);
         $stmt->execute();
     }
}

1 个答案:

答案 0 :(得分:1)

你应该尝试使用GD。

// Loads the image
$img = imagecreatefromjpeg($_FILES["immagine"]["tmp_name"]); // Assuming it's a jpeg

// Creates a image to put the thumbnail
$tmp_img = imagecreatetruecolor(<thumbnail width>, <thumbnail height>)

// Resizes the image
imagecopyresampled ( $tmp_img, $img, 0, 0, 0, 0, <thumbnail width>, <thumbnail height>, <original width>, <original height>); // The zeroes are the offsets

// Starts output buffer to not let the image reach the browser
ob_start();

// Render the image
imagejpeg($tmp_image); 

// Capture the image data
$imageData = ob_get_clean();