| Image upload. Resize the save to dir. Help needed
|
glenrogers

msg:4491952 | 3:16 pm on Sep 6, 2012 (gmt 0) | Hi. Just found this forum, seems good so I joined! I'm a php newbie and need help! I have a form which uploads an image to a direcory, it came out of a book I just bought:Php and MySql for dynamic websites, 4th Edition by Larry Ullman. Here is the code upload_image.php
<DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w3.org/TR//xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Upload an image</title> <style type="text/css" title="text/css" media="all"> .error{ font-weight: bold; color:#c00; } </style> </head> <body> <form enctype="multipart/form-data" action="upload_image.php" method="post"> <fieldset><legend>Select an image file</legend> <p><b>File:</b> <input type="file" name="upload" /></p> </fieldset> <input type="submit" name="submit" value="Submit" /> </div> </form> </body> </html> <?php //Check if the form has been submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') { //check for an uploaded file if (isset($_FILES['upload'])) { //validate the type, jpg or png $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png'); if (in_array($_FILES['upload']['type'], $allowed)) { //move the file over if (move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']}")) { echo '<p><em>The file has been uploaded</em></p>'; }//end of move if } else { echo '<p><em>Uploaded file must be an image. File not uploaded.</em></p>'; } }//end of isset if
//check for error if ($_FILES['upload']['error'] > 0) { echo '<p class="error">The file could not be uploaded because:<strong>'; //print a message based upon the error switch ($_FILES['upload']['error']) { case 1: print 'The file exceeds the max_upload_size in php.ini.'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting in the html form'; break; case 3: print 'The file has only been partially uploaded'; break; case 4: print 'No file was uploaded'; break; case 6: print 'No temporary folder was available'; break; case 7: print 'Unable to write to the hard disk'; break; case 8: print 'File upload stopped'; break; default: print'A system error occured'; break; }//end of switch
print '<?strong></p>'; }//end of error if //delete the file if it still exists if (file_exists ($_FILES['upload']['tmp_name']) && is_file ($_FILES['upload']['tmp_name'])) { unlink ($_FILES['upload']['tmp_name']); } } ?> I also want to resize the images before I upload them to dir and have this code for resizing resize.php
<?php // File and new size $filename = 'rotate2.jpg';//1.HOW DO I MAKE THIS TAKE THE IMAGE THAT HAS BEEN UPLOADED THROUGH THE FORM? $percent = .55;
// Content type header('Content-Type: image/jpeg');
// Get new sizes list($width, $height) = getimagesize($filename); $newwidth = $width * $percent; $newheight = $height * $percent;
// Load $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename);
// Resize imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output imagejpeg($thumb); Can anyone tell me how to resize the image before putting it in the dir? Can I somehow use this resize.php? Thanks for looking Glen.....
|
swa66

msg:4492086 | 9:55 pm on Sep 6, 2012 (gmt 0) | | I also want to resize the images before I upload them to dir and have this code for resizing |
| That's going to be a problem to achieve in php: the php code you create runs on the server so it cannot resize anything before you upload it. (it can after it was uploaded easy enough). To get to the file before it is uploaded: you need to have code that runs in the browser. E.g. javascript (ajax), flash, ...
|
glenrogers

msg:4492348 | 3:25 pm on Sep 7, 2012 (gmt 0) | Well I suppose I could resize it after upload. Can you offer any advice on doing this? Like I said, I'm a total newbie! Thanks..................
|
glenrogers

msg:4492399 | 4:56 pm on Sep 7, 2012 (gmt 0) | Or any advice on how to resize first with javascript then add to the dir with php? cheers...............
|
swa66

msg:4492426 | 5:51 pm on Sep 7, 2012 (gmt 0) | imagecopyresampled is the GD high quality image resize function. It operates on images in variables, so you need to get the input and output functions jsut as well. I'd be careful doing this for extremely large images (memory usage is non-trivial). //read source: $image_src=imagecreatefromjpeg("filename"); //resize a file: $image_dst=imagecreatetruecolor($dst_width, $dst_height); imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); //write the file imagejpeg($dst_image, "filename, including directory", $jpeg_quality); Should do the trick. There's also imagecreatefromgif(), imagecreatefrompng(), getimagesize() Ref: [php.net...] [php.net...] [php.net...] [php.net...]
|
penders

msg:4493121 | 12:20 am on Sep 10, 2012 (gmt 0) | | any advice on how to resize first with javascript... |
| I believe this is possible with HTML 5 and the CANVAS element.
|
brotherhood of LAN

msg:4493128 | 1:19 am on Sep 10, 2012 (gmt 0) | Thanks for sharing penders, I want to look into that option. There's also flash scripts that'll resize/shrink filesizes before uploading to the server.... quite handy in regards to images as people can tend to upload MByte sizes photos that should really be KBytes in size.
|
|
|