Szerző Téma: class ThumbMaker  (Megtekintve 613 alkalommal)

0 Felhasználó és 1 vendég van a témában

Elérhető Keveqiah

  • Globális moderátor
  • Teljes tag
  • *****
  • Hozzászólások: 2.332
  • Kisb
    • Keveqiah
Re: class ThumbMaker
« Válasz #11 Dátum: 2011. január 11., 16:48:16 »
Tudom, nem sokáig váratott magára a v.2, de unatkoztam is, plusz ahogy a v.1-et beépítettem a weblapomba, rájöttem, hogy a linkelt képeket nem jeleníti meg.
Vagyis, ha egy képet direkt linkbe adunk meg, nem csinált vele semmit. Igaz, megszegeltem, hogy jó legyen, de utána gondolkoztam, hogy össze lehet ezt rakni egyszerűbben is.

Ezért is szeretném a jártasabb szakik véleményét kikérni, hogy esetlegesen mi felesleges a kódban vagy mit lehetne összevonni benne.
A használata megegyezik lentebb már publikált verzióval.

Kód: PHP
  1. class ThumbMaker
  2. {
  3.         private $options = array('file' => null, 'name' => null, 'type' => null,
  4.                                 'orig_w' => 0, 'orig_h' => 0, 'width' => 0,     'height' => 0,
  5.                                 'pos_X' => 0, 'pos_Y' => 0,     'resize' => true,
  6.                                 'savepath' => false, 'savename' => null);
  7.  
  8.         function __construct($arg, $path = false)
  9.         {
  10.                 $this->options['file'] = (string)$arg['f'];
  11.  
  12.                 if( $this->CheckFile($this->options['file']) )
  13.                 {
  14.                         $this->SetOptions($arg, $path);
  15.                         if( !$this->CheckThumb() )
  16.                                 $this->Make();
  17.                         else
  18.                                 $this->Show( '', $this->options['savepath']. '/' .$this->options['savename'] );
  19.                 }
  20.                 else
  21.                         $this->Error($arg['w'], $arg['h']);
  22.         }
  23.  
  24.         function CheckFile($file)
  25.         {
  26.                 if(file_exists($file))
  27.                 {
  28.                         $info = pathinfo($this->options['file']);
  29.                         $this->options['type'] = $info['extension'];
  30.                         $this->options['name'] = $info['filename'];
  31.                         list($this->options['orig_w'], $this->options['orig_h']) = getimagesize($file);
  32.  
  33.                         return true;
  34.         }
  35.                 elseif(@fopen($file, "r"))
  36.                 {
  37.                         $info = explode("/", $this->options['file']);
  38.                         $ext = explode(".", end($info) );
  39.                         $this->options['type'] = end($ext);
  40.                         $this->options['name'] = $ext[0];
  41.                         list($this->options['orig_w'], $this->options['orig_h']) = getimagesize($file);
  42.  
  43.                         return true;
  44.                 }
  45.                 else
  46.                         return false;
  47.         }
  48.  
  49.         function SetOptions($arg, $path)
  50.         {
  51.                 if(isset($arg['w']))
  52.                         $this->options['width'] = (int)$arg['w'];
  53.                 if(isset($arg['h']))
  54.                         $this->options['height'] = (int)$arg['h'];
  55.  
  56.                 if(isset($arg['p']))
  57.                 {
  58.                         $pos = explode(',', $arg['p']);
  59.                         $this->options['pos_X'] = $pos[0];
  60.                         $this->options['pos_Y'] = $pos[0];
  61.                 }
  62.                 if(isset($arg['r']))
  63.                         $this->options['resize'] = false;
  64.  
  65.                 if(isset($path))
  66.                         $this->options['savepath'] = $path;
  67.  
  68.                 $this->options['savename'] = md5( implode($this->options) ). '.' .$this->options['type'];
  69.         }
  70.  
  71.         function CheckThumb()
  72.         {
  73.                 if(file_exists($this->options['savepath']. '/' .$this->options['savename']))
  74.                         return true;
  75.                 else
  76.                         return false;
  77.         }
  78.  
  79.         function Make()
  80.         {
  81.                 $img = $this->CreateImg($this->options['file']);
  82.  
  83.                 $percent = $this->options['width'] / $this->options['orig_w'];
  84.  
  85.                 $new['width'] = $this->options['orig_w'] * $percent;
  86.                 $new['height'] = $this->options['orig_h'] * $percent;
  87.                 $topX = 0; $topY = 0;
  88.  
  89.                 $new_img = imagecreatetruecolor($new['width'], $new['height']);
  90.  
  91.                 if( $this->options['pos_X']!=0 )
  92.                 {
  93.                         $topX = $this->options['pos_X'];
  94.                         $topY = $this->options['pos_Y'];
  95.                 }
  96.                 elseif( $this->options['orig_w'] <= $this->options['orig_h'] )
  97.                         $topY = $new['height'] * 0.3;
  98.  
  99.                 if($this->options['resize'])
  100.                 {
  101.                         imagecopyresampled($new_img, $img, 0, 0, $topX, $topY,
  102.                                 $new['width'], $new['height'],
  103.                                 $this->options['orig_w'], $this->options['orig_h']);
  104.                 }
  105.                 else
  106.                 {
  107.                         imagecopyresampled($new_img, $img, 0, 0, $topX, $topY,
  108.                                 $this->options['orig_w'], $this->options['orig_h'],
  109.                                 $this->options['orig_w'], $this->options['orig_h']);
  110.                 }
  111.  
  112.                 $this->Show($new_img, false, $this->options['savepath'].'/'.$this->options['savename']);
  113.         }
  114.  
  115.         function CreateImg($file)
  116.         {
  117.                 switch($this->options['type'])
  118.                 {
  119.                         case 'jpg'      :
  120.                         case 'jpeg'     :       return imagecreatefromjpeg($file);
  121.                         case 'png'      :       return imagecreatefrompng($file);
  122.                         case 'gif'      :       return imagecreatefromgif($file);
  123.                 }
  124.         }
  125.  
  126.         function Show($img, $file = false, $save = null)
  127.         {
  128.                 if($file)
  129.                         $img = $this->CreateImg($file);
  130.  
  131.                 switch($this->options['type'])
  132.                 {
  133.                     case 'jpg' :
  134.                         case 'jpeg':header('Content-type: image/jpeg');
  135.                                 imagejpeg($img, $save, 90);
  136.                                 header('Content-Length: ' . filesize($save));
  137.                                 break;
  138.                         case 'gif' :header('Content-type: image/gif');
  139.                                 imagegif($img, $save);
  140.                                 header('Content-Length: ' . filesize($save));
  141.                                 break;
  142.                         case 'png' :header('Content-type: image/png');
  143.                                 imagepng($img, $save);
  144.                                 header('Content-Length: ' . filesize($save));
  145.                                 break;
  146.                 }
  147.                 ob_clean();
  148.                 flush();
  149.                 readfile($save);
  150.                 imagedestroy($img);
  151.         }
  152.  
  153.         function Error($width, $height)
  154.         {
  155.                 if(!isset($height))
  156.                         $height = $width / 2;
  157.  
  158.                 $img = imagecreate($width, $height);
  159.                 $background_color = imagecolorallocate($img, 255, 255, 255);
  160.                 $text_color = imagecolorallocate($img, 255, 0, 0);
  161.                 $textSize = 5;
  162.                 if($width<=50)
  163.                 {
  164.                         $textSize = 2;
  165.                         imagestring($img, $textSize, 15, 5,  "Nem", $text_color);
  166.                         imagestring($img, $textSize, 1, 20,  "elérhető!", $text_color);
  167.                 }
  168.                 else
  169.                         imagestring($img, $textSize, 5, 5,  "Nem elérhető!", $text_color);
  170.  
  171.                 header('Content-type: image/jpeg');
  172.                 imagejpeg($img, null, 90);
  173.                 imagedestroy($img);
  174.         }
  175. }
  176.  
  177. new ThumbMaker($_GET, 'thumb');
  178.  

