Autor Zpráva
papirek
Profil *
Ahoj všem používám svou funkci na předělání velikosti obrázku (viz níže), ale teď jsem zjistil, že když do ní pustím PNG obrázek, který je v originále průhledný (pozadí), tak po zmenšení se mi udělá černé pozadí. a průhlednost je pryč. Koukám do toho hodiny, Google taky nic, už si nevím rady. Děkuji.

function obrazek($sirka, $vyska, $soubor, $kam, $id, $dopocitat = 'nic') {
//zjistime typ obrazku
$imgtype=getimagesize($soubor);
switch($imgtype[2]){
case 1:
$im=imagecreatefromgif($soubor);
$k = ".gif";
break;
case 2:
$im=imagecreatefromjpeg($soubor);
$k = ".jpg";
break;
case 3:
$im=imagecreatefrompng($soubor);
imagealphablending($im, true);
$k = ".png";
break;
}
$src_width=imagesx($im);
$src_height=imagesy($im);

//ulozi se v originalni velikosti
if($sirka == 0 and $vyska == 0) {
$height1=$src_height;
$width1=$src_width;
$thumb1=imagecreatetruecolor($width1,$height1);
imagealphablending($thumb1, true);
imagecopyresampled($thumb1,$im,0,0,0,0,$width1,$height1,$src_width, $src_height);
$tmp_cil1=$kam.$id.$k;
if (file_exists($tmp_cil1)) unlink($tmp_cil1);
switch($imgtype[2]){
case 1:
imagegif($thumb1,$tmp_cil1);
break;
case 2:
imagejpeg($thumb1,$tmp_cil1, 100);
break;
case 3:
imagesavealpha($thumb1, true);
imagepng($thumb1,$tmp_cil1);
break;
}
imagedestroy($thumb1);
}
else {
if($dopocitat == "sirku") {
$height2=$vyska;
$width2=$height2/$src_height*$src_width;
}
elseif($dopocitat == "vysku") {
$width2=$sirka;
$height2=$width2/$src_width*$src_height;
}
elseif($dopocitat == "nic") {
$width2=$sirka;
$height2=$vyska;
}
$thumb2=imagecreatetruecolor($width2,$height2);
imagealphablending($thumb2, true);
imagecopyresampled($thumb2,$im,0,0,0,0,$width2,$height2,$src_width, $src_height);
$tmp_cil2=$kam.$id.$k;
if (file_exists($tmp_cil2)) unlink($tmp_cil2);
switch($imgtype[2]){
case 1:
imagegif($thumb2,$tmp_cil2);
break;
case 2:
imagejpeg($thumb2,$tmp_cil2, 100);
break;
case 3:
imagesavealpha($thumb2, true);
imagepng($thumb2,$tmp_cil2);
break;
}
imagedestroy($thumb2);
}
}
ShiraNai7
Profil
Zkus (namísto imagealphablending($im, true); na ř.15):

imagealphablending($thumb1, false);
$transColorAl = imagecolorallocatealpha($thumb1, 0, 0, 0, 127);
imagefill($thumb1, 0, 0, $transColorAl);
imagesavealpha($thumb1, true);
papirek
Profil *
Snad jsem to správně pochopil.. upravil jsem funkci takto a pořád černé pozadí...

