BLOB - PHP重新调整大小的图像

时间:2011-05-22 12:25:14

标签: php image resize blob

我有一个小问题,重新调整blob图像的大小。 我发现的是我必须做BLOB的高度和宽度,但由于人们上传的图像不是正方形,我该如何正确地重新调整它们的尺寸?

基本上我想要最大宽度为300px;

我目前的代码是

 $desired_width = 300;
 $desired_height = 300;

 $sth = mysql_query("SELECT photobase FROM userpictures WHERE id = '".$array[0]."'");

 while($r = mysql_fetch_assoc($sth)) {
      $blobcontents = $r["photobase"];

      $im = imagecreatefromstring($blobcontents);
      $new = imagecreatetruecolor($desired_width, $desired_height);

      $x = imagesx($im);
      $y = imagesy($im);

      imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);

      imagedestroy($im);

      header('Content-type: <span class="posthilit">image</span>/jpeg');

      imagejpeg($new, null, 85);

1 个答案:

答案 0 :(得分:3)

一种调整图像大小的简单方法,保持约束比例:

<?php
// Constraints
$max_width = 100;
$max_height = 100;
list($width, $height) = getimagesize($img_path);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
// New dimensions
$width = intval($ratio*$width);
$height = intval($ratio*$height);
?>