Jeg har normalt ikke noget problem med at få vist billeder gemt i en tabel - denne gang har jeg resized billederne og så kan jeg ikke få dem vist...
Er der en her der kan skimme koden igennem for fejl eller har en koden der kan vise indhold uploaded som så ->
<?php
/*
CREATE TABLE imageblob(
imageID TINYINT PRIMAYR KEY AUTO_INCREMENT,
image_type VARCHAR(11),
image MEDIUMBLOB
);
*/
function scaleImageFileToBlob($file){
$source_pic = $file;
$max_width = 200;
$max_height = 200;
list($width, $height, $image_type) = getimagesize($file);
switch($image_type){
case 1: $src = imagecreatefromgif($file); break;
case 2: $src = imagecreatefromjpeg($file); break;
case 3: $src = imagecreatefrompng($file); break;
default: return ''; break;
}
$x_ratio = $max_width/$width;
$y_ratio = $max_height/$height;
if(($width <= $max_width) && ($height <= $max_height)){
$tn_width = $width;
$tn_height = $height;
} elseif(($x_ratio*$height) < $max_height){
$tn_height = ceil($x_ratio*$height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio*$height);
$tn_height = $max_height;
}
$tmp = imagecreatetruecolor($tn_width, $tn_height);
if(($image_type == 1) or ($image_type == 3)){
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, $tn_width, $tn_height, $transparent);
}
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
ob_start();
switch($image_type){
case 1: imagegif($tmp); break;
case 2: imagejpeg($tmp, NULL, 100); break;
case 3: imagepng($tmp, NULL, 0); break;
default: echo ''; break;
}
$final_image = ob_get_contents();
ob_end_clean();
return $final_image;
}
?>
<?php
if(isset($_POST["submit"])){
if($_FILES["image"]["name"] != ''){
$image = scaleImageFileToBlob($_FILES["image"]["tmp_name"]);
if($image == ''){
echo "Filtype ikke understøttet";
} else {
$image_type = $_FILES["image"]["type"];
$image = addslashes($image);
$query = "INSERT INTO imageblob(image_type, image)".
" VALUES('$image_type', '$image')";
$result = mysql_query($query)or die(mysql_error());
if($result){
echo "Scaleret billede er uploaded";
} else {
echo "Fejl i query";
}
}
}
}
?>
<form method="post" action="" enctype="multipart/form-data">
<table>
<tr>
<td>Vælge billede</td>
<td><input type="file" name="image" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value=" Gem billede " /></td>
</tr>
</table>
</form>
<?php
if(isset($_GET["image_id"])){
$foresp = mysql_query("SELECT * FROM imageblob WHERE imageID=".$_GET["image_id"]);
while($post = mysql_fetch_array($foresp)){
header("Content-type: $post[image_type]");
header("Content-disposition: attachment");
echo "$post[image]";
exit();
}
}
?>