images_utils.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * ownCloud - gallery application
  4. *
  5. * @author Bartek Przybylski
  6. * @copyright 2012 Bartek Przybylski bart.p.pl@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. OCP\JSON::checkLoggedIn();
  23. OCP\JSON::checkAppEnabled('gallery');
  24. function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height, $tgtImg, $shift) {
  25. //getting the image dimensions
  26. list($width_orig, $height_orig) = getimagesize($imgSrc);
  27. switch (strtolower(substr($imgSrc, strrpos($imgSrc, '.')+1))) {
  28. case "jpeg":
  29. case "jpg":
  30. case "tiff":
  31. $myImage = imagecreatefromjpeg($imgSrc);
  32. break;
  33. case "png":
  34. $myImage = imagecreatefrompng($imgSrc);
  35. break;
  36. default:
  37. exit();
  38. }
  39. $ratio_orig = $width_orig/$height_orig;
  40. if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
  41. $new_height = $thumbnail_width/$ratio_orig;
  42. $new_width = $thumbnail_width;
  43. } else {
  44. $new_width = $thumbnail_height*$ratio_orig;
  45. $new_height = $thumbnail_height;
  46. }
  47. $x_mid = $new_width/2; //horizontal middle
  48. $y_mid = $new_height/2; //vertical middle
  49. $process = imagecreatetruecolor(round($new_width), round($new_height));
  50. imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
  51. imagecopyresampled($tgtImg, $process, $shift, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
  52. imagedestroy($process);
  53. imagedestroy($myImage);
  54. }
  55. ?>