Ho un sistema che consente all'utente di caricare delle immagini (con dei parametri di tipo - jpg - e dimensione max), che dovrebbero poi essere ridimensionate (max 500 px x 500 px), ed al contempo dovrebbe essere creata una thumbnail larga 100 px. Entrambe i nomi andrebbero registrati su db.
Questo il codice della pagina di input:
Codice PHP:
if ($ctrl == "SEND") {
$upload = new upload("", "200000", "newsimg");
$go = $upload -> putFile ("image");
if ( $go ) {
$filePath = $upload -> splitFilePath ($go);
$img_name = $filePath[filename];
}
$mini = $upload -> miniatura ($go,"newsimg",100,100,"thumb_");
if ( $mini ) {
$filePath = $upload -> splitFilePath ($mini);
$thmb_name = $filePath[filename];
}
$query3 = ("INSERT INTO `news` (`thumb`, `img`) VALUES ('$thmb_name', '$img_name')");
$result3 = mysql_query($query3)
or die("Query failed 1");
echo "UPLOAD EFFETTUATO";
}
e questo è il codice del file upload.class.php che viene richiamato con include:
Codice PHP:
<?php
class upload
{
var $file_permitted;
var $archive_dir;
var $max_filesize;
function upload ( $file_perm , $max_fil = 200000, $arc_dir = ".." )
{
if ( empty ($file_perm) ) $file_perm = array ("image/pjpeg","image/jpeg");
$this->file_permitted = $file_perm;
$this->max_filesize = $max_fil;
$this->archive_dir = $arc_dir;
}
function putFile ( $file )
{
$userfile_type = strtok ( $_FILES[$file]['type'], ";");
$userfile_name = $_FILES[$file]['name'];
$userfile_name = str_replace(" ","","$userfile_name");
$userfile_name=trim($userfile_name);
$userfile_name = strtolower($userfile_name);
$userfile_size = $_FILES[$file]['size'];
$userfile = $_FILES[$file]['tmp_name'];
$error = "Impossible load this kind of file: $userfile_type
";
$file_permitted = $this->file_permitted;
foreach ( $file_permitted as $permitted )
{
if ( $userfile_type == $permitted ) $error = "";
}
if ( ($userfile_size <= 0) || ($userfile_size > $this->max_filesize) )
$error = ".";
if ( $error == "" )
{
$filename = basename ($userfile_name);
if (!empty ($this->archive_dir) )
$destination = $this->archive_dir."/".$filename;
else
$destination = $filename;
if ( file_exists($destination) )
{
srand((double)microtime()*1000000);
$filename = rand(0,20000).$filename;
if (!empty ($this->archive_dir) )
$destination = $this->archive_dir."/".$filename;
else
$destination = $filename;
}
if(!is_uploaded_file($userfile)) die ("The file $userfile_name isn't been loaded correctly.");
if ( !@move_uploaded_file ($userfile,$destination) ) die ("Impossible to copy $userfile_name from $userfile to final directory.");
return $destination;
}
else
{
echo $error;
return false;
}
}
function chkgd2()
{
$testGD = get_extension_funcs("gd");
if (!$testGD)
{
//echo "GD not even installed.";
return false;
}
else
{
ob_start();
phpinfo(8);
$grab = ob_get_contents();
ob_end_clean();
$version = strpos ($grab,"2.0 or higher");
if ( $version ) return "gd2";
else return "gd";
}
}
function resize_jpeg( $image_file_path, $new_image_file_path, $max_width=500, $max_height=500 )
{
$return_val = 1;
if(eregi("\.png$",$image_file_path))
{
$return_val = ( ($img = ImageCreateFromPNG ( $image_file_path )) && $return_val == 1 ) ? "1" : "0";
}
if(eregi("\.(jpg|jpeg)$",$image_file_path))
{
$return_val = ( ($img = ImageCreateFromJPEG ( $image_file_path )) && $return_val == 1 ) ? "1" : "0";
}
$FullImage_width = imagesx ($img);
$FullImage_height = imagesy ($img);
$ratio = ( $FullImage_width > $max_width ) ? (real)($max_width / $FullImage_width) : 1 ;
$new_width = ((int)($FullImage_width * $ratio));
$new_height = ((int)($FullImage_height * $ratio));
$ratio = ( $new_height > $max_height ) ? (real)($max_height / $new_height) : 1 ;
$new_width = ((int)($new_width * $ratio));
$new_height = ((int)($new_height * $ratio));
if ( $new_width == $FullImage_width && $new_height == $FullImage_height ) copy ( $image_file_path, $new_image_file_path );
$gd_version = $this->chkgd2();
if ( $gd_version == "gd2" )
{
$full_id = ImageCreateTrueColor ( $new_width , $new_height );
ImageCopyResampled ( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );
}
elseif ( $gd_version == "gd" )
{
$full_id = ImageCreate ( $new_width , $new_height );
ImageCopyResized ( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );
}
else //"GD Image Library is not installed.";
if(eregi("\.(jpg|jpeg)$",$image_file_path))
{
$return_val = ( $full = ImageJPEG( $full_id, $new_image_file_path, 100 ) && $return_val == 1 ) ? "1" : "0";
}
if(eregi("\.png$",$image_file_path))
{
$return_val = ( $full = ImagePNG( $full_id, $new_image_file_path ) && $return_val == 1 ) ? "1" : "0";
}
ImageDestroy( $full_id );
return ($return_val) ? TRUE : FALSE ;
}
function miniatura ( $image_path, $path, $larghezza=100, $altezza=100, $pre_name="thumb_" )
{
if ( (eregi("\.png$", $image_path) || eregi("\.(jpg|jpeg)$", $image_path)) && $image_path )
{
$image_name = basename ( $image_path );
if (!empty ($path) )
$thumb_path = $path."/".$pre_name.$image_name;
else
$thumb_path = $pre_name.$image_name;
if ( $this->resize_jpeg($image_path, $thumb_path, $larghezza, $altezza) ) return $thumb_path;
else return "Error creating thumbnail.
";
}
elseif ($image_path) return "Impossible create thumbnail by this kind of file.
";
elseif ($send && $image_path) return "<font face=\"Verdana\" size=\"2\" color=\"red\">[b]Error in image $cont loading![/b]</font>
";
}
function splitFilePath ( $completePath = "null/null" ) {
$filePath = array ( path=>"", filename=>"" ) ;
$tok = strtok($completePath,"/");
while ($tok) {
$file_name = $tok;
$tok = strtok("/");
}
$path = str_replace ( $file_name, "", $completePath );
$filePath[path] = $path;
$filePath[filename] = $file_name;
return $filePath;
}
}
?>
Il problema è che:
- crea correttamente l'immagine
- registra su db i nomi dell'immagine grande e della thumb
- non crea la thumb
Qualcuno riesce a capire dov'è il problema?