webForumDet fria alternativet
Logga in / Bli medlem

Bilduppladdning - PHP/XML

PHP

1 svar · 1 676 visningar · startad av Jonny Strömberg

Medlem sedan maj 20061 inlägg
Trådstart#1

Har googlat lite nu men hittar inte det jag söker.

Jag skulle behöva ett gratis bilduppladdningsskript som dels laddar upp bilden och dels skapar en komprimerad och resizad thumb.

Språket är PHP och all information ska sparas i XML.

Medlem sedan dec. 1999286 inlägg
#2

Välkommen till webForum! :)

Här är två exempel på hur filuppladdning resp. storleksförändring av bild i php kan göras. Hämtad ur php-manualen för "Handling file uploads" resp. funktionen "imagecopyresampled".

Filuppladdning:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

Ersätt _URL_ med namnet på din php-fil som tar hand om filuppaddningen.

Storleksförändring av bild, resampling an image proportionally. This example will display an image with the maximum width, or height, of 200 pixels:

<?php
// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

if ($width && ($width_orig < $height_orig)) {
    $width = ($height / $height_orig) * $width_orig;
} else {
    $height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>

Beroende på vilken storlek tumnageln ska ha kanske du måste skriva om get new dimensions-delen. Återkom om du inte får ordning på det eller filuppladdningen.

379 ms totalt · 4 externa anrop · v20260731065814-full.51f67c91
125 ms — deklarationer (db)
116 ms — hämta statistik (db)
135 ms — hämta tråd, inlägg och bilagor (db)
126 ms — ändringar (db)