function obrazek($sirka, $vyska, $soubor, $kam, $id, $dopocitat = 'nic') {
    //zjistime typ obrazku
   $imgtype=getimagesize($soubor);
        switch($imgtype[2]){
        case 1:
            $im=imagecreatefromgif($soubor);
            $k = ".gif";
            break;
        case 2:
            $im=imagecreatefromjpeg($soubor);
            $k = ".jpg";
            break;
        case 3:
            $im=imagecreatefrompng($soubor);
            imagealphablending($im, false);
            $transColorAl = imagecolorallocatealpha($im, 0, 0, 0, 127);
            imagefill($im, 0, 0, $transColorAl);
            imagesavealpha($im, true);
            $k = ".png";
            break;
        }
        $src_width=imagesx($im);
        $src_height=imagesy($im);

//ulozi se v originalni velikosti  
if($sirka == 0 and $vyska == 0) {
$height1=$src_height;
$width1=$src_width;
$thumb1=imagecreatetruecolor($width1,$height1);
imagealphablending($thumb1, true);
imagecopyresampled($thumb1,$im,0,0,0,0,$width1,$height1,$src_width, $src_height);        
$tmp_cil1=$kam.$id.$k;
if (file_exists($tmp_cil1)) unlink($tmp_cil1);    
switch($imgtype[2]){
        case 1:
            imagegif($thumb1,$tmp_cil1);
            break;
        case 2:
            imagejpeg($thumb1,$tmp_cil1, 100);
            break;
        case 3:
            imagesavealpha($thumb1, true);
            imagepng($thumb1,$tmp_cil1);    
            break;
        }
imagedestroy($thumb1);
}
else {
    if($dopocitat == "sirku") {
  $height2=$vyska;
  $width2=$height2/$src_height*$src_width;
    }
    elseif($dopocitat == "vysku") {
  $width2=$sirka;
  $height2=$width2/$src_width*$src_height;
    }
    elseif($dopocitat == "nic") {
  $width2=$sirka;
  $height2=$vyska;
    }
$thumb2=imagecreatetruecolor($width2,$height2);        
imagealphablending($thumb2, true);
imagecopyresampled($thumb2,$im,0,0,0,0,$width2,$height2,$src_width, $src_height);        
$tmp_cil2=$kam.$id.$k;
if (file_exists($tmp_cil2)) unlink($tmp_cil2);
switch($imgtype[2]){
        case 1:
            imagegif($thumb2,$tmp_cil2);
            break;
        case 2:
            imagejpeg($thumb2,$tmp_cil2, 100);
            break;
        case 3:
            imagesavealpha($thumb2, true);
            imagepng($thumb2,$tmp_cil2);
            break;
        }
imagedestroy($thumb2);
}
    }
ShiraNai7
Profil
Nn.. ty funkce aplikuj na $thumb1 tak, jak jsem napsal (jen přepiš $im zpět na $thumb1).
papirek
Profil *
to bych to tam měl mít ale i pro $thumb2 ne ??


hm, tak to mi haže nějaké chyby, nevím přijde mi to nelogický ta tvoje úprava, opravdu jsem to pochopil správně ? Ještě jednou.

function obrazek($sirka, $vyska, $soubor, $kam, $id, $dopocitat = 'nic') {
    //zjistime typ obrazku
   $imgtype=getimagesize($soubor);
        switch($imgtype[2]){
        case 1:
            $im=imagecreatefromgif($soubor);
            $k = ".gif";
            break;
        case 2:
            $im=imagecreatefromjpeg($soubor);
            $k = ".jpg";
            break;
        case 3:
            $im=imagecreatefrompng($soubor);
            imagealphablending($thumb1, false);
            $transColorAl = imagecolorallocatealpha($thumb1, 0, 0, 0, 127);
            imagefill($thumb1, 0, 0, $transColorAl);
            imagesavealpha($thumb1, true);
            $k = ".png";
            break;
        }
        $src_width=imagesx($im);
        $src_height=imagesy($im);

//ulozi se v originalni velikosti  
if($sirka == 0 and $vyska == 0) {
$height1=$src_height;
$width1=$src_width;
$thumb1=imagecreatetruecolor($width1,$height1);
imagealphablending($thumb1, true);
imagecopyresampled($thumb1,$im,0,0,0,0,$width1,$height1,$src_width, $src_height);        
$tmp_cil1=$kam.$id.$k;
if (file_exists($tmp_cil1)) unlink($tmp_cil1);    
switch($imgtype[2]){
        case 1:
            imagegif($thumb1,$tmp_cil1);
            break;
        case 2:
            imagejpeg($thumb1,$tmp_cil1, 100);
            break;
        case 3:
            imagesavealpha($thumb1, true);
            imagepng($thumb1,$tmp_cil1);    
            break;
        }
imagedestroy($thumb1);
}
else {
    if($dopocitat == "sirku") {
  $height2=$vyska;
  $width2=$height2/$src_height*$src_width;
    }
    elseif($dopocitat == "vysku") {
  $width2=$sirka;
  $height2=$width2/$src_width*$src_height;
    }
    elseif($dopocitat == "nic") {
  $width2=$sirka;
  $height2=$vyska;
    }
$thumb2=imagecreatetruecolor($width2,$height2);        
imagealphablending($thumb2, true);
imagecopyresampled($thumb2,$im,0,0,0,0,$width2,$height2,$src_width, $src_height);        
$tmp_cil2=$kam.$id.$k;
if (file_exists($tmp_cil2)) unlink($tmp_cil2);
switch($imgtype[2]){
        case 1:
            imagegif($thumb2,$tmp_cil2);
            break;
        case 2:
            imagejpeg($thumb2,$tmp_cil2, 100);
            break;
        case 3:
            imagesavealpha($thumb2, true);
            imagepng($thumb2,$tmp_cil2);
            break;
        }
imagedestroy($thumb2);
}
    }

