I currently use phpthumb to generate thumbnails for profile pictures. http://phpthumb.gxdlabs.com/

This is my current method:

<?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" />

However, this is very slow due to base64 because it generates a large number of text in your code. What is the best way to use phpthumb to generate thumbnails?

Edit

Is there a way to do it without saving another image since it would consume more space?

有帮助吗?

解决方案

You're better off always generating thumbnail versions of images on upload/creation instead of trying to serve them through php scripts on-demand. You can cache generated files to the filesystem with php using some of your code but that's still much slower than serving them up with apache.

If you're unable to create thumbnails on upload/create you can optimize your current implementation by caching the scaled down thumbnail to the filesystem and serving them up for every subsequent request:

<?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" />
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top