Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Tworzenie miniaturek w locie, problem z czarnym tłem
marrrecki
post 13.05.2008, 11:47:54
Post #1





Grupa: Zarejestrowani
Postów: 110
Pomógł: 0
Dołączył: 19.07.2006
Skąd: Lublin

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


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. ?>
Go to the top of the page
+Quote Post
Kocurro
post 13.05.2008, 12:00:30
Post #2





Grupa: Zarejestrowani
Postów: 461
Pomógł: 32
Dołączył: 17.09.2003
Skąd: Łódź

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


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.
Go to the top of the page
+Quote Post
marrrecki
post 13.05.2008, 13:04:55
Post #3





Grupa: Zarejestrowani
Postów: 110
Pomógł: 0
Dołączył: 19.07.2006
Skąd: Lublin

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


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ł.
Go to the top of the page
+Quote Post
marcio
post 13.05.2008, 15:50:23
Post #4





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


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. ?>


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
marrrecki
post 23.05.2008, 10:37:48
Post #5





Grupa: Zarejestrowani
Postów: 110
Pomógł: 0
Dołączył: 19.07.2006
Skąd: Lublin

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


dzięki, ale wolałbym, aby moja funkcja zadziałała.

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