protože za tohohle stavu mi to háže chyby na těch nových řádcích...
ShiraNai7
Profil
function obrazek($sirka, $vyska, $soubor, $kam, $id, $dopocitat = 'nic')
{
    //zjistime typ obrazku
    $imgtype = getimagesize($soubor);
    switch ($imgtype[2]) {
        case 1:
            $im = imagecreatefromgif($soubor);
            $k = ".gif";
            break;
        case 2:
            $im = imagecreatefromjpeg($soubor);
            $k = ".jpg";
            break;
        case 3:
            $im = imagecreatefrompng($soubor);
            $k = ".png";
            break;
    }
    $src_width = imagesx($im);
    $src_height = imagesy($im);

    //ulozi se v originalni velikosti  
    if ($sirka == 0 and $vyska == 0) {
        $height1 = $src_height;
        $width1 = $src_width;
        $thumb1 = imagecreatetruecolor($width1, $height1);
        imagealphablending($thumb1, true);
        imagecopyresampled($thumb1, $im, 0, 0, 0, 0, $width1, $height1, $src_width, $src_height);
        $tmp_cil1 = $kam . $id . $k;
        if (file_exists($tmp_cil1))
            unlink($tmp_cil1);
        switch ($imgtype[2]) {
            case 1:
                imagegif($thumb1, $tmp_cil1);
                break;
            case 2:
                imagejpeg($thumb1, $tmp_cil1, 100);
                break;
            case 3:
                imagealphablending($thumb1, false);
                $transColorAl = imagecolorallocatealpha($thumb1, 0, 0, 0, 127);
                imagefill($thumb1, 0, 0, $transColorAl);
                imagesavealpha($thumb1, true);
                imagepng($thumb1, $tmp_cil1);
                break;
        }
        imagedestroy($thumb1);
    } else {
        if ($dopocitat == "sirku") {
            $height2 = $vyska;
            $width2 = $height2 / $src_height * $src_width;
        } elseif ($dopocitat == "vysku") {
            $width2 = $sirka;
            $height2 = $width2 / $src_width * $src_height;
        } elseif ($dopocitat == "nic") {
            $width2 = $sirka;
            $height2 = $vyska;
        }
        $thumb2 = imagecreatetruecolor($width2, $height2);
        imagealphablending($thumb2, true);
        imagecopyresampled($thumb2, $im, 0, 0, 0, 0, $width2, $height2, $src_width, $src_height);
        $tmp_cil2 = $kam . $id . $k;
        if (file_exists($tmp_cil2))
            unlink($tmp_cil2);
        switch ($imgtype[2]) {
            case 1:
                imagegif($thumb2, $tmp_cil2);
                break;
            case 2:
                imagejpeg($thumb2, $tmp_cil2, 100);
                break;
            case 3:
                imagealphablending($thumb2, false);
                $transColorAl = imagecolorallocatealpha($thumb2, 0, 0, 0, 127);
                imagefill($thumb2, 0, 0, $transColorAl);
                imagesavealpha($thumb2, true);
                imagepng($thumb2, $tmp_cil2);
                break;
        }
        imagedestroy($thumb2);
    }
}
papirek
Profil *
to už mi nefunguje vůbec, neukládá to vůbec náhledové fotky..
ShiraNai7
Profil
Opraveno. Ta tvoje funkce je neskutečný bordel, nevšiml jsem si, že tam nastavuješ ten blending na více místech. Mimochodem parametr "dopocitat" je zbytečný (to poznáš podle toho, který z parametrů je nebo není null / nula) a kód funkce by se dal zkrátit tak na třetinu. Ale to už nechám na tobě.

