473,243 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,243 software developers and data experts.

Good image uploader with thumbnails?

I want to make a portfoliosystem where user can register and get their
own portfolio... I've started the developer work, but I'm stuck on the
image upload part... I'm experiencing some problems getting the picture
resized and thumbnailed... I'm on a apache server running php 5 with
8MB php_memory.

When uploading, the script works fine most times when uploading small
files (below 100kb and sometimes up close to 300kb too), but when
uploading files bigger than 300kb it only reloads the page, and the
form fields are empty.

Does any of you know of a smoothly working script for uploading,
resizing and thumbnailing? I need it to write a line to the database
too, but that's no problem writing... Excuse my bad english, but I'm
norwegian :)

If you're intrested, the code I have now is pasted below:

<?

ini_set("memory_limit","16M");

//Last inn global konfigurasjonsfil - denne filen må finnes for at
systemet skal laste
if(!file_exists("../global_config.php")){
echo("global_config.php finnes ikke");
die;
}
require("../global_config.php");

//Last inn globale funksjoner

if(!file_exists("../global_functions.php")){
echo("global_functions.php finnes ikke");
die;
}
require("../global_functions.php");

//Hent ut brukernavn

//Koble til databse

db_connect();
$dir = "temp/"; //Change this to the correct dir RELATIVE TO WHERE
THIS SCRIPT IS, or /full/path/

//MIME types to allow, Gif, jpeg, zip ::Edit this to your liking
$types =
array("image/png","image/x-png","image/gif","image/jpeg","image/pjpeg");
// Nothing to edit below here.

//Opprett thumbnail-funksjonen

function endre($name,$filename,$new_w,$new_h){
$system=explode('.',$name);
if (preg_match('/jpg|jpeg/',$system[1])){
$src_img=imagecreatefromjpeg($name);
}
if (preg_match('/png/',$system[1])){
$src_img=imagecreatefrompng($name);
}

$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thum b_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
//Check to determine if the submit button has been pressed
if(isset($_POST['submit'])) {

//Shorten Variables
$tmp_name = $_FILES['upload']['tmp_name'];
$new_name = $_FILES['upload']['name'];
$cat_id = $_POST['cat_id'];
$tittel = $_POST['tittel'];
$beskrivelse = $_POST['beskrivelse'];
$fullpath = "$dir$path/";
$temppath = "bilder/";
$fullpath = str_replace("..", "", str_replace("\.", "",
str_replace("//", "/", $fullpath)));
$clean_name = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_",
str_replace("%20", "_", strtolower($new_name) ) ) );

// lets see if we are uploading a file or doing a dir listing
//Sjekk MIME-typen
if ((in_array($_FILES['upload']['type'], $types)) and
(!file_exists($temppath.$clean_name))){

//Opprett mapper som er nødvendige
if (!is_dir($fullpath)){
mkdir("$fullpath", 0777);
}
if (!is_dir("thumb/")){
mkdir("thumb/", 0777);
}
if (!is_dir("temp/")){
mkdir("temp/", 0777);
}
//Flytt filen til /temp-mappen
move_uploaded_file($tmp_name,$fullpath . $clean_name);

endre("temp/$clean_name","thumb/thumb_$clean_name",125,125);
endre("temp/$clean_name","bilder/$clean_name",550,550);
//Slett originalfilen
$ourFileName = "temp/$clean_name";
$ourFileHandle = fopen($ourFileName, 'w') or die("kunne ikke
&aring;pne fil for sletting");
fclose($ourFileHandle);
unlink($ourFileName);
$query = "INSERT INTO `portfolio_brukere_doffer` (`id`, `cat_id`,
`url`, `url_full`, `tittel`, `beskrivelse`, `om`) VALUES ('',
'$cat_id', 'thumb/thumb_$clean_name', 'bilder/$clean_name', '$tittel',
'$beskrivelse', '')";
mysql_query($query) or die('Error, insert query failed');
echo "$clean_name ble lagt til";
}/*else{

//Print Error Message
echo "<small>Filen
<strong><em>{$_FILES['upload']['name']}</em></strong> finnes fra
f&oslash;r av</small><br />";
//Debug
$name = $_FILES['upload']['name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
$tmp = $_FILES['upload']['name'];

echo "Navn: $name<br />Type: $type<br />St&oslash;rrelse: $size<br
/>Tmp: $tmp";

} */
} else {
echo 'Ready to upload your file';
} ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">

<fieldset>
<legend>Last opp bilde</legend>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Bilde som skal lastes opp </td>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="upload" />&nbsp;</td>
</tr>
<tr>
<td>Tittel</td>
<td><input name="tittel" type="text" maxlength="100">&nbsp;</td>
</tr>
<tr>
<td>Kategori</td>
<td>
<select name="cat_id">
<option selected>Velg en kategori</option>
<option>- - -</option>
<?
$kategorier = "SELECT * FROM `portfolio_cat` LIMIT 0 , 100";
$cat_resultat = mysql_query($kategorier);
while ($row= @mysql_fetch_array($cat_resultat)) {
$cat_id = $row["id"]; //hent ut kategori-id
$cat_navn = $row["name"]; //hent ut kategori-navn

//Send informasjonen ut til leseren
echo("<option value=\"$cat_id\">$cat_navn</option>");
$count++ ;
}
?>
</select>
</td>
</tr>
<tr>
<td>Beskrivelse</td>
<td><textarea name="beskrivelse"></textarea></td>
</tr>
</table>

<input type="submit" name="submit" value="Last opp" />
</fieldset>
</form>

Jul 28 '05 #1
0 1771

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
8
by: Michael Satterwhite | last post by:
I'm opening a page that has a single image on it. I'd like to resize the window containing the image to the size of the image (slightly larger OK, not smaller). The width isn't a problem, but the...
0
by: grahamhe | last post by:
Hi, I have recently upgraded .NET from 1.0 to 1.1 and now an application I am writing is no longer uploading the files, any ideas? I have posted the relevant code below... <%@ Page...
2
by: PhoenixFalconhand | last post by:
Begginners' question: I am trying to make a small webform application that doesn't use frames. The application is supposed to show some thumbnails on the left, and whenever you click on one of...
2
by: IkBenHet | last post by:
Hello, I want to make thumbnails from images. I found this code and modified it and it works fine: <%@Import Namespace="System.Drawing.Imaging" %> <script language="VB" runat="server">...
8
by: Nak | last post by:
Hi there, I've come across quite an annoying bug after optimizing some of my code (which sounds quite strange I know), but take this for a description of my problem. * I have a background...
4
by: RE Kochanski | last post by:
I have attempted to use the CSS techniques from two or three sites to create a CSS only image gallery. I am muddling the affair by placing the thumbnails in one float, the page text in another...
2
ak1dnar
by: ak1dnar | last post by:
Hi, Future Photo is using Basic uploader and advanced uploader to upload images to there albums, My question is how to build up this kind of advanced uploader in php. As i feel its related with ...
2
by: harikrishnan4u | last post by:
How can we check the image size in rad uploader this file uploader is in the rad editor??any one Know
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.