Autor Zpráva
Jack Finger
Profil
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
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
... 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
díky, odpoledne se na to kouknu.
Jack Finger
Profil
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
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 *
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
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

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