Funzioncine collaudate:
Codice PHP:
function resize_jpg_file ($img,$maxX = 220, $maxY = 170)
{
$src = imagecreatefromjpeg($img);
$RealX = imagesx($src);
$RealY = imagesy($src);
if ($RealX > $maxX)
{
$newX = $maxX;
$newY = ($RealY/$RealX)*$newX;
}
else
{
$newY = $maxY;
$newX = ($RealX/$RealY)*$newY;
}
$dst = imagecreatetruecolor($newX, $newY);
imagecopyresized($dst, $src, 0, 0, 0, 0, $newX, $newY, $RealX, $RealY);
imagejpeg($dst, 'new_'.$img, 60);
return $dst;
}
Se l'immagine passata come parametro (devi passare solo il path) eccede le dimensioni passate come paramtri (opzionali).. allora ridimensiona per far stare l'immagine entro quelle dimensioni (e ridimensiona mantenendo le proporzioni originali dell'immagine)
Codice PHP:
function resize_jpg_binary ($img, $max_width = 50, $max_height = 50)
{
$src = imagecreatefromjpeg($img);
$img_width = imagesx($src);
$img_height = imagesy($src);
if ($img_width > $max_width)
{
$thumb_width = $max_width;
$thumb_height= ($img_height/$img_width)*$thumb_width;
}
else
{
$thumb_height = $max_height;
$thumb_width = ($img_width/$img_height)*$thumb_height;
}
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresized($thumb, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $img_width, $img_height);
ob_start();
ImageJPEG($thumb);
$resized = ob_get_contents();
ob_end_clean();
return $resized;
}
Questa fa praticamente la stessa cosa, ma invece di salvare il file ti restituisce il contenuto binario del file..è utile se vuoi salvarlo su database o se comunque vuoi maggior controllo su dove salvi