【问题标题】:BLOB - PHP re-size ImageBLOB - PHP 调整图像大小
【发布时间】:2011-08-30 13:58:28
【问题描述】:

我在调整 blob 图像大小时遇到​​了一个小问题。 我发现我必须做 BLOB 的高度和宽度,但是因为人们上传的图片不是方形的,我该如何正确调整它们的大小?

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

我当前的代码是

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

【问题讨论】:

    标签: php image resize blob


    【解决方案1】:

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

    <?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) 不是需要文件路径而不是文件内容的方式?
    猜你喜欢
    • 2011-11-25
    • 2012-01-09
    • 1970-01-01
    • 2011-03-31
    • 2011-02-18
    • 2012-09-07
    • 2013-01-16
    • 2015-09-12
    • 2010-09-20
    相关资源
    最近更新 更多