GD Image resize functie
Auteur: Koen - 01 januari 2009 - 13:43 - Gekeurd door: Stijn - Hits: 8445 - Aantal punten: 5.00 (6 stemmen)
Uitleg
Met deze functie kan je gemakkelijk een afbeelding resizen zonder dat deze uitgerokken wordt.
Vergeet niet om het bestand voldoende chmod rechten te geven!
Veel plezier
Code:
<?php
/**
* ResizeImage
* @desc A function to easily resize images
*
* @author Koen <koen@sitemasters.be>
* @version 1.2
*
* @param string $sImage The image you would like to have resized
* @param int $iMaxWidth The maximum width [Optional, default: 600]
* @param int $iMaxHeight The maximum height [Optional, default: 600]
*
* @return False on failure, Array(name, mime, width, height) on succeed.
*
*
* Changelog:
* 10-01-2009
* Added support for "image/pjepg" mime type. (Internet Explorer uses this.)
* 24-01-2009
* Fastened script.
*/
function ResizeImage($sImage, $iMaxWidth=600, $iMaxHeight=600)
{
if($aSize = @getimagesize($sImage))
{
list($iOrigWidth, $iOrigHeight) = $aSize;
$sMimeType = $aSize['mime'];
$rResized = null;
switch($sMimeType)
{
case 'image/jpeg':
case 'image/pjpeg':
case 'image/jpg':
$rResized = imagecreatefromjpeg($sImage);
break;
case 'image/gif':
$rResized = imagecreatefromgif($sImage);
break;
case 'image/png':
case 'image/x-png':
$rResized = imagecreatefrompng($sImage);
break;
default:
return false;
}
if(isset($iOrigWidth, $iOrigHeight))
{
if($iOrigWidth <= $iMaxWidth && $iOrigHeight <= $iMaxHeight)
{
$iNewWidth = $iOrigWidth;
$iNewHeight = $iOrigHeight;
} else
{
$iOrigRatio = $iOrigWidth / $iOrigHeight;
if(($iMaxWidth/$iMaxHeight) > $iOrigRatio)
{
$iNewWidth = $iMaxHeight * $iOrigRatio;
$iNewHeight = $iMaxHeight;
} else
{
$iNewHeight = $iMaxWidth / $iOrigRatio;
$iNewWidth = $iMaxWidth;
}
}
$rResampledImage = imagecreatetruecolor($iNewWidth, $iNewHeight);
imagecopyresampled($rResampledImage, $rResized, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOrigWidth, $iOrigHeight);
unlink($sImage);
switch($sMimeType)
{
case 'image/jpeg':
case 'image/pjpeg':
case 'image/jpg':
imagejpeg($rResampledImage, $sImage, 100);
break;
case 'image/gif':
imagegif($rResampledImage, $sImage);
break;
case 'image/png':
case 'image/x-png':
imagepng($rResampledImage, $sImage);
break;
default:
return false;
}
@chmod($sImage, 0777);
return array( "name" => $sImage,
"mime" => $sMimeType,
"width" => $iNewWidth,
"height" => $iNewHeight
);
} else
{
return false;
}
} else
{
return false;
}
}
// Voorbeeld:
echo '<pre>', print_r(resize('picture.png')), '</pre>';
// Return Value:
Array
(
[name] => picture.png
[mime] => image/png
[width] => 436
[height] => 600
)
?>
<?php
/**
* ResizeImage
* @desc A function to easily resize images
*
* @author Koen <koen@sitemasters.be>
* @version 1.2
*
* @param string $sImage The image you would like to have resized
* @param int $iMaxWidth The maximum width [Optional, default: 600]
* @param int $iMaxHeight The maximum height [Optional, default: 600]
*
* @return False on failure, Array(name, mime, width, height) on succeed.
*
*
* Changelog:
* 10-01-2009
* Added support for "image/pjepg" mime type. (Internet Explorer uses this.)
* 24-01-2009
* Fastened script.
*/
function ResizeImage( $sImage , $iMaxWidth = 600 , $iMaxHeight = 600 )
{
{
list ( $iOrigWidth , $iOrigHeight ) = $aSize ; $sMimeType = $aSize [ 'mime' ] ;
$rResized = null ;
switch ( $sMimeType )
{
case 'image/jpeg' :
case 'image/pjpeg' :
case 'image/jpg' :
$rResized = imagecreatefromjpeg( $sImage ) ;
break ;
case 'image/gif' :
$rResized = imagecreatefromgif( $sImage ) ;
break ;
case 'image/png' :
case 'image/x-png' :
$rResized = imagecreatefrompng( $sImage ) ;
break ;
default :
return false ;
}
if ( isset ( $iOrigWidth , $iOrigHeight ) ) {
if ( $iOrigWidth <= $iMaxWidth && $iOrigHeight <= $iMaxHeight )
{
$iNewWidth = $iOrigWidth ;
$iNewHeight = $iOrigHeight ;
} else
{
$iOrigRatio = $iOrigWidth / $iOrigHeight ;
if ( ( $iMaxWidth / $iMaxHeight ) > $iOrigRatio )
{
$iNewWidth = $iMaxHeight * $iOrigRatio ;
$iNewHeight = $iMaxHeight ;
} else
{
$iNewHeight = $iMaxWidth / $iOrigRatio ;
$iNewWidth = $iMaxWidth ;
}
}
$rResampledImage = imagecreatetruecolor( $iNewWidth , $iNewHeight ) ;
imagecopyresampled( $rResampledImage , $rResized , 0 , 0 , 0 , 0 , $iNewWidth , $iNewHeight , $iOrigWidth , $iOrigHeight ) ;
switch ( $sMimeType )
{
case 'image/jpeg' :
case 'image/pjpeg' :
case 'image/jpg' :
imagejpeg( $rResampledImage , $sImage , 100 ) ;
break ;
case 'image/gif' :
imagegif( $rResampledImage , $sImage ) ;
break ;
case 'image/png' :
case 'image/x-png' :
imagepng( $rResampledImage , $sImage ) ;
break ;
default :
return false ;
}
return array ( "name" => $sImage , "mime" => $sMimeType ,
"width" => $iNewWidth ,
"height" => $iNewHeight
) ;
} else
{
return false ;
}
} else
{
return false ;
}
}
// Voorbeeld:
echo '<pre>' , print_r ( resize
( 'picture.png' ) ) , '</pre>' ;
// Return Value:
(
[ name] => picture. png
[ mime] => image/ png
[ width] => 436
[ height] => 600
)
?>
Download code (.txt)
Stemmen
Niet ingelogd.