Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help! PHP image uploader/thumbnailer not working. why?

Help! PHP image uploader/thumbnailer not working. why?
Thread Tools
MacMan4000
Mac Elite
Join Date: Jun 2004
Status: Offline
Reply With Quote
Aug 30, 2005, 12:05 AM
 
Forgive me if this answer is posted already somwhere, but i didn't know what to search for.

I am trying to make my own image uploader that will basically do exactly what imageshack.us does. It will upload the image, then make a thumbnail of it. but I am getting errors that don't seem to make much sense. here's my code (with the password part changed to keep my real password secret):

[php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Image Uploader</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="imagefile">
<br>
<input type="password" name="pass" value="password" >
<input type="submit" name="Submit" value="Submit">

<?php

if(isset( $Submit )) { //If the Submitbutton was pressed do:

if($pass != "pancakes"){ //check the password
echo "incorrect password";
} else {
if ($_FILES['imagefile']['type'] == "image/gif" or $_FILES['imagefile']['type'] == "image/jpeg" or $_FILES['imagefile']['type'] == "image/png"){
//check file type
copy ($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile']['name']) //save the image
or die ("Could not copy");
$file = $_FILES['imagefile']['name'];
echo "<br>";
echo "Name: ".$file."<br>";
echo "Size: ".round((($_FILES['imagefile']['size'])/1024),2)." kb<br>";
echo "Type: ".$_FILES['imagefile']['type']."<br>";
echo "Upload Complete...<br>";
echo "<a href=\"files/".$file."\"><img src=files/".$file."></a><br>";
echo "inline image: <input name=\"bb\" type=\"text\" id=\"bb\" value=\" \" size=\"50\"><br>";
echo "link to image: <input name=\"bb2\" type=\"text\" id=\"bb2\" value=\"Click here!\" size=\"50\">";

##############################
//start thumbnail creator here:

$filename = "files/".$file;

// 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, substr($filename,0,(strlen($filename-4))."tn.jpg", 90);

}
else {
echo "<br><br>";
echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>";
echo $_FILES['imagefile']['type'];
}
}
}
?> </form>

</body>
</html>
[/php]

with this code I get the following error:
Parse error: parse error, unexpected ';' in /path/to/page.php on line 63
if I comment out this line:
imagejpeg($image_p, substr($filename,0,(strlen($filename-4))."tn.jpg", 90);
it loads correctly, but doesn't make the thumbnail obviously.

I tried simply removing the ";" at the end of the line (I know that doesn't make sense but it was worth a try), and that only resulted in more errors.

ANyone have any suggestions? This is probably something really obvious, but I'm just overlooking it. Thanks in advance.
( Last edited by MacMan4000; Aug 30, 2005 at 01:22 AM. )
     
MacMan4000  (op)
Mac Elite
Join Date: Jun 2004
Status: Offline
Reply With Quote
Aug 30, 2005, 12:25 AM
 
well, of course, as soon as i post that massive question i figure it out, kinda.

If i change the problem line from:
imagejpeg($image_p, substr($filename,0,(strlen($filename-4))."tn.jpg", 90);
to
imagejpeg($image_p, $filename."tn.jpg", 90);
it works fine, but i don't exactly want all my thumbnails to be named things like "image.jpgtn.jpg". I'd like to remove the first ".jpg" before i add the "tn.jpg" to the end.

This is probably a much easier question. But I'm still stumped.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 30, 2005, 12:35 AM
 
You'll want to smack yourself when I tell you this.

You're missing a closing parenthesis on that line. I think you want (strlen($filename)-4).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
MacMan4000  (op)
Mac Elite
Join Date: Jun 2004
Status: Offline
Reply With Quote
Aug 30, 2005, 01:21 AM
 
Originally Posted by Chuckit
You'll want to smack yourself when I tell you this.

You're missing a closing parenthesis on that line. I think you want (strlen($filename)-4).
I knew it was something obvious. Now i feel like an idiot.

Thanks for the quick answer.


edit after some more tweeking my uploader/thumbnailer is working like a champ. if anyone wants the finished code, let me know and i will post it. the code in the top post is pretty buggy, even after you fix that one line.
     
selowitch
Mac Elite
Join Date: Nov 2003
Location: Rockville, MD
Status: Offline
Reply With Quote
Sep 7, 2005, 11:07 AM
 
Originally Posted by MacMan4000
edit after some more tweeking my uploader/thumbnailer is working like a champ. if anyone wants the finished code, let me know and i will post it. the code in the top post is pretty buggy, even after you fix that one line.
Yes, please. Looks useful! Does it leverage ImageMagick, or does it do its work by some other mechanism?
     
Jasoco
Mac Elite
Join Date: May 2002
Location: Home in front of my computer
Status: Offline
Reply With Quote
Sep 16, 2005, 03:22 AM
 
Yes, please do! I'd love to see this as well.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:28 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,