生成图像缩略图使用,而不会耗尽内存


Generating image thumbnails using without running out of memory

我目前正在使用php gd实现来调整不断耗尽内存的图像-相当快。我猜问题是php函数,像imagecreatefromstring等。

是否有一个简单的实现来调整不使用此函数的图像大小,所以我不必增加我的php.ini内存限制?

这是一个PHP函数

 function make_thumb($src, $dest, $desired_width,$desired_h) {
  /* read the source image */
  $source_image = imagecreatefromjpeg($src);
  $width = imagesx($source_image);
  $height = imagesy($source_image);
  $desired_height = $desired_h;
  /* create a new, "virtual" image */
  $virtual_image = imagecreatetruecolor($desired_width, $desired_height);
  /* copy source image at a resized size */
  imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
  /* create the physical thumbnail image to its destination */
  imagejpeg($virtual_image, $dest);
}

来源:davidwalsh.name/create-image-thumbnail-php

GD没有使用那么多内存,所以您的代码中有其他问题。

如果您调整多个图像的大小,并且不在新创建的图像上调用imagedestroy,则会导致内存泄漏。