Használható közvetlen ill. direkt link is. (az elsődleges tesztek alapján távoli képeket is kezel )
Kód: HTML
  1. <img src="thumbv2.php?f=http://valami.hu/30.jpg&w=400" alt="" />
  2. <img src="thumbv2.php?f=images/30.jpg&w=400" alt="" />
  3.  
"Gonosz vagyok. Ez ugyebár nem hit dolga."
"Egy ember halála tragédia, ezrek halála statisztika."

Elérhető Keveqiah

  • Globális moderátor
  • Teljes tag
  • *****
  • Hozzászólások: 2.332
  • Kisb
    • Keveqiah
Re: class ThumbMaker
« Válasz #10 Dátum: 2011. január 11., 12:41:45 »
"Gonosz vagyok. Ez ugyebár nem hit dolga."
"Egy ember halála tragédia, ezrek halála statisztika."

Elérhető Keveqiah

  • Globális moderátor
  • Teljes tag
  • *****
  • Hozzászólások: 2.332
  • Kisb
    • Keveqiah
Re: class ThumbMaker
« Válasz #9 Dátum: 2011. január 11., 10:32:51 »
 O0

javítva..
Kód: PHP
  1. // ezt a részt
  2.         $exp = explode(".", $this->options['file']);
  3.         $this->options['type'] = strtolower(end($exp));
  4.  
  5.         $file = explode("/", $exp[0]);
  6.         $this->thumbName = end($file).'-'.$this->options['width'].$this->options['height'].$this->options['resize'];
  7.         $this->thumbName .= $this->options['poz_X'].$this->options['poz_Y'].'.'.$this->options['type'];
  8.  
  9. // csere erre..
  10.         $file = pathinfo($this->options['file']);
  11.         $this->options['type'] = $file['extension'];
  12.         $this->thumbName = $file['filename'].'-'.$this->options['width'].$this->options['height'].$this->options['resize'];
  13.         $this->thumbName .= $this->options['poz_X'].$this->options['poz_Y'].'.'.$this->options['type'];
  14.  
