【问题标题】:how to re-size the image on the fly [closed]如何动态调整图像大小[关闭]
【发布时间】:2011-04-05 21:43:30
【问题描述】:

我计划在我的站点中生成 RSS 提要...为了在 RSS 提要中显示图像,我从源系统(不同于服务器)获取它们。这只是为了减少我服务器的负载和带宽使用。

由于图像尺寸相当大,需要在运行时/飞行中调整图像大小。

请指导如何实现这一点

【问题讨论】:

  • 不要这样做。将图像获取到您的服务器,在那里调整它们的大小,并将它们存储为静态资源。调整大小非常昂贵
  • 他可以在上传时随时调整大小。

标签: php javascript image optimization image-processing


【解决方案1】:
  • 这是我用了一段时间的功能,它旨在自动保持调整后图像的约束比例

用法

imageResize('old_image.jpg', 200, 'new_image.jpg');

function imageResize($image, $thumb_width, $new_filename)
{
  $max_width = $thumb_width;
  //Check if GD extension is loaded
  if (!extension_loaded('gd') && !extension_loaded('gd2')) {
    trigger_error("GD is not loaded", E_USER_WARNING);
    return false;
  }
  //Get Image size info
  list($width_orig, $height_orig, $image_type) = getimagesize($image);
  switch ($image_type) {
    case 1:
      $im = imagecreatefromgif($image);
      break;
    case 2:
      $im = imagecreatefromjpeg($image);
      break;
    case 3:
      $im = imagecreatefrompng($image);
      break;
    default:
      trigger_error('Unsupported filetype!', E_USER_WARNING);
      break;
  }
  //calculate the aspect ratio
  $aspect_ratio = (float) $height_orig / $width_orig;
  //calulate the thumbnail width based on the height
  $thumb_height = round($thumb_width * $aspect_ratio);
  while ($thumb_height > $max_width) {
    $thumb_width -= 10;
    $thumb_height = round($thumb_width * $aspect_ratio);
  }
  $new_image = imagecreatetruecolor($thumb_width, $thumb_height);
  //Check if this image is PNG or GIF, then set if Transparent
  if (($image_type == 1) OR ($image_type == 3)) {
    imagealphablending($new_image, false);
    imagesavealpha($new_image, true);
    $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
    imagefilledrectangle($new_image, 0, 0, $thumb_width, $thumb_height, $transparent);
  }
  imagecopyresampled($new_image, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);
  //Generate the file, and rename it to $new_filename
  switch ($image_type) {
    case 1:
      imagegif($new_image, $new_filename);
      break;
    case 2:
      imagejpeg($new_image, $new_filename);
      break;
    case 3:
      imagepng($new_image, $new_filename);
      break;
    default:
      trigger_error('Failed resize image!', E_USER_WARNING);
      break;
  }
  return $new_filename;
}

【讨论】:

    【解决方案2】:

    答案很简单:

    不要动态调整图像大小。永远不要那样做。

    与著名的回声与打印或单引号与双引号问题不同,调整图像大小会对系统性能造成真实严重损害。因此,您最终会遇到 RSS 提要出现故障并停止服务器

    【讨论】:

      【解决方案3】:

      请注意,您不应即时制作带有缩略图的 RSS 提要。而是将生成的提要(带有图像)保存在 .rss 文件中并提供。

      现在,当您添加新项目时,您会更新您的 .rss 文件。

      【讨论】:

        【解决方案4】:

        我用这个:

        <Directory {DOCUMENT_ROOT}/tn/>
         RewriteEngine on
         RewriteCond %{REQUEST_FILENAME} !-f
         RewriteRule \.png$ /tn/create.php [PT,NE,L]
        </Directory>
        

        在 nginx 后面工作得很好。

        在编写“create.php”之前,请务必确保您了解 PHP。创建一个巨大的安全漏洞很容易。

        注意:仅当您有大量无法轻松静态生成的图像时,此解决方案才值得使用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-04
          • 1970-01-01
          • 2013-04-26
          • 1970-01-01
          • 1970-01-01
          • 2012-07-05
          • 2015-11-21
          相关资源
          最近更新 更多