0

我目前使用 phpthumb 为个人资料图片生成缩略图。http://phpthumb.gxdlabs.com/

这是我目前的方法:

<?php
$path_to_profile_pic = '../icons/default.png';
$profile_pic = file_get_contents('icons/default.png');
$small_profile_pic = PhpThumbFactory::create($profile_pic, array(), true);
$small_profile_pic->adaptiveResize(25, 25);
$small_profile_picAsString = $small_profile_pic->getImageAsString();
?>
<img src="data:image/png;base64,<?php echo base64_encode($small_profile_picAsString); ?>" width="25" height="25" />

但是,由于 base64,这非常慢,因为它会在您的代码中生成大量文本。使用 phpthumb 生成缩略图的最佳方法是什么?

编辑

有没有办法在不保存另一个图像的情况下做到这一点,因为它会占用更多空间?

4

1 回答 1

2

您最好总是在上传/创建时生成图像的缩略图版本,而不是尝试通过 php 脚本按需提供它们。您可以使用您的一些代码使用 php 将生成的文件缓存到文件系统,但这仍然比使用 apache 提供它们要慢得多。

如果您无法在上传/创建时创建缩略图,您可以通过将缩小的缩略图缓存到文件系统并为每个后续请求提供它们来优化您当前的实现:

<?php
$path_to_thumb_pic = '../icons/default_thumb.png';
if (!file_exists($path_to_thumb_pic)) {
  $path_to_profile_pic = '../icons/default.png';
  $profile_pic = file_get_contents('icons/default.png');
  $small_profile_pic = PhpThumbFactory::create($profile_pic, array(), true);
  $small_profile_pic->adaptiveResize(25, 25);
  $small_profile_pic->save($path_to_thumb_pic);
}
?>

<img src="<?php echo $path_to_thumb_pic; ?>" width="25" height="25" />
于 2012-02-19T03:02:14.783 回答