Jajajajaj, cosas que pasan... se me olvidó incluir una parte en el resto de las imágenes XD (la que actualmente creaba la nueva imagen). Aquí esta la nueva clase:
class IMGResizer {
/**
* The image file path.
* @var String.
*/
private $image = null;
/**
* Original image Width.
* @var Integer.
*/
private $owidth = 0;
/**
* Original image Height.
* @var Integer.
*/
private $oheight = 0;
/**
* Image file type (png, jpeg, jpg, gif, etc.). Currently only supports gif, png and jp(e)g.
* @var String.
*/
private $imgType = '';
/**
* Resize width.
* @var Integer.
*/
private $nwidth = 0;
/**
* Resize height.
* @var Integer
*/
private $nheight = 0;
/**
* Resized File name.
* @var String.
*/
private $newFile = '';
/**
* Image File base directory.
* @var String.
*/
private $path = '';
/**
* A class to resize images.
* @param String $imagePath - The image file name and path.
* @param Integer[] $newSize - $newSize[0] is the width and $newSize[1] is the height.
* @param String $resizedFileName - Optional; if not specified, the original filename will be used.
*/
public function __construct( $imagePath, $newSize, $resizedFileName = '' ) {
$this->image = $imagePath;
$data = getimagesize( $imagePath );
$this->owidth = $data[0];
$this->oheight = $data[1];
$tmp = explode( '/', $data['mime'] );
$this->imgType = $tmp[1];
$this->nwidth = $newSize[0];
$this->nheight = $newSize[1];
if( $resizedFileName )
$this->newFile = $resizedFileName;
else
$this->newFile = basename( $imagePath );
$this->path = dirname( $imagePath );
}
/**
* @return String - Returns the new image file name.
*/
public function resize() {
$target = null;
$source = null;
$fsave = "$this->path/$this->newFile";
switch( $this->imgType ) {
case 'png':
$source = imagecreatefrompng( $this->image );
$target = $this->getResizedResource();
imagealphablending( $target, false );
imagesavealpha( $target, true );
$transparent = imagecolorallocatealpha( $target, 255, 255, 255, 127 );
imagefilledrectangle( $target, 0, 0, $this->nwidth, $this->nheight, $transparent );
$this->resample( $target, $source );
imagepng( $target, $fsave );
break;
case 'jpeg':
case 'jpg':
$target = $this->getResizedResource();
$source = imagecreatefromjpeg( $this->image );
$this->resample( $target, $source );
imagejpeg( $target, $fsave );
break;
case 'gif':
$target = $this->getResizedResource();
$source = imagecreatefromgif( $this->image );
$this->resample( $target, $source );
imagegif( $target, $fsave );
break;
}
if( $target !== null )
imagedestroy( $target );
if( $source !== null )
imagedestroy( $source );
chmod( $fsave, 0644 );
return $this->newFile;
}
/**
* Creates a true color image with the values of $newSize param.
* @param Integer[] $origSize - Original Width and Height
* @param Integer[] $newSize - New width and height.
* @return Resource - Returns a resized image resource.
*/
public function getResizedResource() {
$w = 0;
$h = 0;
$x = $this->owidth;
$y = $this->oheight;
if( $x > $y ) {
$w = $this->nwidth;
$h = $y * ( $this->nheight / $x );
} else if( $x < $y ) {
$w = $w * ( $this->nwidth / $y );
$h = $this->nheight;
} else {
$w = $this->nwidth;
$h = $this->nheight;
}
return imagecreatetruecolor( $w, $h );
}
/**
* Sets the image and path of the image to be resized.
* @param String $imagePath - The image path and file name.
*/
public function setImageAndPath( $imagePath ) {
if( is_string( $imagePath ) ) {
$this->image = $imagePath;
$data = getimagesize( $imagePath );
$this->owidth = $data[0];
$this->oheight = $data[1];
$tmp = explode( '/', $data['mime'] );
$this->imgType = $tmp[1];
$this->path = dirname( $imagePath );
}
}
/**
* Sets the resized image name.
* @param String $name - Only the resized file name (not the path).
*/
public function setResizedImageName( $name ) {
if( preg_match( '/^[\w\.\-_]+$/', $name ) ) {
$position = strrchr( $name, '.' );
$this->newFile = preg_replace( "/$position/", '', $name );
}
}
/**
* Set the new sizes.
* @param Integer[] $sizes - Set the new sizes to be used when resizing the image.
*/
public function setNewSizes( $sizes ) {
if( is_array( $sizes ) && IMGResizer::areNumeric( $sizes ) ) {
if( isset( $sizes['width'] ) && isset( $sizes['height'] ) ) {
$this->nwidth = $sizes['width'];
$this->nheight = $sizes['height'];
} else {
$this->nwidth = $sizes[0];
$this->nheight = $sizes[1];
}
}
}
/**
* Checks whether or not the array contains only numbers or not.
* @param Mixed[] $array - The array to check.
* @return Boolean - True, if the array contains only numbers, false otherwise.
*/
public static function areNumeric( $array ) {
$ret = true;
if( ! is_array( $array ) ) {
$ret &= is_numeric( $array );
} else {
foreach( $array as $element ) {
if( is_array( $element ) ) {
$ret &= areNumeric( $element );
} else {
$ret &= is_numeric( $element );
}
if( ! $ret )
break;
}
}
return $ret;
}
/**
* Wrapper arround imagecopyresampled.
* Uses the new image width (set with the class constructor or with setNewSizes( $sizes ) method) and the current image height and width.
* @param Resource $target - The target image resource.
* @param Resource $source - Original Image resource.
* @param Integer[] $coords - The coordinates to be used. The first 2
* elements are the target coordinates and the last 2, the source image coordinates.
* @return Boolean | NULL - NULL if the $target or $source are not resources; True if
* the image was resampled or false if there was an error.
*/
public function resample( $target, $source, $coords = array() ) {
if( is_resource( $target ) && is_resource( $source ) ) {
$tx = 0;
$ty = 0;
$sx = 0;
$sy = 0;
if( ! empty( $coords ) && count( $coords ) == 4 ) {
$tx = $coords[0];
$ty = $coords[1];
$sx = $coords[2];
$sy = $coords[3];
}
return imagecopyresampled( $target, $source, $tx, $ty, $sx, $sy, $this->nwidth, $this->nheight, $this->owidth, $this->oheight );
}
return null;
}
}
Copia y pega la clase donde puse la otra en mi publicación anterior :P. Aquí hay un ejemplo para cualquiera que lo quiera usar:
Estructura de las carpetas
images - Contendrá todas las imágenes.
includes - Contendrá los archivos php (en este caso, la clase - imgresizer.php - y el script que procesa los datos - get.php).
index.php - El archiv principal.
index.php<!DOCTYPE html>
<html>
<head>
<title>Image Resizer</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('input[name=fname]').val($('#file-name').val());
$('#file-name').change(function(e) {
$('input[name=fname]').val($(this).val());
});
});
</head>
<body>
<?php
// Esta función simplemente crea un <select> con las imágenes que existen en la carpeta images.
function create_select() {
$dir = 'images';
$out = '';
if( $dh = opendir( $dir ) ) {
$out = '<select id="file-name">';
while( ( $file = readdir( $dh ) ) !== false ) {
if( $file != '..' && $file != '.' && ! preg_match( '/resized.+/', $file ) ) {
$out .= "<option value='$file'>$file</option>";
}
}
$out .= '</select>';
}
echo $out;
}
create_select();
?>
<form action="includes/get.php" method="get">
<label for="fname">Nombre del Archivo: <input type="text" name="fname" /></label>
<label for="fsize">Tamaño nuevo (ejemplo: 328x200): </label><input type="text" name="fsize" />
<input type="submit" />
</form>
<?php
function show_resized_images() {
$dir = 'images';
$out = '';
if( $dh = opendir( $dir ) ) {
$out = "";
while( ( $file = readdir( $dh ) ) !== false ) {
if( $file != '..' && $file != '.' && preg_match( '/resized.+/', $file ) ) {
$out .= "<img src='images/$file' />";
}
}
}
if( empty( $out ) )
$out = 'No hay imágenes redimensionadas.';
echo $out;
}
show_resized_images();
?>
</body>
</html>
get.php<?php
$root = '..';
include_once 'imgresizer.php';
$dir = $root . '/images';
$file = $dir . '/' . $_GET['fname'];
$resizer = new IMGResizer( $file, explode( 'x', $_GET['fsize'] ), "resized-$_GET[fname]" );
$resizer->resize();
header( "Location: http://localhost/" ); // Si utilizan un servidor local, entonces localhost estará bien, de lo contrario, cambien la URL por la que deseen.
?>
imgresizer.php<?php
class IMGResizer {
/**
* The image file path.
* @var String.
*/
private $image = null;
/**
* Original image Width.
* @var Integer.
*/
private $owidth = 0;
/**
* Original image Height.
* @var Integer.
*/
private $oheight = 0;
/**
* Image file type (png, jpeg, jpg, gif, etc.). Currently only supports gif, png and jp(e)g.
* @var String.
*/
private $imgType = '';
/**
* Resize width.
* @var Integer.
*/
private $nwidth = 0;
/**
* Resize height.
* @var Integer
*/
private $nheight = 0;
/**
* Resized File name.
* @var String.
*/
private $newFile = '';
/**
* Image File base directory.
* @var String.
*/
private $path = '';
/**
* A class to resize images.
* @param String $imagePath - The image file name and path.
* @param Integer[] $newSize - $newSize[0] is the width and $newSize[1] is the height.
* @param String $resizedFileName - Optional; if not specified, the original filename will be used.
*/
public function __construct( $imagePath, $newSize, $resizedFileName = '' ) {
$this->image = $imagePath;
$data = getimagesize( $imagePath );
$this->owidth = $data[0];
$this->oheight = $data[1];
$tmp = explode( '/', $data['mime'] );
$this->imgType = $tmp[1];
$this->nwidth = $newSize[0];
$this->nheight = $newSize[1];
if( $resizedFileName )
$this->newFile = $resizedFileName;
else
$this->newFile = basename( $imagePath );
$this->path = dirname( $imagePath );
}
/**
* @return String - Returns the new image file name.
*/
public function resize() {
$target = null;
$source = null;
$fsave = "$this->path/$this->newFile";
switch( $this->imgType ) {
case 'png':
$source = imagecreatefrompng( $this->image );
$target = $this->getResizedResource();
imagealphablending( $target, false );
imagesavealpha( $target, true );
$transparent = imagecolorallocatealpha( $target, 255, 255, 255, 127 );
imagefilledrectangle( $target, 0, 0, $this->nwidth, $this->nheight, $transparent );
$this->resample( $target, $source );
imagepng( $target, $fsave );
break;
case 'jpeg':
case 'jpg':
$target = $this->getResizedResource();
$source = imagecreatefromjpeg( $this->image );
$this->resample( $target, $source );
imagejpeg( $target, $fsave );
break;
case 'gif':
$target = $this->getResizedResource();
$source = imagecreatefromgif( $this->image );
$this->resample( $target, $source );
imagegif( $target, $fsave );
break;
}
if( $target !== null )
imagedestroy( $target );
if( $source !== null )
imagedestroy( $source );
chmod( $fsave, 0644 );
return $this->newFile;
}
/**
* Creates a true color image with the values of $newSize param.
* @param Integer[] $origSize - Original Width and Height
* @param Integer[] $newSize - New width and height.
* @return Resource - Returns a resized image resource.
*/
public function getResizedResource() {
$w = 0;
$h = 0;
$x = $this->owidth;
$y = $this->oheight;
if( $x > $y ) {
$w = $this->nwidth;
$h = $y * ( $this->nheight / $x );
} else if( $x < $y ) {
$w = $w * ( $this->nwidth / $y );
$h = $this->nheight;
} else {
$w = $this->nwidth;
$h = $this->nheight;
}
return imagecreatetruecolor( $w, $h );
}
/**
* Sets the image and path of the image to be resized.
* @param String $imagePath - The image path and file name.
*/
public function setImageAndPath( $imagePath ) {
if( is_string( $imagePath ) ) {
$this->image = $imagePath;
$data = getimagesize( $imagePath );
$this->owidth = $data[0];
$this->oheight = $data[1];
$tmp = explode( '/', $data['mime'] );
$this->imgType = $tmp[1];
$this->path = dirname( $imagePath );
}
}
/**
* Sets the resized image name.
* @param String $name - Only the resized file name (not the path).
*/
public function setResizedImageName( $name ) {
if( preg_match( '/^[\w\.\-_]+$/', $name ) ) {
$position = strrchr( $name, '.' );
$this->newFile = preg_replace( "/$position/", '', $name );
}
}
/**
* Set the new sizes.
* @param Integer[] $sizes - Set the new sizes to be used when resizing the image.
*/
public function setNewSizes( $sizes ) {
if( is_array( $sizes ) && IMGResizer::areNumeric( $sizes ) ) {
if( isset( $sizes['width'] ) && isset( $sizes['height'] ) ) {
$this->nwidth = $sizes['width'];
$this->nheight = $sizes['height'];
} else {
$this->nwidth = $sizes[0];
$this->nheight = $sizes[1];
}
}
}
/**
* Checks whether or not the array contains only numbers or not.
* @param Mixed[] $array - The array to check.
* @return Boolean - True, if the array contains only numbers, false otherwise.
*/
public static function areNumeric( $array ) {
$ret = true;
if( ! is_array( $array ) ) {
$ret &= is_numeric( $array );
} else {
foreach( $array as $element ) {
if( is_array( $element ) ) {
$ret &= areNumeric( $element );
} else {
$ret &= is_numeric( $element );
}
if( ! $ret )
break;
}
}
return $ret;
}
/**
* Wrapper arround imagecopyresampled.
* Uses the new image width (set with the class constructor or with setNewSizes( $sizes ) method) and the current image height and width.
* @param Resource $target - The target image resource.
* @param Resource $source - Original Image resource.
* @param Integer[] $coords - The coordinates to be used. The first 2
* elements are the target coordinates and the last 2, the source image coordinates.
* @return Boolean | NULL - NULL if the $target or $source are not resources; True if
* the image was resampled or false if there was an error.
*/
public function resample( $target, $source, $coords = array() ) {
if( is_resource( $target ) && is_resource( $source ) ) {
$tx = 0;
$ty = 0;
$sx = 0;
$sy = 0;
if( ! empty( $coords ) && count( $coords ) == 4 ) {
$tx = $coords[0];
$ty = $coords[1];
$sx = $coords[2];
$sy = $coords[3];
}
return imagecopyresampled( $target, $source, $tx, $ty, $sx, $sy, $this->nwidth, $this->nheight, $this->owidth, $this->oheight );
}
return null;
}
}
?>
Ese es un ejemplo completo y funcionando :).