Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Pobieranie zmiennej z innej funkcji?
Forum PHP.pl > Forum > Przedszkole
JamalBIG
Witam

W jaki sposob zrobic aby funkcja jakasfunkcja() pobierala zmienna $nazwa ktora jest nad nia?

$nazwa = "nazwa";
function jakasfunkcja()
b4x
  1. <?php
  2. function jakasfunkcja($nazwa) {
  3. #polecenie do wykonania
  4. }
  5. ?>

Niewiem czy o to ci chodziło :-)
Cezar708
być może o to Ci chodzi:
  1. <?php
  2. $nazwa = "wartość";
  3.  
  4. function jakas_funkcja(){
  5. global $nazwa;
  6. echo $nazwa;
  7. }
  8. ?>


nie jest to polecane, lepiej jak już tak chcesz robić to zrób tak:
  1. <?php
  2. class C{
  3. public static $nazwa;
  4. }
  5.  
  6. function jakas_funkcja(){
  7. echo C::$nazwa;
  8. }
  9. ?>
JamalBIG
Chodzi o to ze nie chce mi sie tworzyc miniaturka w tym kodzie i mysle ze jest to spowodowane tym, ze nie funkcja makeIcons_MergeCenter chce przeczytac zmiennych $cat, $r i $file (96 wiersz)

<?php
function makeIcons_MergeCenter($src, $dst, $dstx, $dsty){

// sprawdzanie rozszerzenia pliku
$allowedExtensions = 'jpg jpeg gif png'; // dozwolone

$name = explode(".", $src);
$currentExtensions = $name[count($name)-1];
$extensions = explode(" ", $allowedExtensions);

for($i=0; count($extensions)>$i; $i=$i+1){
if($extensions[$i]==$currentExtensions)
{ $extensionOK=1;
$fileExtension=$extensions[$i];
break; }
}

if($extensionOK){

$size = getImageSize($src);
$width = $size[0];
$height = $size[1];
// działanie tylko gdy oryginalny rozmiar pliku
// jest większy niż docelowy
if($width >= $dstx AND $height >= $dsty){
// ustalanie proporcji
$proportion_X = $width / $dstx;
$proportion_Y = $height / $dsty;

if($proportion_X > $proportion_Y ){
$proportion = $proportion_Y;
}else{
$proportion = $proportion_X ;
}
$target['width'] = $dstx * $proportion;
$target['height'] = $dsty * $proportion;

$original['diagonal_center'] =
round(sqrt(($width*$width)+($height*$height))/2);
$target['diagonal_center'] =
round(sqrt(($target['width']*$target['width'])+
($target['height']*$target['height']))/2);

$crop = round($original['diagonal_center'] - $target['diagonal_center']);

if($proportion_X < $proportion_Y ){
$target['x'] = 0;
$target['y'] = round((($height/2)*$crop)/$target['diagonal_center']);
}else{
$target['x'] = round((($width/2)*$crop)/$target['diagonal_center']);
$target['y'] = 0;
}
// tworzenie grafiki w zależności od rozszerzenia
if($fileExtension == "jpg" OR $fileExtension=='jpeg'){
$from = ImageCreateFromJpeg($src);
}elseif ($fileExtension == "gif"){
$from = ImageCreateFromGIF($src);
}elseif ($fileExtension == 'png'){
$from = imageCreateFromPNG($src);
}

$new = ImageCreateTrueColor ($dstx,$dsty);

imagecopyresampled ($new, $from, 0, 0, $target['x'],
$target['y'], $dstx, $dsty, $target['width'], $target['height']);
// zapisywanie nowo utworzonej grafiki
if($fileExtension == "jpg" OR $fileExtension == 'jpeg'){
imagejpeg($new, $dst, 70);
}elseif ($fileExtension == "gif"){
imagegif($new, $dst);
}elseif ($fileExtension == 'png'){
imagepng($new, $dst);
}
}
}
}

if ($cat == "c1") {
$cat = "videos";
} else {
$cat = "torrents";
}
$file = $_FILES['my_file']['name'];
$file = str_replace(' ', '', $file);
$r = explode('.',$file);
$r = $r[0];
$r = str_replace(' ', '', $r);

