开发者

php resize blob image

开发者 https://www.devze.com 2023-04-10 15:00 出处:网络
开发者_运维百科I am echoing the data received from a blob column from mysql like this: <?php
开发者_运维百科

I am echoing the data received from a blob column from mysql like this:

<?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');
  }

?>

QUESTION: How can i reduce my image to say like 500px X 500px


It is really the bad idea to store images in DB, because they are too big, harder to maintain, harder to work with and so on. You should store only path to it or file name.

To resize image you may use PHP's GD library. Create it using imagecreatefromstring() and manipulate using imagecopyresized() or 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号