"Gonosz vagyok. Ez ugyebár nem hit dolga."
"Egy ember halála tragédia, ezrek halála statisztika."

Nem elérhető Rimelek

  • Adminisztrátor
  • Őstag
  • *****
  • Hozzászólások: 8.942
    • rimelek.hu
Re: class ThumbMaker
« Válasz #8 Dátum: 2011. január 11., 10:08:45 »
megj.: A szerkesztőmben lévő kód rendezése megfelelő, a fórumban megjelenő kód kicsit szétesik és lusta vagyok kiigazítani.
Következetesen szóközt, vagy tabot használj behúzásra, akkor itt is jó lesz :)

Jó cucc egyébként, csak egy tipp:
Fájlnév, kiterjesztés, és útvonal meghatározásához talán jobb a pathinfo()
Egyszerűen létezem, és lézengek a Földön,
mint idióta szellem, kinek kastélya a börtön.

Elérhető Keveqiah

  • Globális moderátor
  • Teljes tag
  • *****
  • Hozzászólások: 2.332
  • Kisb
    • Keveqiah
Re: class ThumbMaker
« Válasz #7 Dátum: 2011. január 11., 09:49:31 »
Bővített változat, a generált képeket tárolja a megadott paraméterek listájával és a fájl névvel, ami alapján a későbbiekben azonosítja a bélyegképet, így a további letöltések során már nem generál új képet.

Demo: http://keveqiah.hu/projects/thumbmaker/

