Drukowana wersja tematu

Kliknij tu, aby zobaczyć temat w orginalnym formacie

Forum PHP.pl _ Przedszkole _ [PHP] Skalowanie zdjęć i tworzenie minatur

Napisany przez: unw 3.03.2021, 19:01:29

Witam.

Posiadam funkcje odpowiedzialne za wgranie i resize obrazka w locie.
Problem zaczyna się, gdy chcę skalować różnej szerokości obrazki.
Tutaj mam wklepany rozmiar dla zdjęć pionowych. Jak zrobić, aby w jakiś sposób wykrywać czy zdjęcie jest poziome i pionowe i wtedy dopasować konkretną proporcję rozmiaru?

Wszystko fajnie działa, gdy obrazek jest pionowy. Przy poziomym zaczynają się schody.
Jak to rozwiązać?

Poniżej fragment kodu

functions.php:

  1. <?php
  2. function createFolder($path)
  3. {
  4. if (!http://www.php.net/file_exists($path)) {
  5. http://www.php.net/mkdir($path, 0755, TRUE);
  6. }
  7. }
  8.  
  9. function createThumbnail($sourcePath, $targetPath, $file_type, $thumbWidth, $thumbHeight){
  10.  
  11. $source = imagecreatefromjpeg($sourcePath);
  12.  
  13. $width = imagesx($source);
  14. $height = imagesy($source);
  15.  
  16. $tnumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
  17.  
  18. imagecopyresampled($tnumbImage, $source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
  19.  
  20. if (imagejpeg($tnumbImage, $targetPath, 90)) {
  21. imagedestroy($tnumbImage);
  22. imagedestroy($source);
  23. return TRUE;
  24. } else {
  25. return FALSE;
  26. }
  27. }
  28. ?>


image_upload_submit.php:


  1. <?php
  2. require_once('functions.php');
  3.  
  4. if(http://www.php.net/isset($_FILES['image_file_input']))
  5. {
  6. $output['status']=FALSE;
  7. http://www.php.net/set_time_limit(0);
  8. $allowedImageType = http://www.php.net/array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png" );
  9.  
  10. if ($_FILES['image_file_input']["error"] > 0) {
  11. $output['error']= "File Error";
  12. }
  13. elseif (!http://www.php.net/in_array($_FILES['image_file_input']["type"], $allowedImageType)) {
  14. $output['error']= "Invalid image format";
  15. }
  16. elseif (http://www.php.net/round($_FILES['image_file_input']["size"] / 1024) > 4096) {
  17. $output['error']= "Maximum file upload size is exceeded";
  18. } else {
  19. $temp_path = $_FILES['image_file_input']['tmp_name'];
  20. $file = http://www.php.net/pathinfo($_FILES['image_file_input']['name']);
  21. $fileType = $file["extension"];
  22. $fileName = http://www.php.net/rand(222, 888) . http://www.php.net/time() . ".$fileType";
  23.  
  24. $small_thumbnail_path = "uploads/small/";
  25. createFolder($small_thumbnail_path);
  26. $small_thumbnail = $small_thumbnail_path . $fileName;
  27.  
  28. $medium_thumbnail_path = "uploads/medium/";
  29. createFolder($medium_thumbnail_path);
  30. $medium_thumbnail = $medium_thumbnail_path . $fileName;
  31.  
  32. $large_thumbnail_path = "uploads/large/";
  33. createFolder($large_thumbnail_path);
  34. $large_thumbnail = $large_thumbnail_path . $fileName;
  35.  
  36. $thumb1 = createThumbnail ($temp_path, $small_thumbnail,$fileType, 160, 193);
  37. $thumb2 = createThumbnail($temp_path, $medium_thumbnail, $fileType, 360, 547);
  38. $thumb3 = createThumbnail($temp_path, $large_thumbnail,$fileType, 460, 647);
  39.  
  40. if($thumb1 && $thumb2 && $thumb3) {
  41. $output['status']=TRUE;
  42. $output['small']= $small_thumbnail;
  43. $output['medium']= $medium_thumbnail;
  44. $output['large']= $large_thumbnail;
  45. }
  46. }
  47. http://www.php.net/echo json_encode($output);
  48. }
  49. ?>

Napisany przez: LowiczakPL 3.03.2021, 20:45:15

ja używam tego

https://www.php.net/manual/en/imagick.getimageorientation.php

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)