Hej!
Jag håller på och försöker få till bilduppladdning, jag baserar mitt script på det här, som jag hittade bland PHP-Filerna här på wF:
http://www.webforum.nu/showthread.php?t=121070
Koden jag använder ser ut såhär:
<?php
if(isset($_GET['do']) && $_GET['do'] = "add_product"){
$product_cat = fix_sql($_POST['product_cat']);
$product_name = fix_sql($_POST['product_name']);
$product_article = fix_sql($_POST['product_article']);
$product_photo_name = $_FILES['product_photo']['name'];
$product_photo_tmp_name = $_FILES['product_photo']['tmp_name'];
$product_photo_type = $_FILES['product_photo']['type'];
$product_photo_ext = strtolower(substr($product_photo_name, (strrpos($product_photo_name, '.'))));
$product_prize = fix_sql($_POST['product_prize']);
$product_about = fix_sql($_POST['product_about']);
$fsize_dir = './../pics/product/';
$thumb_dir = './../pics/product/thumbs/';
$new_img_width_large = 480;
$new_img_width_small = 110;
$img_compress = 85;
$query = "INSERT INTO site_products "
."(product_cat, product_name, product_article, product_prize, product_about) "
."VALUES "
."("
."" . $product_cat . ", "
."'" . $product_name . "', "
."'" . $product_article . "', "
."" . $product_prize . ", "
."'" . $product_about . "'"
.")";
mysql_query($query, $the_link) or die("<p>Sql frågan genererade ett fel:<br /><span>Orsak: " . mysql_error() . "</span></p></body></html>");
$last_id = mysql_insert_id($the_link);
if(isset($last_id) && is_numeric($last_id) && !empty($product_photo_name)){
if ($product_photo_type == "image/pjpeg" || $product_photo_type == "image/gif" || $product_photo_type == "image/jpeg") {
// Proceed img_upload...
$query = "UPDATE site_products SET "
."product_picture = 'product_" . $last_id . $product_photo_ext . "' "
."WHERE product_id = " . $last_id . " "
."LIMIT 1";
mysql_query($query) or die(mysql_error());
$img_x_y_size = getimagesize($product_photo_tmp_name);
if ($new_img_width_large < $img_x_y_size[0]) {
$new_img_height_large = ceil(($new_img_width_large / $img_x_y_size[0]) * $img_x_y_size[1]);
$new_img_height_small = ceil(($new_img_width_small / $img_x_y_size[0]) * $img_x_y_size[1]);
// Gör om bildens storlek
$new_image_large = imagecreatetruecolor($new_img_width_large, $new_img_height_large);
$new_image_small = imagecreatetruecolor($new_img_width_small, $new_img_height_small);
// Kontrollera vilken bildtyp det är (1=gif , 2=jpg , 3=png)
switch($img_x_y_size[2]) {
case 1:
$image = imagecreatefromgif($product_photo_tmp_name);
break;
case 2:
$image = imagecreatefromjpeg($product_photo_tmp_name);
break;
case 3:
$image = imagecreatefrompng($product_photo_tmp_name);
break;
}
imagecopyresampled($new_image_large, $image, 0, 0, 0, 0, $new_img_width_large, $new_img_height_large, $img_x_y_size[0], $img_x_y_size[1]);
imagecopyresampled($new_image_small, $image, 0, 0, 0, 0, $new_img_width_small, $new_img_height_small, $img_x_y_size[0], $img_x_y_size[1]);
// Kopiera bilden till sin mapp
$save_img_large = $fsize_dir . 'product_' . $last_id . $product_photo_ext;
$save_img_small = $thumb_dir . 'product_' . $last_id . $product_photo_ext;
imagedestroy($image);
} else {
move_uploaded_file($product_photo_tmp_name, $fsize_dir . 'product_' . $last_id . $product_photo_ext);
move_uploaded_file($product_photo_tmp_name, $thumb_dir . 'product_' . $last_id . $product_photo_ext);
}
[b]imagejpeg($new_image_large, $save_img_large, $img_compress);
imagejpeg($new_image_small, $save_img_small, $img_compress);[/b]
[b]imagedestroy($new_image_large);
imagedestroy($new_image_small);[/b]
}else{
// Inte en bild...
}
}
[b]header("Location: product_add.php");[/b]
exit;
}
?>
Problem dyker upp när man laddar upp en bild som _inte_ överstiger maxbreddn (480px), då får jag ett helt gäng felmeddelanden:
Notice: Undefined variable: new_image_large in E:\Webserver\*******\*****\product_add.php on line 83
Notice: Undefined variable: save_img_large in E:\Webserver\*******\*****\product_add.php on line 83
Warning: imagejpeg(): supplied argument is not a valid Image resource in E:\Webserver\*******\*****\product_add.php on line 83
Notice: Undefined variable: new_image_small in E:\Webserver\*******\*****\product_add.php on line 84
Notice: Undefined variable: save_img_small in E:\Webserver\*******\*****\product_add.php on line 84
Warning: imagejpeg(): supplied argument is not a valid Image resource in E:\Webserver\*******\*****\product_add.php on line 84
Notice: Undefined variable: new_image_large in E:\Webserver\*******\*****\product_add.php on line 86
Warning: imagedestroy(): supplied argument is not a valid Image resource in E:\Webserver\*******\*****\product_add.php on line 86
Notice: Undefined variable: new_image_small in E:\Webserver\*******\*****\product_add.php on line 87
Warning: imagedestroy(): supplied argument is not a valid Image resource in E:\Webserver\*******\*****\product_add.php on line 87
Warning: Cannot modify header information - headers already sent by (output started at E:\Webserver\*******\*****\product_add.php:83) in E:\Webserver\*******\*****\product_add.php on line 93
Raderna som är fetstilta ovan är de rader som ger fel.
(Använde kod-taggen eftersom php-taggen ger så konstiga radbrytningar).
Skulle behöva lite hjälp med att få ordning på det här, jag är ingen hejjare på PHP.
Tack!