Kód: PHP
  1. class ThumbMaker
  2. {
  3.         private $options, $stop, $orig, $Im;
  4.         private $thumbName;
  5.         private $thumbDir = "thumb"; // A bélyegképek mentésére szánt könyvtár
  6.  
  7.         function __construct($args)
  8.         {
  9.                 $this->stop = false;
  10.                 if(isset($args['p']))
  11.                         $xy = explode(',', $args['p']);
  12.  
  13.                 $this->options = array('file' => (string)$args['f'],
  14.                                                  'width' => (int)$args['w'],
  15.                                                  'height' => isset($args['h']) ? (int)$args['h'] : null,
  16.                                                  'resize' => isset($args['r']) ? 1 : 0,
  17.                                                  'poz_X' => isset($xy) ? $xy[0] : 0,
  18.                                                  'poz_Y' => isset($xy) ? $xy[1] : 0
  19.                                                  );
  20.  
  21.                 $exp = explode(".", $this->options['file']);
  22.         $this->options['type'] = strtolower(end($exp));
  23.  
  24.                 $file = explode("/", $exp[0]);
  25.                 $this->thumbName = end($file).'-'.$this->options['width'].$this->options['height'].$this->options['resize'];
  26.                 $this->thumbName .= $this->options['poz_X'].$this->options['poz_Y'].'.'.$this->options['type'];
  27.  
  28.         $check = $this->ChechThumbnail();
  29.  
  30.                 if(!$this->stop)
  31.                         if($check==1)
  32.                                 $this->ShowThumbnail($this->options['type'], $this->Im);
  33.                         elseif($check==2)
  34.                                 $this->MakeThumbnail();
  35.                         else
  36.                                 $this->ErrorThumbnail();
  37.                 else
  38.                         $this->ErrorThumbnail();
  39.         }
  40.  
  41.         function ChechThumbnail()
  42.         {
  43.                 if(file_exists($this->options['file']))
  44.                 {
  45.                         switch($this->options['type'])
  46.                         {
  47.                                 case 'jpg'      :
  48.                                 case 'jpeg'     :       $this->Im = imagecreatefromjpeg($this->options['file']);        break;
  49.                                 case 'png'      :       $this->Im = imagecreatefrompng($this->options['file']); break;
  50.                                 case 'gif'      :       $this->Im = imagecreatefromgif($this->options['file']); break;
  51.                                 default         :       $this->stop = true; break;
  52.                         }
  53.                         $this->orig = array('width' => imagesx($this->Im), 'height' => imagesy($this->Im));
  54.                         return(2);
  55.                 }
  56.                 else
  57.                 {
  58.                         $this->stop = true;
  59.                         return(3);
  60.                 }
  61.  
  62.             if(file_exists($this->thumbDir.'/'.$this->thumbName))
  63.                 {
  64.                         switch($this->options['type'])
  65.                         {
  66.                                 case 'jpg'      :
  67.                                 case 'jpeg'     :       $this->Im = imagecreatefromjpeg($this->thumbDir.'/'.$this->thumbName);  break;
  68.                                 case 'png'      :       $this->Im = imagecreatefrompng($this->thumbDir.'/'.$this->thumbName);   break;
  69.                                 case 'gif'      :       $this->Im = imagecreatefromgif($this->thumbDir.'/'.$this->thumbName);   break;
  70.                                 default         :       $this->stop = true; break;
  71.                         }
  72.                         return(1);
  73.                 }
  74.                 else
  75.                 {
  76.                         $this->stop = true;
  77.                         return(3);
  78.                 }
  79.         }
  80.         function MakeThumbnail()
  81.         {
  82.             $percent = ( $this->options['width'] / $this->orig['width'] );
  83.  
  84.             // ha nincs magassági méret
  85.             if(!$this->options['height'])
  86.             {
  87.                         $new['width'] = $this->orig['width'] * $percent;
  88.                     $new['height'] = $this->orig['height'] * $percent;
  89.                         $topY = 0;
  90.  
  91.                     $newIm = imagecreatetruecolor($new['width'], $new['height']);
  92.             }
  93.             else
  94.             {
  95.                 $new['width'] = ($this->orig['width'] * ($percent + 0.02));
  96.                 $new['height'] = ($this->orig['height'] * ($percent + 0.02));
  97.  
  98.                     if($this->orig['width'] >= $this->orig['height'])
  99.                                 $topY = 0;
  100.                     else
  101.                         $topY = $new['height'] * 0.3;
  102.  
  103.                     $newIm = imagecreatetruecolor($this->options['width'], $this->options['height']);
  104.             }
  105.  
  106.             if($this->options['resize']==0)
  107.                     imagecopyresampled($newIm, $this->Im, 0, 0, 0, $topY, $new['width'], $new['height'], $this->orig['width'], $this->orig['height']);
  108.                 else
  109.                         imagecopyresampled($newIm, $this->Im, 0, 0, $this->options['poz_X'], $this->options['poz_Y'], $this->orig['width'], $this->orig['height'], $this->orig['width'], $this->orig['height']);
  110.  
  111.                 $savePath = $this->thumbDir.'/'.$this->thumbName;
  112.  
  113.                 $this->SaveThumbnail($this->options['type'], $newIm, $savePath);
  114.         }
  115.  
  116.         function ErrorThumbnail()
  117.         {
  118.                 if(!$this->options['height'])
  119.                         $this->options['height'] = $this->options['width'];
  120.  
  121.                 $Im = imagecreate($this->options['width'], $this->options['height']);
  122.                 $background_color = imagecolorallocate($Im, 255, 255, 255);
  123.                 $text_color = imagecolorallocate($Im, 255, 0, 0);
  124.                 $textSize = 5;
  125.                 if($this->options['width']<=50)
  126.                 {
  127.                         $textSize = 2;
  128.                         imagestring($Im, $textSize, 15, 5,  "Nem", $text_color);
  129.                         imagestring($Im, $textSize, 1, 20,  "elérhető!", $text_color);
  130.                 }
  131.                 else
  132.                         imagestring($Im, $textSize, 5, 5,  "Nem elérhető!", $text_color);
  133.  
  134.                 $this->ShowThumbnail('jpg', $Im);
  135.         }
  136.  
  137.         function ShowThumbnail($type, $img)
  138.         {
  139.                 switch($type)
  140.                 {
  141.                     case 'jpg' :
  142.                         case 'jpeg': header('Content-type: image/jpeg');
  143.                                                  imagejpeg($img, null, 90);
  144.                                                  break;
  145.                         case 'gif' : header('Content-type: image/gif');
  146.                                                  imagegif($img, null);
  147.                                                  break;
  148.                         case 'png' : header('Content-type: image/png');
  149.                                                  imagepng($img, null);
  150.                                                  break;
  151.                 }
  152.                 imagedestroy($this->Im);
  153.                 imagedestroy($img);
  154.         }
  155.  
  156.         function SaveThumbnail($type, $img, $save = null)
  157.         {
  158.                 switch($type)
  159.                 {
  160.                     case 'jpg' :
  161.                         case 'jpeg': imagejpeg($img, $save, 90);
  162.                                                  header("Content-type: image/jpeg");
  163.                                                  header('Content-Length: ' . filesize($save));
  164.  
  165.                                                  break;
  166.                         case 'gif' : imagegif($img, $save);
  167.                                                  header('Content-type: image/gif');
  168.                                                  header('Content-Length: ' . filesize($save));
  169.  
  170.                                                  break;
  171.                         case 'png' : imagepng($img, $save);
  172.                                                  header('Content-type: image/png');
  173.                                                  header('Content-Length: ' . filesize($save));
  174.                                                  break;
  175.                 }
  176.             ob_clean();
  177.             flush();
  178.             readfile($save);
  179.         }
  180. }
  181. new ThumbMaker($_GET);
  182.  

