Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Skalowanie zdjęć i tworzenie minatur
unw
post 3.03.2021, 19:01:29
Post #1





Grupa: Zarejestrowani
Postów: 44
Pomógł: 0
Dołączył: 12.06.2008

Ostrzeżenie: (0%)
-----


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





Grupa: Zarejestrowani
Postów: 531
Pomógł: 55
Dołączył: 3.01.2016
Skąd: Łowicz

Ostrzeżenie: (0%)
-----


ja używam tego

https://www.php.net/manual/en/imagick.getim...orientation.php


--------------------
Szukam zleceń Symfony, Laravel, Back-End, Front-End, PHP, MySQL ...
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.03.2024 - 16:54