А можно классом воспользоваться....
| Код | <? class ImaSize { var $image = 0;
function ImaSize($img='', $typ='') { if($this->image) @ImageDestroy($this->image); if($img!='') { $typ = strtolower($typ); if(($typ == 'string') || (!@file_exists($img))) $this->loadFromString($img); else $this->loadFromFile($img, $typ); } }
function loadFromString($strng) { if($this->image) @ImageDestroy($this->image); return ($this->image = @ImageCreateFromString($strng)); }
function loadFromFile($fn, $typ='') { if($this->image) @ImageDestroy($this->image); if(!@file_exists($fn)) $this->image = 0; else { if(($typ=='jpeg')||($typ=='jpg')) $this->image = @ImageCreateFromJPEG($fn); elseif($typ=='png') $this->image = @ImageCreateFromPNG($fn); elseif($typ=='gif') $this->image = @ImageCreateFromGIF($fn); if(!$this->image) { $funcar = @get_extension_funcs('gd'); foreach($funcar as $func) { if($this->image) break; $func = strtolower($func); if(substr($func,0,15) != 'imagecreatefrom') continue; if(substr($func,15) == 'string') continue; $this->image = @$func($fn); } } } return $this->image; }
function sizeTo($x, $y, $clr=0) { if((!$this->image) || (($x*$y) < 1)) return 0; if(sizeof($clr) != 3) $clr = array(255,255,255); $img = @ImageCreate($x, $y); $bg = @ImageColorAllocate($img, $clr[0], $clr[1], $clr[2]); $sx = @ImageSX($this->image); $sy = @ImageSY($this->image); $dw = (float)($x / $sx); $dh = (float)($y / $sy); $dd = ($dw < $dh) ? $dw : $dh; @ImageCopyResized($img, $this->image, (int)(($x-($sx*$dd))/2), (int)(($y-($sy*$dd))/2), 0, 0, (int)($sx*$dd)+1, (int)($sy*$dd)+1, $sx, $sy); @ImageDestroy($this->image); return ($this->image = $img); } }; ?>
| |