IRCForumları - IRC ve mIRC Kullanıcılarının Buluşma Noktası
  reklamver

>
+
Etiketlenen Kullanıcılar

 
 
LinkBack Seçenekler Arama Stil
Prev önceki Mesaj   sonraki Mesaj Next
Alt 07 Kasım 2014, 06:28   #1
Çevrimdışı
Kullanıcıların profil bilgileri misafirlere kapatılmıştır.
IF Ticaret Sayısı: (0)
IF Ticaret Yüzdesi:(%)
PHP image resize class




PHP Kod:   Kodu kopyalamak için üzerine çift tıklayın!
<?php
//image class by Reinis Veips
//requires at least GD2

class Image {
var 
$imageType;
var 
$imageExtension;
var 
$imageHandle;
var 
$originalPath;
var 
$jpegQuality=100;
function 
__construct($imgpath=false) {
if (
$imgpath$this->openImage($imgpath);
}
function 
openImage($imgpath) {
if (!
file_exists($imgpath)) throw new Exception('File doesn\'t exist!');
$imageData=getimagesize($imgpath);
if (!
$imageData) {
throw new 
Exception('Unknown image format!');
} else {
$this->imageType=$imageData[2];
switch (
$this->imageType) {
case 
IMAGETYPE_GIF:
$this->imageHandle=imagecreatefromgif($imgpath);
$this->imageExtension='gif';
break;
case 
IMAGETYPE_PNG:
$this->imageHandle=imagecreatefrompng($imgpath);
$this->imageExtension='png';
break;
case 
IMAGETYPE_JPEG:
$this->imageHandle=imagecreatefromjpeg($imgpath);
$this->imageExtension='jpeg';
break;
case 
IMAGETYPE_BMP:
$this->imageHandle=imagecreatefrombmp($imgpath);
$this->imageExtension='bmp';
break;
default: throw new 
Exception('Unknown image format!');
}
$this->originalPath=$imgpath;
}
}

function 
saveImage($imgpath=false,$type=false) {
if (!
$imgpath$imgpath=$this->originalPath;
if (!
$type) {
$ext=strtolower(substr($imgpathstrrpos($imgpath'.') + 1));
switch (
$ext) {
case 
'jpeg':
case 
'jpg':
$type=IMAGETYPE_JPEG;
break;
case 
'bmp':
$type=IMAGETYPE_BMP;
break;
case 
'gif':
$type=IMAGETYPE_GIF;
break;
case 
'png':
$type=IMAGETYPE_PNG;
default:
$type=IMAGETYPE_JPEG;
}
}

switch (
$type) {
case 
IMAGETYPE_JPEG:
return 
imagejpeg($this->imageHandle,$imgpath,$this->jpegQuality);
break;
case 
IMAGETYPE_GIF:
return 
imagegif($this->imageHandle,$imgpath);
break;
case 
IMAGETYPE_PNG:
return 
imagepng($this->imageHandle,$imgpath);
break;
case 
IMAGETYPE_BMP:
return 
imagebmp($this->imageHandle,$imgpath);
break;
default:
return 
imagejpeg($this->imageHandle,$imgpath);
}
}

function 
resizeImage($maxwidth,$maxheight,$preserveAspect=true) {
//function resizes an image
//it doesn't enlarge the image
$width=imagesx($this->imageHandle);
$height=imagesy($this->imageHandle);
if (
$width>$maxwidth && $height>$maxheight) {
$oldprop=round($width/$height,2);
$newprop=round($maxwidth/$maxheight,2);
$preserveAspectx=round($width/$maxwidth,2);
$preserveAspecty=round($height/$maxheight,2);

if (
$preserveAspect)
{
if (
$preserveAspectx<$preserveAspecty)
{
$newwidth=$width/($height/$maxheight);
$newheight=$maxheight;
}
else 
{
$newwidth=$maxwidth;
$newheight=$height/($width/$maxwidth);
}

$dest=imagecreatetruecolor($newwidth,$newheight);
if (
imagecopyresampled($dest,$this->imageHandle,0,0,0,0,$newwidth,$newheight,$width,$height)==false) throw new Exception('Couldn\'t resize image!');
}
else
{
$dest=imagecreatetruecolor($maxwidth,$maxheight);
if (
imagecopyresampled($dest,$this->imageHandle,0,0,0,0,$maxwidth,$maxheight,$width,$height)==false) throw new Exception('Couldn\'t resize image!') ;
}
$this->imageHandle=$dest;
}
}

function 
outputImage() {
//function outputs all the headers and image data
// var_dump($this);
header('Content-type: '.image_type_to_mime_type($this->imageType));
switch (
$this->imageType) {
case 
IMAGETYPE_GIF:
return 
imagegif($this->imageHandle);
break;
case 
IMAGETYPE_JPEG:
return 
imagejpeg($this->imageHandle,'',$this->jpegQuality);
break;
case 
IMAGETYPE_BMP:
return 
imagebmp($this->imageHandle);
break;
case 
IMAGETYPE_PNG:
return 
imagepng($this->imageHandle);
break;
}
}
}



/*
ÖRNEK:
try {
$img= new Image('C:\logo4.jpg');
$img->resizeImage(50,30);
$img->outputImage();
} catch ( Exception $e) {
echo $e;
}
*/
?>


__________________
Bu kez pek bir afili yalnızlık, ağlatan bir kadın kadar düşman. Ağzı bozuk üstelik bırakmıyor acıtmadan.
 
Alıntı ile Cevapla

 

Etiketler
class, image, php, resize


Konuyu Toplam 1 Üye okuyor. (0 Kayıtlı üye ve 1 Misafir)
 

Yetkileriniz
Konu Acma Yetkiniz Yok
Cevap Yazma Yetkiniz Yok
Eklenti Yükleme Yetkiniz Yok
Mesajınızı Değiştirme Yetkiniz Yok

BB code is Açık
Smileler Açık
[IMG] Kodları Açık
HTML-Kodu Kapalı
Trackbacks are Kapalı
Pingbacks are Açık
Refbacks are Açık


Benzer Konular
Konu Konuyu Başlatan Forum Cevaplar Son Mesaj
image resize ve crop class hAte PHP 0 16 Ekim 2014 15:06