megj.: A szerkesztőmben lévő kód rendezése megfelelő, a fórumban megjelenő kód kicsit szétesik és lusta vagyok kiigazítani.
"Gonosz vagyok. Ez ugyebár nem hit dolga."
"Egy ember halála tragédia, ezrek halála statisztika."

Elérhető Keveqiah

  • Globális moderátor
  • Teljes tag
  • *****
  • Hozzászólások: 2.332
  • Kisb
    • Keveqiah
Re: class ThumbMaker
« Válasz #6 Dátum: 2011. január 10., 19:49:29 »
WP is elmenti, az eredeti mellé, de sokszor nem úgy használja, mint kéne. Nem kimondottan CMS-hez szánom a szkriptet, csak úgy anglobal. A saját WP témámnál is használok egy ilyen szkriptet, de mint látható is, nem tökéletes. Ezért is építettem bele a kivágás funkciót.
A mentés pedig egy külön (meghatározható) mappában lenne tárolva, egyedi névvel és az osztály kapna egy új metódust, ami csak visszatöltené a képet, ha már van mentett verziója.

ui. Kétlem, hogy nálad jobb szkirpteket tudnék írni. Mégiscsak te tanulod ezt intenzíven. :)
"Gonosz vagyok. Ez ugyebár nem hit dolga."
"Egy ember halála tragédia, ezrek halála statisztika."

