Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Tworzenie miniaturek w locie
Forum PHP.pl > Forum > PHP
marrrecki
Witam.
Napisałem sobie funkcję do tworzenia miniaturek w locie. Dla wyrównania rozmiaru miniaturek, tworzę sobie tło przezroczyste. Problem pojawia się, gdy miniaturyzowany obrazek posiada czarne tło. Po prostu tło czarne robi się białe i psuje mi miniature. W czym może być problem?
Poniżej przedstawiam kod:

  1. <?php
  2. function createMiniImage($url, $width, $height, $alt){
  3. if(file_exists($url)){
  4. $img = $url;
  5. $max_width = $width;
  6. $image_type = substr($img, -3);
  7. $max_height = $height;
  8. $dest = 'images/miniaturki';
  9. $img_name = explode('/', $img);
  10. $im_n=0;
  11. for($im_n=0, $n=sizeof($img_name); $im_n<$n; $im_n++){
  12. $name = $img_name[$im_n];
  13. }
  14.  
  15. $image_to_resize = $img; 
  16. $image_size = getimagesize($image_to_resize); 
  17. if($image_size[0] < $image_size[1]){
  18. $tryb = 'height';
  19. } else {
  20. $tryb = 'width';
  21. }
  22.  
  23. switch($tryb){
  24. case 'width':
  25. $ratio = $image_size[0]/$image_size[1];
  26. if($image_size[0] > $max_width){
  27. $new_image_width = $max_width;
  28. $new_image_height = $new_image_width / $ratio;
  29. } else {
  30. $new_image_width = $image_size[0];
  31. $new_image_height = $image_size[1];
  32. }
  33. break;
  34. case 'height':
  35. $ratio = $image_size[1]/$image_size[0];
  36. if($image_size[1] > $max_height){
  37. $new_image_height = $max_height;
  38. $new_image_width = $new_image_height / $ratio;
  39. } else {
  40. $new_image_width = $image_size[0];
  41. $new_image_height = $image_size[1];
  42. }
  43. break;
  44. }
  45.  
  46.  
  47. $image_type = strtolower($image_type);
  48.  
  49. switch($image_type){
  50. case 'jpg':
  51. $name = strtolower($name);
  52. $name = str_replace('.jpg', '', $name);
  53. $name .= '_w'.$new_image_width.'_h'.$new_image_height.'.jpg.gif';
  54. if(!file_exists($dest.'/'.$name)){
  55. $image = imagecreatefromjpeg($image_to_resize);
  56. $img_mini = imagecreatetruecolor($width, $height);
  57. $trnprt_indx = imagecolortransparent($img_mini);
  58. $trnprt_indx = imagecolorallocate($img_mini, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  59. imagefill($img_mini, 0, 0, $trnprt_indx);
  60. imagecolortransparent($img_mini, $trnprt_indx);
  61. $imagewidth = $width;
  62. $imageheight = $height;
  63. $watermarkwidth = $new_image_width;
  64. $watermarkheight = $new_image_height;
  65. $startwidth = ($imagewidth/2) - ($watermarkwidth/2);
  66. $startheight = ($imageheight/2) - ($watermarkheight/2);
  67. imagecopyresampled($img_mini, $image, $startwidth, $startheight, 0, 0, $new_image_width , $new_image_height, $image_size[0], $image_size[1]);
  68. imagegif($img_mini, $dest.'/'.$name);
  69. imagedestroy($image);
  70. imagedestroy($img_mini);
  71. } else {
  72. $now = strtotime(date('Y-m-d'));
  73. $mk = strtotime(filectime($dest.'/'.$name));
  74. if(($now - $mk) > 604800) {
  75. @unlink($dest.'/'.$name);
  76. $image = imagecreatefromjpeg($image_to_resize);
  77. $img_mini = imagecreatetruecolor($width, $height);
  78. $trnprt_indx = imagecolortransparent($img_mini);
  79. $trnprt_indx = imagecolorallocate($img_mini, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  80. imagefill($img_mini, 0, 0, $trnprt_indx);
  81. imagecolortransparent($img_mini, $trnprt_indx);
  82. $imagewidth = $width;
  83. $imageheight = $height;
  84. $watermarkwidth = $new_image_width;
  85. $watermarkheight = $new_image_height;
  86. $startwidth = ($imagewidth/2) - ($watermarkwidth/2);
  87. $startheight = ($imageheight/2) - ($watermarkheight/2);
  88. imagecopyresampled($img_mini, $image, $startwidth, $startheight, 0, 0, $new_image_width , $new_image_height, $image_size[0], $image_size[1]);
  89. imagegif($img_mini, $dest.'/'.$name);
  90. imagedestroy($image);
  91. imagedestroy($img_mini);
  92. }
  93. }
  94. break;
  95. }
  96. $img = '<img src="'.$dest.'/'.$name.'" alt="'.$alt.'" title="'.$alt.'" />';
  97. return $img;
  98. }
  99. }
  100. ?>
Kocurro
W tych dwóch linijkach (57-58 oraz 78-79):

  1. <?php
  2. $trnprt_indx = imagecolortransparent($img_mini);
  3. $trnprt_indx = imagecolorallocate($img_mini, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  4. ?>


Dwa razy ta sama zmienna inicjujesz ... za drugim razem uzywasz zmiennych niezdefiniowanych ...

pozdr.
marrrecki
faktycznie zrobiłęm błąd w tych miejscach.
Zamiast:
  1. <?php
  2. $trnprt_indx = imagecolortransparent($img_mini);
  3. $trnprt_indx = imagecolorallocate($img_mini, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  4. ?>

powinno być:
  1. <?php
  2. $trnprt_color = imagecolortransparent($img_mini);
  3. $trnprt_indx = imagecolorallocate($img_mini, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  4. ?>


Zmieniłem, niestety problem pozostał.
marcio
Tu masz moja funkcje
  1. <?php
  2. function skaluj($imageFile, $type, $maxImageWidth, $maxImageHeight, $newImageName, $imageQuality) {
  3.  
  4. if($type == 'image/jpg' || $type == 'image/jpeg') $imageData = imagecreatefromjpeg($imageFile);
  5. else if($type == 'image/gif') $imageData = imagecreatefromgif($imageFile);
  6. else if($type == 'image/png') $imageData = imagecreatefrompng($imageFile);
  7.  
  8. //$imageData = imagecreatefromjpeg($imageFile);
  9. list($imageWidth, $imageHeight) = getimagesize($imageFile);
  10. $imageRatioWidth = $imageWidth > $maxImageWidth ? $maxImageWidth / $imageWidth : 1;
  11. $imageRatioHeight = $imageHeight * $imageRatioWidth > $maxImageHeight ? $maxInameHeight / $imageHeight : 1;
  12. $newImageSizeWidth = floor($imageWidth * $imageRatioWidth * $imageRatioHeight);
  13. $newImageSizeHeight = floor($imageHeight * $imageRatioWidth * $imageRatioHeight);
  14. $newImage = imagecreatetruecolor($newImageSizeWidth, $newImageSizeHeight);
  15. imagecopyresampled($newImage, $imageData, 0, 0, 0, 0, $newImageSizeWidth, $newImageSizeHeight, $imageWidth, $imageHeight);
  16. $savePath = $newImageName;
  17. imagejpeg($newImage, $savePath, $imageQuality);
  18. //imagedestroy($imageData); 
  19. }
  20. ?>
marrrecki
dzięki, ale wolałbym, aby moja funkcja zadziałała.

Naprawdę nikt nie miał takiego problemu? Ludzie pomocy!!! sadsmiley02.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.