I've created a PHP5 image resize class, using ImageCopyResampled, that someone might find useful, with support for JPEG, PNG, and GIF formats. It retains the original image's aspect ratio when resizing, and doesn't resize or resample if the original width and height is smaller then the desired resize.
<?php
class imaging
{
private $img_input;
private $img_output;
private $img_src;
private $format;
private $quality = 80;
private $x_input;
private $y_input;
private $x_output;
private $y_output;
private $resize;
public function set_img($img)
{
$ext = strtoupper(pathinfo($img, PATHINFO_EXTENSION));
if(is_file($img) && ($ext == "JPG" OR $ext == "JPEG"))
{
$this->format = $ext;
$this->img_input = ImageCreateFromJPEG($img);
$this->img_src = $img;
}
elseif(is_file($img) && $ext == "PNG")
{
$this->format = $ext;
$this->img_input = ImageCreateFromPNG($img);
$this->img_src = $img;
}
elseif(is_file($img) && $ext == "GIF")
{
$this->format = $ext;
$this->img_input = ImageCreateFromGIF($img);
$this->img_src = $img;
}
$this->x_input = imagesx($this->img_input);
$this->y_input = imagesy($this->img_input);
}
public function set_size($size = 100)
{
if($this->x_input > $size && $this->y_input > $size)
{
if($this->x_input >= $this->y_input)
{
$this->x_output = $size;
$this->y_output = ($this->x_output / $this->x_input) * $this->y_input;
}
else
{
$this->y_output = $size;
$this->x_output = ($this->y_output / $this->y_input) * $this->x_input;
}
$this->resize = TRUE;
}
else { $this->resize = FALSE; }
}
public function set_quality($quality)
{
if(is_int($quality))
{
$this->quality = $quality;
}
}
public function save_img($path)
{
if($this->resize)
{
$this->img_output = ImageCreateTrueColor($this->x_output, $this->y_output);
ImageCopyResampled($this->img_output, $this->img_input, 0, 0, 0, 0, $this->x_output, $this->y_output, $this->x_input, $this->y_input);
}
if($this->format == "JPG" OR $this->format == "JPEG")
{
if($this->resize) { imageJPEG($this->img_output, $path, $this->quality); }
else { copy($this->img_src, $path); }
}
elseif($this->format == "PNG")
{
if($this->resize) { imagePNG($this->img_output, $path); }
else { copy($this->img_src, $path); }
}
elseif($this->format == "GIF")
{
if($this->resize) { imageGIF($this->img_output, $path); }
else { copy($this->img_src, $path); }
}
}
public function get_width()
{
return $this->x_input;
}
public function get_height()
{
return $this->y_input;
}
public function clear_cache()
{
@ImageDestroy($this->img_input);
@ImageDestroy($this->img_output);
}
}
$src = "myimage.jpg";
$img = new imaging;
$img->set_img($src);
$img->set_quality(80);
$img->set_size(200);
$img->save_img("small_" . $src);
$img->set_size(50);
$img->save_img("baby_" . $src);
$img->clear_cache();
?>