function obrazek($sirka, $vyska, $soubor, $kam, $id, $dopocitat = 'nic')
{
    //zjistime typ obrazku
    $imgtype = getimagesize($soubor);
    switch ($imgtype[2]) {
        case 1:
            $im = imagecreatefromgif($soubor);
            $k = ".gif";
            break;
        case 2:
            $im = imagecreatefromjpeg($soubor);
            $k = ".jpg";
            break;
        case 3:
            $im = imagecreatefrompng($soubor);
            $k = ".png";
            break;
    }
    $src_width = imagesx($im);
    $src_height = imagesy($im);

    //ulozi se v originalni velikosti
    if ($sirka == 0 and $vyska == 0) {
        $height1 = $src_height;
        $width1 = $src_width;
        $thumb1 = imagecreatetruecolor($width1, $height1);
        imagealphablending($thumb1, true);
        imagecopyresampled($thumb1, $im, 0, 0, 0, 0, $width1, $height1, $src_width, $src_height);
        $tmp_cil1 = $kam . $id . $k;
        if (file_exists($tmp_cil1))
            unlink($tmp_cil1);
        switch ($imgtype[2]) {
            case 1:
                imagegif($thumb1, $tmp_cil1);
                break;
            case 2:
                imagejpeg($thumb1, $tmp_cil1, 100);
                break;
            case 3:
                imagealphablending($thumb1, false);
                $transColorAl = imagecolorallocatealpha($thumb1, 0, 0, 0, 127);
                imagefill($thumb1, 0, 0, $transColorAl);
                imagesavealpha($thumb1, true);
                imagepng($thumb1, $tmp_cil1);
                break;
        }
        imagedestroy($thumb1);
    } else {
        if ($dopocitat == "sirku") {
            $height2 = $vyska;
            $width2 = $height2 / $src_height * $src_width;
        } elseif ($dopocitat == "vysku") {
            $width2 = $sirka;
            $height2 = $width2 / $src_width * $src_height;
        } elseif ($dopocitat == "nic") {
            $width2 = $sirka;
            $height2 = $vyska;
        }
        $thumb2 = imagecreatetruecolor($width2, $height2);
        if (3 == $imgtype[2]) {
            imagealphablending($thumb2, false);
            $transColorAl = imagecolorallocatealpha($thumb2, 0, 0, 0, 127);
            imagefill($thumb2, 0, 0, $transColorAl);
            imagesavealpha($thumb2, true);
        }
        imagecopyresampled($thumb2, $im, 0, 0, 0, 0, $width2, $height2, $src_width, $src_height);
        $tmp_cil2 = $kam . $id . $k;
        if (file_exists($tmp_cil2))
            unlink($tmp_cil2);
        switch ($imgtype[2]) {
            case 1:
                imagegif($thumb2, $tmp_cil2);
                break;
            case 2:
                imagejpeg($thumb2, $tmp_cil2, 100);
                break;
            case 3:
                imagepng($thumb2, $tmp_cil2);
                break;
        }
        imagedestroy($thumb2);
    }
}

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0