Вход Регистрация
* -=(C)DRU987=- (Dev)

Ресайз фото по большей стороне

  1. function resize($newWidth, $targetFile, $originalFile) {
  2.  
  3. $info = getimagesize($originalFile);
  4. $mime = $info['mime'];
  5.  
  6. switch ($mime) {
  7. case 'image/jpeg':
  8. $image_create_func = 'imagecreatefromjpeg';
  9. $image_save_func = 'imagejpeg';
  10. $new_image_ext = 'jpg';
  11. break;
  12.  
  13. case 'image/png':
  14. $image_create_func = 'imagecreatefrompng';
  15. $image_save_func = 'imagepng';
  16. $new_image_ext = 'png';
  17. break;
  18.  
  19. case 'image/gif':
  20. $image_create_func = 'imagecreatefromgif';
  21. $image_save_func = 'imagegif';
  22. $new_image_ext = 'gif';
  23. break;
  24.  
  25. default:
  26. throw Exception('Unknown image type.');
  27. }
  28.  
  29. $img = $image_create_func($originalFile);
  30. list($width, $height) = getimagesize($originalFile);
  31.  
  32. if ((!$width) || (!$height)) { $GLOBALS['errors'][] = 'Image couldn\'t be resized because it wasn\'t a valid image.'; return false; }
  33.  
  34. if (($width <= $newWidth) && ($height <= $newWidth)) { return $image; } //no resizing needed
  35.  
  36. //try max width first...
  37. $ratio = $newWidth / $width;
  38. $new_w = $newWidth;
  39. $new_h = $height * $ratio;
  40.  
  41. //if that didn't work
  42. if ($new_h > $newWidth) {
  43. $ratio = $newWidth / $height;
  44. $new_h = $newWidth;
  45. $new_w = $width * $ratio;
  46. }
  47.  
  48. $tmp = imagecreatetruecolor($new_w, $new_h);
  49. imagecopyresampled($tmp, $img, 0, 0, 0, 0, $new_w, $new_h, $width, $height);
  50.  
  51. if (file_exists($targetFile)) {
  52. unlink($targetFile);
  53. }
  54. $image_save_func($tmp, "$targetFile.$new_image_ext");
  55. }
» Описание: Всё легко и просто. Использовать resize(800, $img, $img);
» Время добавления: 8 Марта 2016 в 12:51
» Посмотров: 1830
» textarea
» Рейтинг: [+0 | -0]
Комментарии [0]
Онлайн: 2
Реклама