Autor | Zpráva | ||
---|---|---|---|
Jack Finger Profil |
#1 · Zasláno: 13. 1. 2008, 14:01:33
tak ještě jeden dotaz:
mám funkci Resize(), které předávám 5 parametrů: cestu zdroje, cestu cíle, max. cílová šířka, max. cílová výška a typ souboru. fce je odkudsi zkopírovaná, ale dost předělaná... s obrázky JPEG/JPG si rozumí dobře, s GIF a PNG funguje taky, jen místo zmenšeného obrázku vyhodí černou plochu. čím to? // --- Resize() --- function Resize($filename, $copypath, $MaxWidth, $MaxHeight, $filetype = 'jpeg'){ list($OrigWidth, $OrigHeight) = getimagesize($filename); if ($MaxWidth == 0) $MaxWidth = $OrigWidth; if ($MaxHeight == 0) $MaxHeight = $OrigHeight; $pw = $OrigWidth / $MaxWidth; $ph = $OrigHeight / $MaxHeight; if ($pw > $ph) $p = $pw; else $p = $ph; if ($p < 1) $p = 1; $NewWidth = (int)$OrigWidth / $p; $NewHeight = (int)$OrigHeight / $p; $image_p = imagecreatetruecolor($NewWidth, $NewHeight); if($filetype == 'jpeg'){ $image = imagecreatefromjpeg($filename); } elseif($filetype == 'gif'){ $image = imagecreatefromgif($filename); } elseif($filetype == 'png'){ $image = imagecreatefrompng($filename); } imagecopyresampled($image_p, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $OrigWidth, $OrigHeight); if($filetype == 'jpeg'){ $do = imagejpeg($image_p,$copypath, 100); } elseif($filetype == 'gif'){ $do = imagegif($image_p,$copypath, 100); } elseif($filetype == 'png'){ $do = imagepng($image_p,$copypath, 100); } if($do) return true; else return false; } |
||
widlak Profil |
#2 · Zasláno: 13. 1. 2008, 20:53:17 · Upravil/a: widlak
Mne to funguje presne tak ako má. Akú verziu GD používaš? Ja mám 2.0.34. A ešte jedna vec, ten tretí parameter (100) v imagegif(), imagejpeg() a imagepng() je podporovaný až od PHP 5.1.2, skús ho vymazať, alebo zmeniť. Povolené hodnoty sú totižto 0-9.
|
||
widlak Profil |
#3 · Zasláno: 13. 1. 2008, 21:13:52 · Upravil/a: widlak
... a tú funkciu by som upravil takto:
<?php // --- Resize() --- function Resize($filename, $copypath, $MaxWidth, $MaxHeight){ list($OrigWidth, $OrigHeight) = getimagesize($filename); if ($MaxWidth == 0) $MaxWidth = $OrigWidth; if ($MaxHeight == 0) $MaxHeight = $OrigHeight; $pw = $OrigWidth / $MaxWidth; $ph = $OrigHeight / $MaxHeight; if ($pw > $ph) $p = $pw; else $p = $ph; if ($p < 1) $p = 1; $NewWidth = (int)$OrigWidth / $p; $NewHeight = (int)$OrigHeight / $p; $image_p = imagecreatetruecolor($NewWidth, $NewHeight); $info = pathinfo($filename); switch($info['extension']) { case 'jpeg': case 'png': case 'gif': $func1 = "imagecreatefrom{$info['extension']}"; $func2 = "image{$info['extension']}"; break; default: return false; } $image = $func1($filename); if(!$image) return false; imagecopyresampled($image_p, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $OrigWidth, $OrigHeight); return ($func2($image_p, $copypath) ? true : false); } ?> |
||
Jack Finger Profil |
#4 · Zasláno: 14. 1. 2008, 07:27:35
díky, odpoledne se na to kouknu.
|
||
Jack Finger Profil |
#5 · Zasláno: 14. 1. 2008, 16:08:08
tak gd píše 2.0 or higher a PHP 5.2.3. více info zde.
teď jdu testovat ten tvůj upgrade. |
||
Jack Finger Profil |
#6 · Zasláno: 14. 1. 2008, 19:29:50
tak po asi 2 hodinách vychytávání různých chybek a testování mi to funguje krásně pro JP(E)G, GIF i PNG. kód:
// --- Resize() --- function Resize($filename, $copypath, $MaxWidth, $MaxHeight){ list($OrigWidth, $OrigHeight) = getimagesize($filename); if ($MaxWidth == 0) $MaxWidth = $OrigWidth; if ($MaxHeight == 0) $MaxHeight = $OrigHeight; $pw = $OrigWidth / $MaxWidth; $ph = $OrigHeight / $MaxHeight; if ($pw > $ph) $p = $pw; else $p = $ph; if ($p < 1) $p = 1; $NewWidth = (int)$OrigWidth / $p; $NewHeight = (int)$OrigHeight / $p; $image_p = imagecreatetruecolor($NewWidth, $NewHeight); $info = pathinfo($copypath); switch($info['extension']){ case 'jpeg': case 'jpg': $func1 = "imagecreatefromjpeg"; $func2 = "imagejpeg"; break; case 'png': case 'gif': $func1 = "imagecreatefrom{$info['extension']}"; $func2 = "image{$info['extension']}"; break; default: return false; break; } $image = $func1($filename); if(!$image){ return false; } imagecopyresampled($image_p, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $OrigWidth, $OrigHeight); if($func2($image_p, $copypath)){ return true; } else { return false; } } widlakovi moc díky za pomoc. |
||
Anonymní Profil * |
#7 · Zasláno: 14. 1. 2008, 22:27:36
snad jen ještě zda se dá nějak nastavit, aby to dalo jako pozadí průhlednou barvu ("vrstvu") a jak to aspoň zhruba zakomponovat do tohoto skriptu..?
JF |
||
widlak Profil |
#8 · Zasláno: 15. 1. 2008, 15:23:44
snad jen ještě zda se dá nějak nastavit, aby to dalo jako pozadí průhlednou barvu ("vrstvu") a jak to aspoň zhruba zakomponovat do tohoto skriptu..?
JF http://www.php.net/manual/en/function.imagecolortransparent.php |
||
Časová prodleva: 16 let
|
0