Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] imagecreatefromjpeg() problem ze ścieżką dostępu do pliku.
Forum PHP.pl > Forum > Przedszkole
AdamT
Witajcie,

Próbuję napisać prymitywny skrypcik, który po wgraniu pliku graficznego na serwer zmieni jego rozmiar i zapisze miniaturę w katalogu podrzędnym o nazwie thumb.

Problem jest tego typu, że skrypt (resize.php) jest zapisany w innym katalogu (admin) a wgrane pliki graficzne w innym (files/products), kiedy plik resize.php i plik graficzny (np. example.jpg) są w tym samym katalogu nie ma problemu, natomiast kiedy podaję ścieżkę relatywną (../file/products/example.jpg) do pliku graficznego dostaję komunikat:

Error Invalid Image Type

Czy jest jakaś rada jak to "ugryść" Czy plik resize.php musi być w katalogu z obrazkami?



Kod resize.php - http://scripts.ringsworld.com/image-handli...resize.php.html

  1. <?php
  2.  
  3. /*
  4.   Version 1.0 Created by: Ryan Stemkoski
  5.   Questions or comments: ryan <at> ipowerplant <dot> com
  6.   Visit us on the web at: http://www.ipowerplant.com
  7.   Purpose: This script can be used to resize one or more images. It will save the file to a directory and output the path to that directory which you
  8.   can display or write to a databse.
  9.  
  10.   TO USE, SET:
  11.   $filename = image to be resized
  12.   $newfilename = added to filename to for each use to keep from overwriting images created example thumbnail_$filename is how it will be saved.
  13.   $path = where the image should be stored and accessed.
  14.   $newwidth = resized width could be larger or smaller
  15.   $newheight = resized height could be larger or smaller
  16.  
  17.   SAMPLE OF FUNCTION: makeimage('image.jpg', 'fullimage_', 'imgs/', 250, 250)
  18.  
  19.   Include the file containing the function in your document and simply call the function with the correct parameters and your image will be resized.
  20.  
  21. */
  22.  
  23. //IMAGE RESIZE FUNCTION FOLLOW ABOVE DIRECTIONS
  24. function makeimage($filename, $newfilename, $path, $newwidth, $newheight) {
  25.  
  26. //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
  27. $image_type = strstr($filename, '.');
  28.  
  29. //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
  30. switch($image_type) {
  31. case '.jpg':
  32. $source = imagecreatefromjpeg($filename);
  33. break;
  34. case '.png':
  35. $source = imagecreatefrompng($filename);
  36. break;
  37. case '.gif':
  38. $source = imagecreatefromgif($filename);
  39. break;
  40. default:
  41. echo("Error Invalid Image Type");
  42. die;
  43. break;
  44. }
  45.  
  46. //CREATES THE NAME OF THE SAVED FILE
  47. $file = $newfilename . $filename;
  48.  
  49. //CREATES THE PATH TO THE SAVED FILE
  50. $fullpath = $path . $file;
  51.  
  52. //FINDS SIZE OF THE OLD FILE
  53. list($width, $height) = getimagesize($filename);
  54.  
  55. //CREATES IMAGE WITH NEW SIZES
  56. $thumb = imagecreatetruecolor($newwidth, $newheight);
  57.  
  58. //RESIZES OLD IMAGE TO NEW SIZES
  59. imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  60.  
  61. //SAVES IMAGE AND SETS QUALITY || NUMERICAL VALUE = QUALITY ON SCALE OF 1-100
  62. imagejpeg($thumb, $fullpath, 60);
  63.  
  64. //CREATING FILENAME TO WRITE TO DATABSE
  65. $filepath = $fullpath;
  66.  
  67. //RETURNS FULL FILEPATH OF IMAGE ENDS FUNCTION
  68. return $filepath;
  69.  
  70. }
  71.  
  72. ?>


Kod sprawdzający test.php

  1. <?php
  2.  
  3. include ('resize.php');
  4.  
  5. $image = 'example.jpg';
  6. $rootpath = '../files/products/';
  7.  
  8. echo $rootpath.$image."<br><br><br>";
  9.  
  10. if (file_exists($rootpath.$image)) {
  11. echo "Plik " .$image. " istnieje<br><br>";
  12. echo "Próbuję utworzyć miniaturę ".$image."<br><br>";
  13.  
  14. makeimage($rootpath.$image, '', '../files/products/thumb/', 67, 67);
  15.  
  16. echo $filepath;
  17.  
  18. } else {
  19. echo "The file " .$image. " does not exist";
  20. }
  21. ?>
