Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] imagecreatefromjpeg() problem ze ścieżką dostępu do pliku., Problem ze zmianą rozmiaru obrazka z innej lokaliacji niż skrypt.
AdamT
post
Post #1





Grupa: Zarejestrowani
Postów: 54
Pomógł: 0
Dołączył: 22.01.2004
Skąd: LDZ

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


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. ?>
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 22.12.2025 - 23:44