Al caricamento di un immagine uploadata devo farne una miniatura. Le due immagini (grande e piccola) vanno i cartelle diverse.
Utilizzo questa funzione:
codice:
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){ $imgsize = getimagesize($source_file);
$width = $imgsize[0];
$height = $imgsize[1];
$mime = $imgsize['mime'];
switch($mime){
case 'image/png':
$image_create = "imagecreatefrompng";
$image = "imagepng";
$quality = 7;
break;
case 'image/jpeg':
$image_create = "imagecreatefromjpeg";
$image = "imagejpeg";
$quality = 80;
break;
default:
return false;
break;
}
$dst_img = imagecreatetruecolor($max_width, $max_height);
$src_img = $image_create($source_file);
$width_new = $height * $max_width / $max_height;
$height_new = $width * $max_height / $max_width;
//if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
if($width_new > $width){
//cut point by height
$h_point = (($height - $height_new) / 2);
//copy image
imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
} else {
//cut point by width
$w_point = (($width - $width_new) / 2);
imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
}
$image($dst_img, $dst_dir, $quality);
if($dst_img)imagedestroy($dst_img);
if($src_img)imagedestroy($src_img);
}
Come prima cosa, metto il file nella sua cartella:
codice:
$uploadfile = $_FILES["uploadFile"]["tmp_name"];
$temp = explode(".", $_FILES["uploadFile"]["name"]);
$newfilename = $new_name . "." . end($temp);
move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $big_dir . $newfilename);
Fin qui, funziona tutto; il file viene rinominato, e copiato nella sua cartella.
A questo punto, devo fare la miniatura (Le miniature devono essere 354px / 238px), ed utilizzo questo codice:
codice:
// path & file
$orpathfile = $big_dir . $newfilename;
$destpathfile = $thumb_dir . $newfilename;
// get new big image data
list($th_width, $th_height, $type, $attr) = getimagesize($orpathfile);
if ($th_width > $th_height) {
$newheight = round(($th_width * 238) / 354);
$newwidth = 354;
} else if ($th_height > $th_width) {
$newwidth = round(($th_height * 354) / 238);
$newheight = 238;
}
resize_crop_image($newwidth, $newheight, $orpathfile, $thumb_dir);
Ed è qui che il codice fallisce; l'immagine in miniatura non viene creata né salvata nella sua cartella.
Con un echo ho verificato che legge i valori delle variabili $th_width e $th_height, ma la funzione resize_crop_image non funge. Ho provato sia scrivendo come nell'esempio qui sopra, sia indicando $destpathfile al posto di $thumb_dir, e quindi dando come valore finale passato alla funzione, sia la sola cartella di destinazione, sia questa ed il nome del file.
In entrambe i casi, nulla. Dove sbaglio???
P.S.
Ovviamente, entrambe le cartelle hanno i permessi necessari...