Szeszek1992
W jakim katalogu znajduje się plik test.php?
AdamT
test.php tez w katalogu admin. Wszystkie pliki php znajdują si w katalogu: admin natomiast wszystkie pliki graficzne znajdują się w katalogu: file/products/

  1. /public_html
  2. |
  3. --- admin/
  4. |
  5. --- test.php
  6. --- resize.php
  7. --- files/
  8. |
  9. --- products/
  10. |
  11. --- example.jpg
  12. |
  13. --- thumb/
  14. |
  15. --- example.jpg
zordon
zastosuj funkcję explode() zamiast strstr().
Jeśli się nie mylę podajesz plik jako zlepek path i nazwy pliku, więc w przypadku relatywnej ścieżki '../files/products/example.jpg' $image_type = strstr($filename, '.'); zwraca ci najpewniej '../files/products/example.jpg' , a nie '.jpg'
Szeszek1992
Przerobiłem trochę skrypty.
test.php
  1. <?php
  2.  
  3. include ('resize.php');
  4.  
  5. $image = 'example.jpg';
  6. $rootpath = '../files/products/';
  7.  
  8. echo $rootpath.$image."<br><br><br>";
  9.  
  10. if (file_exists($rootpath.$image)) {
  11. echo "Plik " .$image. " istnieje<br><br>";
  12. echo "Próbuję utworzyć miniaturę ".$image."<br><br>";
  13.  
  14. $filepath=makeimage($rootpath.$image, $image, '../files/products/thumb/', 67, 67);
  15.  
  16. echo $filepath;
  17.  
  18. } else {
  19. echo "The file " .$image. " does not exist";
  20. }
  21. ?>


  1. <?php
  2.  
  3. /*
  4.   Version 1.0 Created by: Ryan Stemkoski
  5.   Questions or comments: ryan <at> ipowerplant <dot> com
  6.   Visit us on the web at: http://www.ipowerplant.com
  7.   Purpose: This script can be used to resize one or more images. It will save the file to a directory and output the path to that directory which you
  8.   can display or write to a databse.
  9.  
  10.   TO USE, SET:
  11.   $filename = image to be resized
  12.   $newfilename = added to filename to for each use to keep from overwriting images created example thumbnail_$filename is how it will be saved.
  13.   $path = where the image should be stored and accessed.
  14.   $newwidth = resized width could be larger or smaller
  15.   $newheight = resized height could be larger or smaller
  16.  
  17.   SAMPLE OF FUNCTION: makeimage('image.jpg', 'fullimage_', 'imgs/', 250, 250)
  18.  
  19.   Include the file containing the function in your document and simply call the function with the correct parameters and your image will be resized.
  20.  
  21. */
  22.  
  23. //IMAGE RESIZE FUNCTION FOLLOW ABOVE DIRECTIONS
  24. function makeimage($filename, $newfilename, $path, $newwidth, $newheight) {
  25.  
  26. //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
  27.  
  28. $extension=substr($filename, -4);
  29. if($extension=='.jpg' || $extension=='.gif' ||$extension=='.png')$image_type=$extension;
  30. elseif($extension=='jpeg') $image_type='.jpg';
  31.  
  32.  
  33.  
  34.  
  35. //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
  36. switch($image_type) {
  37. case '.jpg':
  38. $source = imagecreatefromjpeg($filename);
  39. break;
  40. case '.png':
  41. $source = imagecreatefrompng($filename);
  42. break;
  43. case '.gif':
  44. $source = imagecreatefromgif($filename);
  45. break;
  46. default:
  47. echo("Error Invalid Image Type");
  48. die;
  49. break;
  50. }
  51.  
  52. //CREATES THE NAME OF THE SAVED FILE
  53. $file = $newfilename;
  54.  
  55. //CREATES THE PATH TO THE SAVED FILE
  56. $fullpath = $path . $file;
  57.  
  58. //FINDS SIZE OF THE OLD FILE
  59. list($width, $height) = getimagesize($filename);
  60.  
  61. //CREATES IMAGE WITH NEW SIZES
  62. $thumb = imagecreatetruecolor($newwidth, $newheight);
  63.  
  64. //RESIZES OLD IMAGE TO NEW SIZES
  65. imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  66.  
  67. //SAVES IMAGE AND SETS QUALITY || NUMERICAL VALUE = QUALITY ON SCALE OF 1-100
  68. imagejpeg($thumb, $fullpath, 60);
  69.  
  70.  
  71.  
  72. //RETURNS FULL FILEPATH OF IMAGE ENDS FUNCTION
  73. return $fullpath;
  74.  
  75. }
  76.  
  77. ?>


Do rozpoznawania typu zdjęć można też zastosować mime_content_type() lub finfo_file() w połączeniu z funkcją zamieniającą mime_type w rozszerzenie pliku.

Pozdrawiam,

Szeszek1992.
AdamT
Bardzo dziękuję za pomoc Panowie, wszystko chodzi jak należy smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.