smc Posted January 27, 2007 Share Posted January 27, 2007 Hello,I would like to add an upload function when submitting an article on my website that allows to upload a picture. Now this picture will need to be resized to a certain dimension for the physical article and another will need to be resized to be a thumbnail.I've googled but not found a reliable source of information on how to resize a picture.Thanks!! Quote Link to comment Share on other sites More sharing options...
Hypnos Posted January 27, 2007 Share Posted January 27, 2007 http://us3.php.net/manual/en/function.imagecopyresampled.php Quote Link to comment Share on other sites More sharing options...
smc Posted January 27, 2007 Author Share Posted January 27, 2007 I could use a bit more guidence. I'm finding the provided examples a bit murkyMy goal is to upload the image and as it's uploaded to resize it, then to copy it to a new directory with the name $articletitle . "_pic" or $articletitle . "_thumb" so I can store the link to it and call the link from the databaseThanks Quote Link to comment Share on other sites More sharing options...
Hypnos Posted January 27, 2007 Share Posted January 27, 2007 This is a function and test script that I wrote a while back for a friend who wanted to do something similar:[code]<html><body><?phpfunction resize_jpg($inputFilename, $new_h, $new_w, $outputFilename){ echo $inputFilename; $imagedata = getimagesize($inputFilename); $w = $imagedata[0]; $h = $imagedata[1]; $im2 = ImageCreateTrueColor($new_w, $new_h); $image = ImageCreateFromJpeg($inputFilename); imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $imagedata[0], $imagedata[1]); touch($outputFilename); imagejpeg($im2, $outputFilename);}if (!($_POST)) {?> <form name="form1" method="post" action="" enctype="multipart/form-data"> <input type="file" name="imagefile"> <br> <input type="submit" name="Submit" value="Submit"> </form><?php}else { if ($_FILES['imagefile']['type'] == "image/jpeg") { resize_jpg($_FILES['imagefile']['tmp_name'], 300, 300, "rox.jpg"); }}?></body></html>[/code]A problem you'll hit if you use the function as it is, is that it will fail if the file is already there. You can modifiy it to delete, or rename.But, hopefully you won't just copy and paste it, and this will help you understand how the functions work, and what functions to use. Quote Link to comment Share on other sites More sharing options...
smc Posted January 28, 2007 Author Share Posted January 28, 2007 Can you detect a function failing?Like I could always do a function saying[code]if ( resize failed ) { delete the current picture on file; reupload the picture}[/code] Quote Link to comment Share on other sites More sharing options...
Hypnos Posted January 28, 2007 Share Posted January 28, 2007 Yes. Most functions return a FALSE bool when they fail. So stuff like:[code=php:0]if(!$imagedata = getimagesize($inputFilename)) die('WTF are you trying to upload??');[/code]And it will still set $imagedata correctly, but can be used for error handling of sorts. Quote Link to comment Share on other sites More sharing options...
smc Posted January 28, 2007 Author Share Posted January 28, 2007 When attempting to use your function I get thisWarning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/een/public_html/functions.php on line 205Nothing I adapted deals with that function so I'm not sureEDIT: To help further here are specific instancesfunctions.php[code]function resize_jpg($inputFilename, $new_h, $new_w, $outputFilename){// chmod("images", 777); echo $inputFilename; $imagedata = getimagesize($inputFilename); $w = $imagedata[0]; $h = $imagedata[1]; $im2 = ImageCreateTrueColor($new_w, $new_h); $image = ImageCreateFromJpeg($inputFilename); imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $imagedata[0], $imagedata[1]); touch($outputFilename); imagejpeg($im2, $outputFilename);// chmod("images", 755);}[/code]actual doc calling it[code]if ( $picture != "" || $_FILES['picture']['type'] == "image/jpeg" ){ $outputFilename = $titledb . "_pic.jpg"; $inputFilename = $_FILES['picture']['tmp_name']; resize_jpg($inputFilename, 240, 340, $outputFilename); $picturedb = "http://eagleeye5.com/images/" . $outputFilename;}else{ $picturedb = "false";}[/code] Quote Link to comment Share on other sites More sharing options...
Hypnos Posted January 28, 2007 Share Posted January 28, 2007 I really didn't intend for that function to be reused... Did it at least echo a temp filename? If not, make sure you used the right var. Quote Link to comment Share on other sites More sharing options...
smc Posted January 28, 2007 Author Share Posted January 28, 2007 No it didn't echo. Granted I'm not totally sure about $inputFilename = $_FILES['picture']['tmp_name'];I used that from your example. I assume FILES and tmp_name are PHP resources but if not that would explain it Quote Link to comment Share on other sites More sharing options...
Hypnos Posted January 28, 2007 Share Posted January 28, 2007 $_FILES is like $_POST. It's for file upload form results.Notice that on my form, the file upload field is named "imagefile", then the array key in $_FILES is also "imagefile".http://www.tizag.com/phpT/fileupload.php Quote Link to comment Share on other sites More sharing options...
smc Posted January 28, 2007 Author Share Posted January 28, 2007 Yeah my file upload form is called picture so that reflects it in my functions.What about tmp_name, is that a resource? Quote Link to comment Share on other sites More sharing options...
smc Posted January 28, 2007 Author Share Posted January 28, 2007 I was actually using the wrong name after all. I fixed it and it no longer generates errors but unfortunatly it doesn't echo the input file eithier. Any ideas?Also, if anyone has any other methods I'm open to hear them. I'm stuck on this issue Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.