Vezi daca merge varianta asta, sunt definite la inceput variabilele pt imagini pe care le foloseste comanda de adaugare in baza de date, astfel, daca nu sunt imagini adaugate, ele vor fi goale. Iar functia de la $result e accesata doar daca sunt imagini.
Mai departe cred ca te descurci daca apare vreo eroare, sau, refacut tot scriptul asa cum trebuie.
Cod: Selectaţi tot
$fisier_final_jpg = $fisier_final2_jpg = $fisier_final3_jpg = '';
define( 'THUMBNAIL_IMAGE_MAX_WIDTH', 140 ); // latime maxima a imaginii thumbnail generate
define( 'THUMBNAIL_IMAGE_MAX_HEIGHT', 100 ); // inaltime maxima thumbnail
$fisier= $_FILES['Image1']['name'];
$marime = $_FILES['Image1']['size'];
$fisier2= $_FILES['Image2']['name'];
$marime = $_FILES['Image2']['size'];
$fisier3= $_FILES['Image3']['name'];
$marime = $_FILES['Image3']['size'];
// putem pune o limita la marimea pozei, in kB
if($marime >= 1500000)
{
echo "Marime fisier poza prea mare. Maxim 150 KB";
echo '<br><a href="javascript: history.go(-1)">Inapoi</a>';
exit;
}
// denumire fisier poza si eliminare caractere interzise din numele fisierului:
function RewriteFisier ($string){
return strtolower(trim(preg_replace("/[^0-9a-zA-Z.]+/", "-", $string),"-"));
}
$fisier = RewriteFisier($fisier);
$now = time();
$fisier = $now."-".$fisier;
$fisier2 = $now."-".$fisier2;
$fisier3 = $now."-".$fisier3;
$fisier_final = $now."-".$fisier;
$fisier_final2 = $now."-".$fisier2;
$fisier_final3 = $now."-".$fisier3; // adaugare ID unic la umele fisierului, util in cazul cand uploadam doua poze cu acelasi nume de fisier
// thumbnailul va fi transformat in imagine de tip "jpg"
if (substr($fisier_final, -3) =='png')
{
$fisier_final_jpg = str_replace("png", "jpg", $fisier_final);
}
if (substr($fisier_final, -3) =='gif')
{
$fisier_final_jpg = str_replace("gif", "jpg", $fisier_final);
}
if (substr($fisier_final, -3) =='bmp')
{
$fisier_final_jpg = str_replace("bmp", "jpg", $fisier_final);
}
if (substr($fisier_final, -4) =='jpeg')
{
$fisier_final_jpg = str_replace("jpeg", "jpg", $fisier_final);
}
if (substr($fisier_final, -3) =='jpg')
{
$fisier_final_jpg = $fisier_final;
}
// thumbnailul va fi transformat in imagine de tip "jpg"
if (substr($fisier_final2, -3) =='png')
{
$fisier_final2_jpg = str_replace("png", "jpg", $fisier_final2);
}
if (substr($fisier_final, -3) =='gif')
{
$fisier_final2_jpg = str_replace("gif", "jpg", $fisier_final2);
}
if (substr($fisier_final2, -3) =='bmp')
{
$fisier_final2_jpg = str_replace("bmp", "jpg", $fisier_final2);
}
if (substr($fisier_final2, -4) =='jpeg')
{
$fisier_final2_jpg = str_replace("jpeg", "jpg", $fisier_final2);
}
if (substr($fisier_final2, -3) =='jpg')
{
$fisier_final2_jpg = $fisier_final2;
}
// thumbnailul va fi transformat in imagine de tip "jpg"
if (substr($fisier_final3, -3) =='png')
{
$fisier_final3_jpg = str_replace("png", "jpg", $fisier_final3);
}
if (substr($fisier_final3, -3) =='gif')
{
$fisier_final3_jpg = str_replace("gif", "jpg", $fisier_final3);
}
if (substr($fisier_final3, -3) =='bmp')
{
$fisier_final3_jpg = str_replace("bmp", "jpg", $fisier_final3);
}
if (substr($fisier_final3, -4) =='jpeg')
{
$fisier_final3_jpg = str_replace("jpeg", "jpg", $fisier_final3);
}
if (substr($fisier_final3, -3) =='jpg')
{
$fisier_final3_jpg = $fisier_final3;
}
// functie creare thumbnail
function generate_image_thumbnail( $source_image_path, $thumbnail_image_path )
{
list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );
switch ( $source_image_type )
{
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif( $source_image_path );
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg( $source_image_path );
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng( $source_image_path );
break;
}
if ( $source_gd_image === false )
{
return false;
}
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;
if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
{
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
}
elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
{
$thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio );
}
else
{
$thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio );
}
$thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );
imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );
imagejpeg( $thumbnail_gd_image, $thumbnail_image_path, 80 );
imagedestroy( $source_gd_image );
imagedestroy( $thumbnail_gd_image );
return true;
}
define( 'UPLOADED_IMAGE_DESTINATION', 'img_original/' ); // calea catre directorul unde se uploadeaza poza originala
define( 'THUMBNAIL_IMAGE_DESTINATION', 'img_thumbnail/' ); // calea catre directorul unde se uploadeaza poza mica (thumbnail)
function process_image_upload( $field )
{
$temp_image_path = $_FILES[ $field ][ 'tmp_name' ];
$now = time();
$temp_image_name = $now."-".$_FILES[ $field ][ 'name' ];
$temp_image_name = RewriteFisier($temp_image_name);
list( , , $temp_image_type ) = getimagesize( $temp_image_path );
if ( $temp_image_type === NULL )
{
return false;
}
switch ( $temp_image_type )
{
case IMAGETYPE_GIF:
break;
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_PNG:
break;
default:
return false;
}
$uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
move_uploaded_file( $temp_image_path, $uploaded_image_path );
$thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace( '{\\.[^\\.]+$}', '.jpg', $temp_image_name );
$result = generate_image_thumbnail( $uploaded_image_path, $thumbnail_image_path );
return $result
? array( $uploaded_image_path, $thumbnail_image_path )
: false;
}
$result = (isset($_FILES['Image1']) && !empty($_FILES['Image1']) ? process_image_upload( 'Image1' ) : array();
$result = (isset($_FILES['Image2']) && !empty($_FILES['Image2']) ? process_image_upload( 'Image2' ) : array();
$result = (isset($_FILES['Image3']) && !empty($_FILES['Image3']) ? process_image_upload( 'Image3' ) : array();
$query="INSERT INTO anunturi (categorie, subCategorie, judet, pret, tipPret, titlu, continut, email, valid, telefon, nume, data, poze, poze2, poze3 )
VALUES ('$categorie','$subcategorie','$judet','$pret','$tipPret','$titlu','$continut','$email','0','$telefon','$nume','$servdate','$fisier_final_jpg','$fisier_final2_jpg','$fisier_final3_jpg')";
if (!mysql_query($query)) die(mysql_error());
else echo " Mesajul a fost trimis la data de $servdate";
mysql_close($conn);