BLOB - PHP 调整图片大小

3

我在重新调整二进制大对象图像的大小时遇到了一些小问题。我发现不得不对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个回答

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

如果是BLOB类型的情况,getimagesize($img_path)方法就不适用了,因为它需要一个文件路径而不是文件内容。 - Tiny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接