Hey y'all,
This script takes care of the creation of a thumbnail and the image upload to the database. But:
1) I would like to see the images written to their own dirs instead of the database, but I can't do it.
2) When uploading an image I get the 1st errormessage, even though the pictureformat is good. What could be the problem?
Can anyone help me with this? I'm puzzled and could use a push in the right direction.
Thanks!
<?php
include ("/usr/local/plesk/apache/vhosts/XXXXXXXX.nl/httpdocs/support.inc");
$MAX_THUMB_WIDTH = 100;
function smCreateImage ($src_img, $MAX_IMAGE_WIDTH) {
$SCALE_FACTOR = (ImageSX ($src_img) > $MAX_IMAGE_WIDTH ? (ImageSX ($src_img) / $MAX_IMAGE_WIDTH) : 1);
$new_w = ImageSX ($src_img) / $SCALE_FACTOR;
$new_h = ImageSY ($src_img)/ $SCALE_FACTOR;
$dst_img = ImageCreate ($new_w, $new_h);
ImageCopyResized ($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, ImageSX ($src_img), ImageSY($src_img));
$tmp_img = tempnam ("/usr/local/plesk/apache/vhosts/XXXXXXXX.nl/httpdocs/tmp", "smImg");
ImageJPEG ($dst_img, $tmp_img);
$fd = fopen ($tmp_img, "r");
$foto_data = addslashes (fread ($fd, filesize ($tmp_img)));
fclose ($fd);
unlink ($tmp_img);
return $foto_data;
}
if ($sm_cookie["logged_in"] != "yes") {
header ("Location: ".$usersettings_script_login);
exit;
}
if ($album_id >= $MAX_ALBUM_ID) {
echo "Ongeldig album ID!";
exit;
}
if (!isset ($action) or strtolower ($action) == "upload_main") {
include ($usersettings_form_fotoalbum);
exit;
}
if ($action == "add") {
include ($usersettings_form_fotoprocess);
exit;
}
if ($action == "change") {
include ($usersettings_form_fotoprocess);
exit;
}
if (strtolower ($action) == "upload_main_process") {
/* Connect to the database */
$dblink = mysql_connect ($dbinfo["hostname"], $dbinfo["username"], $dbinfo["password"]) or die ("Could not connect to database!");
$query = "SELECT * FROM ".$dbinfo["table_persoon"]." WHERE handle LIKE '".$sm_cookie["handle"]."' AND wachtwoord LIKE '".$sm_cookie["wachtwoord"]."'";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Authentication query failed...");
if (mysql_num_rows ($result) == 0)
die ("Authentication failed");
$persoon = mysql_fetch_array ($result);
$mimetype_foto = $userfile_type;
switch ($mimetype_foto) {
case "image/jpeg" :
$src_img = ImageCreateFromJpeg ($userfile);
break;
case "image/jpg" :
$src_img = ImageCreateFromJpeg ($userfile);
break;
case "image/pjpeg" :
$src_img = ImageCreateFromJpeg ($userfile);
break;
case "image/png" :
$src_img = ImageCreateFromPNG ($userfile);
break;
case "image/x-png" :
$src_img = ImageCreateFromPNG ($userfile);
break;
default:
$error_msg[] = "Alleen foto's in JPEG of PNG formaat worden geaccepteerd.";
break;
}
if (!$src_img) {
switch ($mimetype_foto) {
case "image/jpeg" :
$error_msg[] = "De foto werd niet herkend als JPEG-formaat.<br>
Neem contact met ons op als dit probleem zich voor blijft doen.";
break;
case "image/jpg" :
$error_msg[] = "De foto werd niet herkend als JPEG-formaat.<br>
Neem contact met ons op als dit probleem zich voor blijft doen.";
break;
case "image/pjpeg" :
$error_msg[] = "De foto werd niet herkend als JPEG-formaat.<br>
Neem contact met ons op als dit probleem zich voor blijft doen.";
break;
case "image/png" :
$error_msg[] = "De foto werd niet herkend als PNG-formaat.<br>
Neem contact met ons op als dit probleem zich voor blijft doen.";
break;
case "image/x-png" :
$error_msg[] = "De foto werd niet herkend als PNG-formaat.<br>
Neem contact met ons op als dit probleem zich voor blijft doen.";
break;
default:
$error_msg[] = "De foto werd niet herkend.";
break;
}
}
if (count ($error_msg)) {
include ($usersettings_form_fotoalbum);
exit;
}
$query = "SELECT * FROM ".$dbinfo["table_fotoalbum"]." WHERE persoon_id LIKE '".$persoon["id"]."' AND album_id LIKE '$album_id'";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Query failed...");
/* Make new record */
if (($PIC_UPDATE = mysql_num_rows ($result)) == 1) {
$query = "DELETE FROM ".$dbinfo["table_fotoalbum"]." WHERE persoon_id LIKE '".$persoon["id"]."' AND album_id LIKE '$album_id'";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Query failed...");
}
$foto_data = smCreateImage ($src_img, $MAX_IMAGE_WIDTH);
$thumb_data = smCreateImage ($src_img, $MAX_THUMB_WIDTH);
$mimetype_foto = "image/jpeg";
$query = "INSERT INTO ".$dbinfo["table_fotoalbum"]." (id, persoon_id, album_id, mimetype_foto, foto, thumbnail, hoofdfoto) ";
$query .= "VALUES ('', '".$persoon["id"]."', '$album_id', '$mimetype_foto', '$foto_data', '$thumb_data', 'N')";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Query failed...$query");
$last_insert_id = mysql_insert_id ();
$ok_msg[] = ($PIC_UPDATED ? "Foto gewijzigd" : "Nieuwe foto toegevoegd");
if ($is_hoofdfoto == "yes") {
/* Set main picture flag to No for all pictures */
$query = "UPDATE ".$dbinfo["table_fotoalbum"]." SET hoofdfoto='N' WHERE persoon_id LIKE '".$sm_cookie["id"]."'";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Update query failed...");
/* And make picture the main picture */
$query = "UPDATE ".$dbinfo["table_fotoalbum"]." SET hoofdfoto='J' WHERE persoon_id LIKE '".$sm_cookie["id"]."' AND album_id LIKE '$album_id'";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Update query failed...");
$ok_msg[] = "Foto is de huidige hoofdfoto.";
}
mysql_close ($dblink);
include ($usersettings_form_fotoalbum);
exit;
}
if ($action == "remove") {
if (!isset ($album_id)) {
echo "Geen album_id meegegeven!";
exit;
}
/* Connect to the database */
$dblink = mysql_connect ($dbinfo["hostname"], $dbinfo["username"], $dbinfo["password"]) or die ("Could not connect to database!");
$query = "DELETE FROM ".$dbinfo["table_fotoalbum"]." WHERE persoon_id LIKE '".$sm_cookie["id"]."' AND album_id LIKE '$album_id'";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Update query failed...");
$ok_msg[] = "Foto ".($album_id+1)." verwijderd.";
mysql_close ($dblink);
include ($usersettings_form_fotoalbum);
exit;
}
if ($action == "makemain") {
if (!isset ($album_id)) {
echo "Geen album_id meegegeven!";
exit;
}
/* Connect to the database */
$dblink = mysql_connect ($dbinfo["hostname"], $dbinfo["username"], $dbinfo["password"]) or die ("Could not connect to database!");
/* First check whether slot with ID = album_id exists */
$query = "SELECT * FROM ".$dbinfo["table_fotoalbum"]." WHERE persoon_id LIKE '".$sm_cookie["id"]."' AND album_id LIKE '$album_id'";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Query failed...");
if (mysql_num_rows ($result) == 1) {
/* Set main picture flag to No for all pictures */
$query = "UPDATE ".$dbinfo["table_fotoalbum"]." SET hoofdfoto='N' WHERE persoon_id LIKE '".$sm_cookie["id"]."'";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Update query failed...");
/* And make picture the main picture */
$query = "UPDATE ".$dbinfo["table_fotoalbum"]." SET hoofdfoto='J' WHERE persoon_id LIKE '".$sm_cookie["id"]."' AND album_id LIKE '$album_id'";
$result = mysql_db_query ($dbinfo["dbname"], $query, $dblink);
if ($result == FALSE)
die ("Update query failed...");
}
mysql_close ($dblink);
include ($usersettings_form_fotoalbum);
exit;
}
?>