$filename = "downloads/$cat/$file";
if (file_exists($filename)) {
echo "Plik $filename juz istnieje $file";
} else {
mkdir ("downloads/$cat/$r");
if(isset($_FILES['my_file'])) {
if(is_uploaded_file($_FILES['my_file']['tmp_name'])) {
move_uploaded_file($_FILES['my_file']['tmp_name'], "downloads/$cat/$r/$file");
}
}
$pic = $_FILES['my_pic']['name'];
$pic = str_replace(' ', '', $pic);
$p = explode('.',$pic);
$p = $r[0];
$p = str_replace(' ', '', $p);
if(isset($_FILES['my_pic'])) {
if(is_uploaded_file($_FILES['my_pic']['tmp_name'])) {
move_uploaded_file($_FILES['my_pic']['tmp_name'], "downloads/$cat/$r/$pic");
}
}
echo "Plik downloads/$cat/$r/$file zostal dodany";
}

makeIcons_MergeCenter('downloads/$cat/$r/$file', 'downloads/$cat/$r/thumb.jpg', 200, 200);
?>
Cezar708
no to zrób na początku funckji:

  1. <?php
  2. function makeIcons_MergeCenter($src, $dst, $dstx, $dsty){
  3.  
  4. global $cat, $r, $file;
  5.  
  6. // i dalsza część kodu
  7. ?>


pozdrawiam
Neeke
o boshe .......... cos takiego jak bbcody znasz? zrób to ladnie
b_chmura
swoją funkcje makeIcons_MergeCenter podmień na tą:

  1. <?php
  2.  
  3. function makeIcons_MergeCenter($in, $out, $max_width, $max_height, $jakosc = 80) //mała poprawka 
  4. {
  5.  
  6. $typ = img_typ($in);
  7.  
  8. if ($typ == ".jpg") {$img_src = imagecreatefromjpeg($in);}
  9. elseif($typ == ".gif") {$img_src = imagecreatefromgif($in); }
  10. elseif($typ == ".png") {$img_src = imagecreatefrompng($in); }
  11.  
  12. $x = imagesx($img_src);
  13. $y = imagesy($img_src);
  14.  
  15. if($x > $y) 
  16. {
  17. $xr = ($x > $max_width)$max_width/$x : 1;
  18. $yr = $xr;
  19. }
  20. else 
  21. {
  22. $yr = ($y > $max_height)$max_height/$y : 1;
  23. $xr = $yr;
  24. }
  25.  
  26. $new_x = $x*$xr;
  27. $new_y = $y*$yr;
  28.  
  29. $new_img = imagecreatetruecolOR($new_x, $new_y);
  30. $background = imagecolORallocate($new_img, 255, 255, 255);
  31.  
  32. imagefill($new_img, 0, 0, $background);
  33. imagecopyresampled($new_img, $img_src, 0, 0, 0, 0, $new_x, $new_y, $x, $y);
  34.  
  35. $typ = img_typ($out);
  36.  
  37. if ($typ == ".jpg") {imagejpeg($new_img, $out, $jakosc);}
  38. elseif($typ == ".png") { imagepng($new_img, $out, $jakosc);}
  39. elseif($typ == ".gif") { imagegif($new_img, $out);  }
  40. }
  41. ?>


i dodaj tą:
  1. <?php
  2. function img_typ($name) 
  3. {
  4. $name = strtolower(substr($name, -4, 4));
  5.  
  6. if($name == '.jpg' OR $name == 'jpeg') 
  7. {
  8. return ".jpg";
  9. }
  10. elseif($name == '.gif') 
  11. {
  12. return ".gif";
  13. }
  14. elseif($name == '.png')
  15. {
  16. return ".png";
  17. }
  18. }
  19. ?>


Edit:
Cytat
o boshe ...

~Neekea a Ty pisać po polsku
JamalBIG
Wyszlo cos takiego ale nadal nic... Pojawiaja sie takie ostrzezenia:

Cytat
Warning: imagesx(): supplied argument is not a valid Image resource in c:\usr\apache\httpd\html\index.php on line 45

Warning: imagesy(): supplied argument is not a valid Image resource in c:\usr\apache\httpd\html\index.php on line 46

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in c:\usr\apache\httpd\html\index.php on line 62

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in c:\usr\apache\httpd\html\index.php on line 63

Warning: imagefill(): supplied argument is not a valid Image resource in c:\usr\apache\httpd\html\index.php on line 65

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in c:\usr\apache\httpd\html\index.php on line 66

Warning: imagejpeg(): supplied argument is not a valid Image resource in c:\usr\apache\httpd\html\index.php on line 70


  1. <?php
  2. if ($cat == "c1") {
  3. $cat = "videos";
  4. } else {
  5. $cat = "torrents";
  6. }
  7. $file = $_FILES['my_file']['name'];
  8. $file = str_replace(' ', '', $file);
  9. $r = explode('.',$file);
  10. $r = $r[0];
  11. $r = str_replace(' ', '', $r);
  12.  
  13. $filename = "downloads/$cat/$file";
  14. if (file_exists($filename)) {
  15. echo "Plik $filename juz istnieje $file";
  16. } else {
  17. mkdir ("downloads/$cat/$r");
  18.  if(isset($_FILES['my_file'])) {
  19. if(is_uploaded_file($_FILES['my_file']['tmp_name'])) {
  20. move_uploaded_file($_FILES['my_file']['tmp_name'], "downloads/$cat/$r/$file");
  21. }
  22. }
  23. $pic = $_FILES['my_pic']['name'];
  24. $pic = str_replace(' ', '', $pic);
  25. $p = explode('.',$pic);
  26. $p = $r[0];
  27. $p = str_replace(' ', '', $p);
  28. if(isset($_FILES['my_pic'])) {
  29. if(is_uploaded_file($_FILES['my_pic']['tmp_name'])) {
  30. move_uploaded_file($_FILES['my_pic']['tmp_name'], "downloads/$cat/$r/$pic");
  31. }
  32. }
  33. echo "Plik downloads/$cat/$r/$file zostal dodany";
  34. }
  35.  
  36. function makeIcons_MergeCenter($in, $out, $max_width, $max_height, $jakosc = 80) //mała poprawka 
  37. {
  38.  
  39. $typ = img_typ($in);
  40.  
  41. if ($typ == ".jpg") {$img_src = imagecreatefromjpeg($in);}
  42. elseif($typ == ".gif") {$img_src = imagecreatefromgif($in); }
  43. elseif($typ == ".png") {$img_src = imagecreatefrompng($in); }
  44.  
  45. $x = imagesx($img_src);
  46. $y = imagesy($img_src);
  47.  
  48. if($x > $y) 
  49. {
  50. $xr = ($x > $max_width)$max_width/$x : 1;
  51. $yr = $xr;
  52. }
  53. else 
  54. {
  55. $yr = ($y > $max_height)$max_height/$y : 1;
  56. $xr = $yr;
  57. }
  58.  
  59. $new_x = $x*$xr;
  60. $new_y = $y*$yr;
  61.  
  62. $new_img = imagecreatetruecolOR($new_x, $new_y);
  63. $background = imagecolORallocate($new_img, 255, 255, 255);
  64.  
  65. imagefill($new_img, 0, 0, $background);
  66. imagecopyresampled($new_img, $img_src, 0, 0, 0, 0, $new_x, $new_y, $x, $y);
  67.  
  68. $typ = img_typ($out);
  69.  
  70. if ($typ == ".jpg") {imagejpeg($new_img, $out, $jakosc);}
  71. elseif($typ == ".png") { imagepng($new_img, $out, $jakosc);}
  72. elseif($typ == ".gif") { imagegif($new_img, $out);  }
  73. }
  74.  
  75. function img_typ($name) 
  76. {
  77. $name = strtolower(substr($name, -4, 4));
  78.  
  79. if($name == '.jpg' OR $name == 'jpeg') 
  80. {
  81. return ".jpg";
  82. }
  83. elseif($name == '.gif') 
  84. {
  85. return ".gif";
  86. }
  87. elseif($name == '.png')
  88. {
  89. return ".png";
  90. }
  91. }
  92.  
  93. makeIcons_MergeCenter('downloads/$cat/$r/$file', 'downloads/$cat/$r/thumb.jpg', 200, 200);
  94. ?>
b_chmura
sprawdź dobrze co kryje sie pod ścieżką downloads/$cat/$r/$file
JamalBIG
Wszystko jest ok bo fotka mi sie dobrze uploaduje, katalog sie tworzy tylko ta nieszczesna miniaturka nie chce sie 'zrobic'.... Calkowicie nie wiem dlaczego....
W tej postaci dziala, w tej powyzej juz nie...

  1. <?php
  2.  
  3. function makeIcons_MergeCenter($in, $out, $max_width, $max_height, $jakosc = 80) //mała poprawka 
  4. {
  5.  
  6. $typ = img_typ($in);
  7.  
  8. if ($typ == ".jpg") {$img_src = imagecreatefromjpeg($in);}
  9. elseif($typ == ".gif") {$img_src = imagecreatefromgif($in); }
  10. elseif($typ == ".png") {$img_src = imagecreatefrompng($in); }
  11.  
  12. $x = imagesx($img_src);
  13. $y = imagesy($img_src);
  14.  
  15. if($x > $y) 
  16. {
  17. $xr = ($x > $max_width)$max_width/$x : 1;
  18. $yr = $xr;
  19. }
  20. else 
  21. {
  22. $yr = ($y > $max_height)$max_height/$y : 1;
  23. $xr = $yr;
  24. }
  25.  
  26. $new_x = $x*$xr;
  27. $new_y = $y*$yr;
  28.  
  29. $new_img = imagecreatetruecolOR($new_x, $new_y);
  30. $background = imagecolORallocate($new_img, 255, 255, 255);
  31.  
  32. imagefill($new_img, 0, 0, $background);
  33. imagecopyresampled($new_img, $img_src, 0, 0, 0, 0, $new_x, $new_y, $x, $y);
  34.  
  35. $typ = img_typ($out);
  36.  
  37. if ($typ == ".jpg") {imagejpeg($new_img, $out, $jakosc);}
  38. elseif($typ == ".png") { imagepng($new_img, $out, $jakosc);}
  39. elseif($typ == ".gif") { imagegif($new_img, $out);  }
  40. }
  41.  
  42. function img_typ($name) 
  43. {
  44. $name = strtolower(substr($name, -4, 4));
  45.  
  46. if($name == '.jpg' OR $name == 'jpeg') 
  47. {
  48. return ".jpg";
  49. }
  50. elseif($name == '.gif') 
  51. {
  52. return ".gif";
  53. }
  54. elseif($name == '.png')
  55. {
  56. return ".png";
  57. }
  58. }
  59.  
  60. makeIcons_MergeCenter('test.jpg', 'nazwa.jpg', 200, 200);
  61. ?>
b_chmura
spróbuj tak
  1. <?php
  2. function makeIcons_MergeCenter($in, $out, $max_width, $max_height, $jakosc = 80) 
  3. {
  4.  
  5. $typ = img_typ($in);
  6. /*
  7. if ($typ == ".jpg") {$img_src = imagecreatefromjpeg($in);}
  8. elseif($typ == ".gif") {$img_src = imagecreatefromgif($in); }
  9. elseif($typ == ".png") {$img_src = imagecreatefrompng($in); }
  10. */
  11. $img_src = $in;
  12. $x = imagesx($img_src);
  13. $y = imagesy($img_src);
  14.  
  15. if($x > $y) 
  16. {
  17. $xr = ($x > $max_width)$max_width/$x : 1;
  18. $yr = $xr;
  19. }
  20. else 
  21. {
  22. $yr = ($y > $max_height)$max_height/$y : 1;
  23. $xr = $yr;
  24. }
  25.  
  26. $new_x = $x*$xr;
  27. $new_y = $y*$yr;
  28.  
  29. $new_img = imagecreatetruecolOR($new_x, $new_y);
  30. $background = imagecolORallocate($new_img, 255, 255, 255);
  31.  
  32. imagefill($new_img, 0, 0, $background);
  33. imagecopyresampled($new_img, $img_src, 0, 0, 0, 0, $new_x, $new_y, $x, $y);
  34.  
  35. $typ = img_typ($out);
  36.  
  37. if ($typ == ".jpg") {imagejpeg($new_img, $out, $jakosc);}
  38. elseif($typ == ".png") { imagepng($new_img, $out, $jakosc);}
  39. elseif($typ == ".gif") { imagegif($new_img, $out);  }
  40. }
  41. ?>


ale nie obiecuje, nie mam na razie jak testować.
JamalBIG
w wierszu 90

  1. <?php
  2. makeIcons_MergeCenter('downloads/$cat/$r/$file', 'downloads/$cat/$r/thumb.jpg', 200, 200);
  3. ?>


trzeba tylko zmienic ' na " i wszystko chodzi gladko... taki pierdol a potrafi zepsuc wszystko, ale mimo to dzieki wszystkim za pomoc!

  1. <?php
  2. makeIcons_MergeCenter("downloads/$cat/$r/$file", "downloads/$cat/$r/thumb.jpg", 200, 200);
  3. ?>
b_chmura
gapa ze mnie

ewentulanie
  1. <?php
  2. makeIcons_MergeCenter('downloads/'.$cat.'/'.$r.'/'.$file, 'downloads/'.$cat.'/'.$r.'/thumb.jpg', 200, 200);
  3. ?>
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.