Nem elérhető Rimelek

  • Adminisztrátor
  • Őstag
  • *****
  • Hozzászólások: 8.942
    • rimelek.hu
Re: class ThumbMaker
« Válasz #5 Dátum: 2011. január 10., 18:25:03 »
Úgy értettem, szép, hogy kapásból kép formátumú kimenetet tud adni, de ha megcsinálod, hogy lementős verzióban is használható legyen, akkor nem az osztály dolga lesz a megjelenítés, hanem csak a lementésért felel. Onnantól a felhasználó (programozó) dolga, hogy a képeket hogy nevezi el. És ugyanolyan néven egy mappába lementheted a thumbnailt is. Majd megjelenítésnél elég linkelni mindkét verziót. Ami már a tárhelyen van. Így csak a generálás lesz idő. A megjelenítés már nem.

Én is hasonló osztályt írtam, csak cakephp komponensnek. Tud feltölteni, thumbnailt készíteni és mindezt különböző beállítások szerint. Még nem publikáltam.Remélem nem írsz jobbat az enyémnél :D Még átnézem és az egyetlen cakephp függő metódust kicserélem. Aztán majd én is megosztom.

Addig viszont ezt a lementős dolgot mindenképp tudom javasolni.
Elküldve:: 2011. Január 10.  18:24:01
Esetleg, ha a fájl név és paraméterek együttesét a fájlnévbe mentem és ezzel tudnám azonosítani.
Viszont ez sem rossz ötlet. WordPress is használ valami ilyesmit. De az is lementi a fájlokat. ( tudtommal. De nem néztem utána )
Egyszerűen létezem, és lézengek a Földön,
mint idióta szellem, kinek kastélya a börtön.

Elérhető Keveqiah

  • Globális moderátor
  • Teljes tag
  • *****
  • Hozzászólások: 2.332
  • Kisb
    • Keveqiah
Re: class ThumbMaker
« Válasz #4 Dátum: 2011. január 10., 18:16:02 »
Nem is rossz ötlet. :)
Akkor az elképzelés az, hogy legyen egy ennek megfelelő mappa, ahova mentem az összes lekért változatot? Ez a könnyebbik módja. Hogyan oldjam meg, hogy meg is találja? :)
Esetleg, ha a fájl név és paraméterek együttesét a fájlnévbe mentem és ezzel tudnám azonosítani.
Egyéb észrevétel a mentéssel kapcsolatban? :)
"Gonosz vagyok. Ez ugyebár nem hit dolga."
"Egy ember halála tragédia, ezrek halála statisztika."

