0

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?

Mico Abrina
  • 507
  • 1
  • 7
  • 25

1 Answers1

2

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" />
Mike B
  • 31,886
  • 13
  • 87
  • 111
  • Is there a way to do it without saving another image since it would consume more space? – Mico Abrina Feb 19 '12 at 06:11
  • @MicoAbrina If not the disk then the only other place is memory in some sort of memcache. However, if you're concerned about disk-space then I imagine memory is out as well. Disk space is the cheapest hardware resource right now.. is it really an issue? – Mike B Feb 19 '12 at 06:14
  • I'm using this for profile pictures for my website. The problem with this is that if the profile picture changes, how will the system know to generate another thumbnail? – Mico Abrina Feb 19 '12 at 06:21
  • @MicoAbrina That's why I strongly recommended generating the thumbnails upon creation. When someone uploads a new photo you create and overwrite any existing thumbnail. You kill two birds with one stone. – Mike B Feb 19 '12 at 06:24
  • OK, but what if lets say, I did that, put out my website to the public and realized that I needed to generate more thumbnails. How would I generate the new thumbnails for the current users? – Mico Abrina Feb 19 '12 at 06:29
  • Sorry for asking so much questions. – Mico Abrina Feb 19 '12 at 06:30
  • @MicoAbrina They're good questions :) - In that case you can have the thumbnail generation occur in both places: 1) When the user uploads a new photo, generate the thumbnail. 2) When you're showing the thumbnail (like in your example). That way if all the user's thumbnails gets wiped from the disk they'll be recreated on demand. Alternatively, you could have a script that loops over the users and creates thumbnails. – Mike B Feb 19 '12 at 06:35
  • Awesome! It worked out well! I simply created a class that generates thumbnails for me. Thanks a lot! – Mico Abrina Feb 19 '12 at 07:15
  • I'm having a bit of a problem with your suggestion. Please check http://stackoverflow.com/questions/9347698/how-do-you-make-thumbnails-on-upload-with-phpthumb – Mico Abrina Feb 19 '12 at 08:13