php调整blob图像

时间:2011-10-01 20:00:21

标签: php mysql blob

我正在回复从mysql的blob列收到的数据,如下所示:

<?php
$con = mysql_connect("localhost","username","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("mydb", $con);
  if(isset($_GET['imageid'])){
      $img=$_GET['imageid'];
      $sql="SELECT image FROM vrzgallery WHERE id=$img";
      $que=mysql_query($sql);
      $ar=mysql_fetch_assoc($que);
      echo $ar['image'];
      header('Content-type: image/jpeg');
  }

?>

问题:如何将图像缩小为500px X 500px

1 个答案:

答案 0 :(得分:-1)

将图像存储在数据库中真是个坏主意,因为它们太大,难以维护,难以使用等等。您应该只存储它的路径或文件名。

要调整图片大小,您可以使用PHP的GD库。使用imagecreatefromstring()创建并使用imagecopyresized()imagecopyresampled()进行操作。 Example from manual

// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);