Nem elérhető Rimelek

  • Adminisztrátor
  • Őstag
  • *****
  • Hozzászólások: 8.942
    • rimelek.hu
Re: class ThumbMaker
« Válasz #3 Dátum: 2011. január 10., 18:08:20 »
Ha az elküldés alatt a szervertől kliensnek küldést érted, azaz letöltést, akkor igen. De ha sok ilyen kép van, az se túl gyors. Én próbáltam. Ezért javaslom a lementés lehetőségét is. Nem nagy változtatás, mert csak egy plusz paraméter az image* függvényekben, de hasznos :)
Egyszerűen létezem, és lézengek a Földön,
mint idióta szellem, kinek kastélya a börtön.

Elérhető tomi6230i

  • Törzstag
  • **
  • Hozzászólások: 344
Re: class ThumbMaker
« Válasz #2 Dátum: 2011. január 10., 18:02:45 »
A lényege annyi, hogy kisebb fájlméretű képet kelljen elküldeni?

Elérhető Keveqiah

  • Globális moderátor
  • Teljes tag
  • *****
  • Hozzászólások: 2.332
  • Kisb
    • Keveqiah
class ThumbMaker
« Válasz #1 Dátum: 2011. január 10., 14:44:46 »
Idézet
Igen, megint felvetődhet a kérdés, minek még egy ilyen hülyeség, mikor ilyenekkel van tele az internet. A válaszom egyszerű. Gyakorlás, gyakorlás és nem utolsó sorban, ha valaki saját maga ír meg egy kódot, tudja hogyan és miként is működik. Gyorsan képes javítani az esetleges hibákat vagy épp fejleszteni, bővíteni és gyorsan képes. Én is ilyen elhatározás mellett készítettem ezt a kis képkezelő osztályt, nomeg azért, mert épp egy ilyenre volt szükségem.

Demó: (hogy mit is tud)
http://keveqiah.hu/projects/thumbmaker/

