Salve,
tempo fa ho scaricato questo script per l'upload e resize di immagini ho cercato di modificarlo in questo modo:
Codice PHP:
<?php ini_set("memory_limit", "200000000");?>
<?php
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
// Il file deve essere jpg, gif, bmp, png e massimo 4 MB
if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
{
// impostazioni massime
$max_upload_width = 2592;
$max_upload_height = 1944;
if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
$max_upload_width = $_REQUEST['max_width_box'];
}
if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
$max_upload_height = $_REQUEST['max_height_box'];
}
// se l'immagine caricata è JPG/JPEG
if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){
$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
}
// se l'immagine caricata è GIF
if($_FILES["image_upload_box"]["type"] == "image/gif"){
$image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
}
// se l'immagine caricata è PNG
if($_FILES["image_upload_box"]["type"] == "image/x-png"){
$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
}
// rinomino il file aggiungendo dopo il nome data, ora, minuti e secondi e poi l'estensione
$uploaded_file = $_FILES["image_upload_box"]["name"];
$last_dot = strrpos($uploaded_file, ".");
//restituisce l'estensione del file
$ext = substr($uploaded_file, $last_dot);
//nome del file
$filename = substr($uploaded_file, 0 , $last_dot);
$remote_file = "image_files/" . $filename . date('dmYHi') . $ext;
$nuovo_nome_file = $filename . date('dmYHi') . $ext;
$vecchio_nome_file = $filename . $ext;
imagejpeg($image_source,$remote_file,100);
chmod($remote_file,0644);
// ottengo la larghezza e l'altezza dell'immagine originale
list($image_width, $image_height) = getimagesize($remote_file);
if($image_width>$max_upload_width || $image_height >$max_upload_height){
$proportions = $image_width/$image_height;
if($image_width>$image_height){
$new_width = $max_upload_width;
$new_height = round($max_upload_width/$proportions);
}
else{
$new_height = $max_upload_height;
$new_width = round($max_upload_height*$proportions);
}
$new_image = imagecreatetruecolor($new_width , $new_height);
$image_source = imagecreatefromjpeg($remote_file);
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagejpeg($new_image,$remote_file,100);
imagedestroy($new_image);
}
imagedestroy($image_source);
header("Location: submit.php?upload_message=immagine inviata correttamente&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
exit;
}
else{
header("Location: submit.php?upload_message=assicurarsi che il file sia jpg, jpeg , gif o png e che sia piu' piccolo di 4MB&upload_message_type=error");
exit;
}
}
?>
<?php if(isset($_REQUEST['upload_message'])){?>
<div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
<?php echo htmlentities($_REQUEST['upload_message']);?>
</div>
<?php }?>
<form action="submit.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">
<label>Immagine massimo 4MB, formato jpg, jpeg, gif o png:</label>
<input name="image_upload_box" type="file" id="image_upload_box" size="40" />
<input type="submit" name="submit" value="Upload image" />
<input name="max_width_box" type="hidden" id="max_width_box" value="600" size="4">
<input name="max_height_box" type="hidden" id="max_height_box" value="300" size="4">
<input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
</form>
<?php
if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){
$vedi_img = explode(".", $_REQUEST['show_image']);
$nuova_img = $vedi_img [0] .date('dmYHi') . "." .$vedi_img [1];
?>
[img]image_files/<?php echo $nuova_img?>[/img]
<?php } ?>
Vorrei fare in modo che se l'immagine è ad esempio 1024x768 dovrà essere ridimensionata, mantenendo le proporzioni, ad un massimo di larghezza 600 e altezza 300.
Nel caso in esempio è chiaro che non dovendo superare l'altezza di 300 la larghezza sarà di 400.
Spero di essere stato chiaro.