Áttetsző PNG képek megjelenítése
A minap akadt egy olyan problémám, hogy átméretezve kellett megjelenítenem egy áttetsző png képet php-vel. Azonban fehér hátteret adott vissza a megjelenítő. Megosztom veletek is a kódot, hátha valakinek szüksége lesz rá. Én is úgy töltöttem le valahonnan, de sajnos már nem tudom honnan!
A kód a következő:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function setTransparency($new_image,$image_source) { $transparencyIndex = imagecolortransparent($image_source); $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255); if ($transparencyIndex >= 0) { $transparencyColor = imagecolorsforindex($image_source, $transparencyIndex); } $transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']); imagefill($new_image, 0, 0, $transparencyIndex); imagecolortransparent($new_image, $transparencyIndex); } |
Áttetsző Croppolt PNG
A bejegyzésben látható borocskát így jelenítettem meg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | <?php function png($source="image.png",$width="30",$height="100"){ header('Content-Type: image/png'); $arr = getimagesize($source); $dWidth = $arr[0]; $dHeight = $arr[1]; $ratio_w = $width / $dWidth; $ratio_h = $height / $dHeight; if ($ratio_w > $ratio_h) $ratio = $ratio_w; else $ratio = $ratio_h; $image = imagecreatefrompng($source); $new_width = $ratio * $dWidth; $new_height = $ratio * $dHeight; $temp = imagecreatetruecolor($new_width, $new_height); setTransparency($temp,$image); imagecopyresampled($temp, $image, 0, 0, 0, 0, $new_width, $new_height, $dWidth, $dHeight); $src = $temp; $w = imagesx($src); $h = imagesy($src); $dest = imagecreatetruecolor($width, $height); setTransparency($dest,$image); if ($w < $width) { $x_coord = floor(($width - $w)/2); $width = $w; } else { $x_coord = (-1) * floor(($w - $width)/2); $width = floor(($w - $width)/2) + $width; } if ($h < $height) { $y_coord = floor(($height - $h)/2); $height = $h; } else { $y_coord = (-1) * floor(($h - $height)/2); $height = floor(($h - $height)/2) + $height; } $white = imagecolorallocate($dest,255,255,255); imagefill($dest, 0, 0, $white); imagecopyresampled($dest,$src, $x_coord, $y_coord, 0,0, $width, $height, $width, $height); imagefilledrectangle($dest, 0,$height - ($y_coord), $width, $height, $white); imagepng($dest); imagedestroy($src); } function setTransparency($new_image,$image_source) { $transparencyIndex = imagecolortransparent($image_source); $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255); if ($transparencyIndex >= 0) { $transparencyColor = imagecolorsforindex($image_source, $transparencyIndex); } $transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']); imagefill($new_image, 0, 0, $transparencyIndex); imagecolortransparent($new_image, $transparencyIndex); } png(); ?> |
