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

IRCForumları - IRC ve mIRC Kullanıcılarının Buluşma Noktası (https://www.ircforumlari.net/)
-   PHP (https://www.ircforumlari.net/php/)
-   -   PHP image resize class (https://www.ircforumlari.net/php/640989-php-image-resize-class.html)

hAte 07 Kasım 2014 06:28

PHP image resize class
 
PHP- Kodu:

<?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;
}
*/
?>



Tüm Zamanlar GMT +3 Olarak Ayarlanmış. Şuanki Zaman: 11:53.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO
Copyright ©2004 - 2025 IRCForumlari.Net Sparhawk