Kód: PHP
  1. class ThumbMaker
  2. {
  3.         private $options, $stop, $orig, $Im;
  4.  
  5.         function __construct($args)
  6.         {
  7.                 $this->stop = false;
  8.                 if(isset($args['p']))
  9.                         $xy = explode(',', $args['p']);
  10.  
  11.                 $this->options = array('file' => (string)$args['f'],
  12.                                                  'width' => (int)$args['w'],
  13.                                                  'height' => isset($args['h']) ? (int)$args['h'] : null,
  14.                                                  'resize' => isset($args['r']) ? true : false,
  15.                                                  'poz_X' => isset($xy) ? $xy[0] : 0,
  16.                                                  'poz_Y' => isset($xy) ? $xy[1] : 0
  17.                                                  );
  18.  
  19.                 $exp = explode(".", $this->options['file']);
  20.         $this->options['type'] = strtolower(end($exp));
  21.  
  22.                 if(file_exists($this->options['file']))
  23.                 {
  24.                         switch($this->options['type'])
  25.                         {
  26.                                 case 'jpg'      :
  27.                                 case 'jpeg'     :       $this->Im = imagecreatefromjpeg($this->options['file']);        break;
  28.                                 case 'png'      :       $this->Im = imagecreatefrompng($this->options['file']); break;
  29.                                 case 'gif'      :       $this->Im = imagecreatefromgif($this->options['file']); break;
  30.                                 default         :       $this->stop = true; break;
  31.                         }
  32.                         $this->orig = array('width' => imagesx($this->Im), 'height' => imagesy($this->Im));
  33.                 }
  34.                 else
  35.                         $this->stop = true;
  36.  
  37.                 if(!$this->stop)
  38.                         $this->MakeThumbnail();
  39.                 else
  40.                         $this->ErrorThumbnail();
  41.         }
  42.  
  43.         function MakeThumbnail()
  44.         {
  45.             $percent = ( $this->options['width'] / $this->orig['width'] );
  46.  
  47.             // ha nincs magassági méret
  48.             if(!$this->options['height'])
  49.             {
  50.                         $new['width'] = $this->orig['width'] * $percent;
  51.                     $new['height'] = $this->orig['height'] * $percent;
  52.                         $topX = 0;
  53.  
  54.                     $newIm = imagecreatetruecolor($new['width'], $new['height']);
  55.             }
  56.             else
  57.             {
  58.                 $new['width'] = ($this->orig['width'] * ($percent + 0.02));
  59.                 $new['height'] = ($this->orig['height'] * ($percent + 0.02));
  60.  
  61.                     if($this->orig['width'] >= $this->orig['height'])
  62.                                 $topX = 0;
  63.                     else
  64.                         $topX = $new['height'] * 0.3;
  65.  
  66.                     $newIm = imagecreatetruecolor($this->options['width'], $this->options['height']);
  67.             }
  68.  
  69.             if(!$this->options['resize'])
  70.                     imagecopyresampled($newIm, $this->Im, 0, 0, 0, $topX, $new['width'], $new['height'], $this->orig['width'], $this->orig['height']);
  71.                 else
  72.                         imagecopyresampled($newIm, $this->Im, 0, 0, $this->options['poz_X'], $this->options['poz_Y'], $this->orig['width'], $this->orig['height'], $this->orig['width'], $this->orig['height']);
  73.  
  74.                 $this->ShowThumbnail($this->options['type'], $newIm);
  75.         }
  76.  
  77.         function ErrorThumbnail()
  78.         {
  79.                 if(!$this->options['height'])
  80.                         $this->options['height'] = $this->options['width'];
  81.  
  82.                 $Im = imagecreate($this->options['width'], $this->options['height']);
  83.                 $background_color = imagecolorallocate($Im, 255, 255, 255);
  84.                 $text_color = imagecolorallocate($Im, 255, 0, 0);
  85.                 $textSize = 5;
  86.                 if($this->options['width']<=50)
  87.                 {
  88.                         $textSize = 2;
  89.                         imagestring($Im, $textSize, 15, 5,  "Nem", $text_color);
  90.                         imagestring($Im, $textSize, 1, 20,  "elérhető!", $text_color);
  91.                 }
  92.                 else
  93.                         imagestring($Im, $textSize, 5, 5,  "Nem elérhető!", $text_color);
  94.  
  95.                 $this->ShowThumbnail('jpg', $Im);
  96.         }
  97.  
  98.         function ShowThumbnail($type, $img)
  99.         {
  100.                 switch($type)
  101.                 {
  102.                     case 'jpg' :
  103.                         case 'jpeg': header('Content-type: image/jpeg');
  104.                                                  imagejpeg($img, null, 90);
  105.                                                  break;
  106.                         case 'gif' : header('Content-type: image/gif');
  107.                                                  imagegif($img, null);
  108.                                                  break;
  109.                         case 'png' : header('Content-type: image/png');
  110.                                                  imagepng($img, null);
  111.                                                  break;
  112.                 }
  113.                 imagedestroy($this->Im);
  114.         }
  115. }
  116. new ThumbMaker($_GET);
  117.  

Paraméterek:
  • f : fájl elérése
  • w : szélesség (int)
  • h : magasság (int)
  • r : átméretezés (alap: ture / false)
  • p : pozíció eltolás (x, y) koordináta

Példa:

Kód: HTML
  1. <img src="thumb.php?f=images/30.jpg&w=400&h=200" alt="" />
  2. <img src="thumb.php?f=images/30.jpg&w=400&h=200&r=false&p=50,50" alt="" />
  3.  

ui. ha van valakinek ötlete, hogy mit lehet még bele írni, mint funkciót, az mondja bátran.
« Utoljára szerkesztve: 2011. január 10., 14:50:22 írta Keveqiah »
"Gonosz vagyok. Ez ugyebár nem hit dolga."
"Egy ember halála tragédia, ezrek halála statisztika."