Al turrón, la clase SimpleImage seguro que todos o casi todos la conocéis, de todos modos dejo el código con las funciones que realmente uso para acortar.
Código PHP:
class SimpleImage
{
var $image;
var $image_type;
function load($filename)
{
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if ($this->image_type == IMAGETYPE_JPEG) {
$this->image = imagecreatefromjpeg($filename);
} elseif ($this->image_type == IMAGETYPE_GIF) {
$this->image = imagecreatefromgif($filename);
} elseif ($this->image_type == IMAGETYPE_PNG) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = null)
{
if ($image_type == IMAGETYPE_JPEG) {
imagejpeg($this->image, $filename, $compression);
} elseif ($image_type == IMAGETYPE_GIF) {
imagegif($this->image, $filename);
} elseif ($image_type == IMAGETYPE_PNG) {
imagepng($this->image, $filename);
}
if ($permissions != null) {
chmod($filename, $permissions);
}
}
function output($image_type = IMAGETYPE_JPEG)
{
if ($image_type == IMAGETYPE_JPEG) {
imagejpeg($this->image);
} elseif ($image_type == IMAGETYPE_GIF) {
imagegif($this->image);
} elseif ($image_type == IMAGETYPE_PNG) {
imagepng($this->image);
}
}
function getWidth()
{
return imagesx($this->image);
}
function getHeight()
{
return imagesy($this->image);
}
function resizeToHeight($height)
{
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width, $height);
}
function resize($width, $height)
{
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
Código PHP:
$randomFileName = date("d_m_Y_H_i_s")."-".rand(0,999).".".$extension;
move_uploaded_file($_FILES['upl']['tmp_name'], $fullPath."/".$randomFileName))
Código PHP:
$image = new SimpleImage();
$image->load($_FILES['upl']['tmp_name']);
$image->resizeToHeight(500);
$image->save($_FILES['upl']['tmp_name']);
$randomFileName = date("d_m_Y_H_i_s")."-".rand(0,999).".".$extension;
move_uploaded_file($_FILES['upl']['tmp_name'], $fullPath."/".$randomFileName))
Seguro que alguien ha tenido el mismo problema alguna vez y tiene más conocimientos que yo :/