resize.image.class.php
3 removals
94 lines
42 additions
131 lines
<?php
<?php
/**
/**
* @title J51 Thumbs Gallery
* @title J51 Thumbs Gallery
* @version 1.1
* @version 1.1
* @website http://www.joomla51.com
* @website http://www.joomla51.com
* @copyright Copyright (C) 2012 Joomla51.com. All rights reserved.
* @copyright Copyright (C) 2012 Joomla51.com. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* @license GNU/GPL, see LICENSE.php
Credits: Bit Repository
Credits: Bit Repository
Source URL: http://www.bitrepository.com/web-programming/php/resizing-an-image.html
Source URL: http://www.bitrepository.com/web-programming/php/resizing-an-image.html
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
*/
*/
class Resize_Image
class Resize_Image
{
{
var $image_to_resize;
var $image_to_resize;
var $new_width;
var $new_width;
var $new_height;
var $new_height;
var $save;
var $save;
function resize()
function resize()
{
{
if( !file_exists( $this -> image_to_resize ) )
if( !file_exists( $this -> image_to_resize ) )
{
{
exit( "File " . $this -> image_to_resize . " does not exist." );
exit( "File " . $this -> image_to_resize . " does not exist." );
}
}
$info = GetImageSize( $this -> image_to_resize );
$info = GetImageSize( $this -> image_to_resize );
if( empty( $info ) )
if( empty( $info ) )
{
{
exit( "The file " . $this -> image_to_resize . " doesn't seem to be an image." );
exit( "The file " . $this -> image_to_resize . " doesn't seem to be an image." );
}
}
$width = $info[ 0 ];
$width = $info[ 0 ];
$height = $info[ 1 ];
$height = $info[ 1 ];
$mime = $info[ 'mime' ];
$mime = $info[ 'mime' ];
$type = substr( strrchr( $mime, '/' ), 1 );
$type = substr( strrchr( $mime, '/' ), 1 );
switch( $type )
switch( $type )
{
{
case 'jpeg':
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
$new_image_ext = 'jpg';
break;
break;
case 'png':
case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png';
$new_image_ext = 'png';
break;
break;
case 'bmp':
case 'bmp':
$image_create_func = 'ImageCreateFromBMP';
$image_create_func = 'ImageCreateFromBMP';
$image_save_func = 'ImageBMP';
$image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
$new_image_ext = 'bmp';
break;
break;
case 'gif':
case 'gif':
$image_create_func = 'ImageCreateFromGIF';
$image_create_func = 'ImageCreateFromGIF';
$image_save_func = 'ImageGIF';
$image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
$new_image_ext = 'gif';
break;
break;
case 'vnd.wap.wbmp':
case 'vnd.wap.wbmp':
$image_create_func = 'ImageCreateFromWBMP';
$image_create_func = 'ImageCreateFromWBMP';
$image_save_func = 'ImageWBMP';
$image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
$new_image_ext = 'bmp';
break;
break;
case 'xbm':
case 'xbm':
$image_create_func = 'ImageCreateFromXBM';
$image_create_func = 'ImageCreateFromXBM';
$image_save_func = 'ImageXBM';
$image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
$new_image_ext = 'xbm';
break;
break;
default: $image_create_func = 'ImageCreateFromJPEG';
default: $image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
$new_image_ext = 'jpg';
}// New Image
}// New Image
$image_c = ImageCreateTrueColor( $this -> new_width, $this -> new_height );
//$image_c = ImageCreateTrueColor( $this -> new_width, $this -> new_height );
// Set a maximum height and width
$new_width = $this -> new_width;
$new_height = $this -> new_height;
//$ratio_orig = $width/$height;
//if ($new_width/$new_height > $ratio_orig) {
// $new_width = (int)$new_height*$ratio_orig;
//} else {
// $new_height = $new_width/$ratio_orig;
//}
// resize
$crop = true;
if($crop){
//if($width < $new_width or $height < $new_height) return "Picture is too small!";
$ratio = max($new_width/$width, $new_height/$height);
$height = $new_height / $ratio;
$x = ($width - $new_width / $ratio) / 2;
$width = $new_width / $ratio;
}
else{
//if($width < $new_width and $height < $new_height) return "Picture is too small!";
$ratio = min($new_width/$width, $new_height/$height);
$new_width = $width * $ratio;
$new_height = $height * $ratio;
$x = 0;
}
$image_c = ImageCreateTrueColor( $new_width, $new_height );
if ($type=='png') {
if ($type=='png') {
imagealphablending($image_c, false);
imagealphablending($image_c, false);
imagesavealpha($image_c, true);
imagesavealpha($image_c, true);
}
}
$new_image = $image_create_func( $this -> image_to_resize );
$new_image = $image_create_func( $this -> image_to_resize );
if ($type=='png') {
if ($type=='png') {
imagealphablending($new_image, true);
imagealphablending($new_image, true);
}
}
ImageCopyResampled( $image_c, $new_image, 0, 0, 0, 0, $this -> new_width, $this -> new_height, $width, $height );
//ImageCopyResampled( $image_c, $new_image, 0, 0, 0, 0, $this -> new_width, $this -> new_height, $width, $height );
//ImageCopyResampled( $image_c, $new_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
ImageCopyResampled( $image_c, $new_image, 0, 0, $x, 0, $new_width, $new_height, $width, $height );
$process = $image_save_func( $image_c, $this->save );
$process = $image_save_func( $image_c, $this->save );
// Free up the memory.
imagedestroy($image);
imagedestroy($newImage);
return array( 'result' => $process, 'new_file_path' => $this->save );
return array( 'result' => $process, 'new_file_path' => $this->save );
}
}
}
}
?>
?>