photolib.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * GNU Social
  4. * Copyright (C) 2010, Free Software Foundation, Inc.
  5. *
  6. * PHP version 5
  7. *
  8. * LICENCE:
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Widget
  23. * @package GNU Social
  24. * @author Ian Denhardt <ian@zenhack.net>
  25. * @copyright 2010 Free Software Foundation, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  27. */
  28. function photo_make_thumbnail($filename)
  29. {
  30. $height_dest = 192;
  31. $width_dest = 256;
  32. $size_src = getimagesize(INSTALLDIR . '/file/' . $filename);
  33. $image_type = $size_src[2];
  34. switch($image_type) {
  35. case IMAGETYPE_JPEG:
  36. $image_src = imagecreatefromjpeg(INSTALLDIR . '/file/' . $filename);
  37. break;
  38. case IMAGETYPE_PNG:
  39. $image_src = imagecreatefrompng(INSTALLDIR . '/file/' . $filename);
  40. break;
  41. case IMAGETYPE_GIF:
  42. $image_src = imagecreatefromgif(INSTALLDIR . '/file/' . $filename);
  43. break;
  44. default:
  45. return false;
  46. }
  47. $width_src = $size_src[0];
  48. $height_src = $size_src[1];
  49. $ratio_src = (float) $width_src / (float) $height_src;
  50. $ratio_dest = (float) $width_dest / (float) $height_dest;
  51. if ($ratio_src > $ratio_dest) {
  52. $height_crop = $height_src;
  53. $width_crop = (int)($height_crop * $ratio_dest);
  54. $x_crop = ($width_src - $width_crop) / 2;
  55. } else {
  56. $width_crop = $width_src;
  57. $height_crop = (int)($width_crop / $ratio_dest);
  58. $x_crop = 0;
  59. }
  60. $image_dest = imagecreatetruecolor($width_dest, $height_dest);
  61. imagecopyresampled($image_dest, $image_src, 0, 0, $x_crop, 0, $width_dest, $height_dest, $width_crop, $height_crop);
  62. switch ($image_type) {
  63. case IMAGETYPE_JPEG:
  64. imagejpeg($image_dest, INSTALLDIR . '/file/' . 'thumb.' . $filename, 100);
  65. break;
  66. case IMAGETYPE_PNG:
  67. imagepng($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
  68. break;
  69. case IMAGETYPE_GIF:
  70. imagegif($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
  71. break;
  72. }
  73. imagedestroy($image_src);
  74. imagedestroy($image_dest);
  75. return true;
  76. }