rollOrDie Posted June 28, 2008 Share Posted June 28, 2008 I turned an image resizing script I coded previously; into a function so that I can re use the code when I like. The problem is, it doesn't really seem to do anything anymore! Im thinking, do I need to 'return' something at the end? I dont see why I would though? Here is my code! PHP Code: <?php function imgResize ($fieldName, $width, $maxHeight) { // temporary file created by php $imgUpload = $_FILES[$fieldName]['tmp_name']; // create an image from it to be resized $imgSrc = imagecreatefromjpeg ($imgUpload); // Capture the original size of the uploaded image $size = array (getimagesize ($imgUpload)); $width = $size[0]; $height = $size[1]; // define new dimensions $newWidth = $width; $newHeight = ($height / $width) * $newWidth; // height must be no more than $maxHeight if($newHeight > $maxHeight) { $newHeight = $maxHeight; $newWidth = ($width / $height) * $newHeight; } // create true color image $imgDest = imagecreatetruecolor ($newWidth, $newHeight); // resize $imgSrc and turn into $imgDest imagecopyresampled ($imgDest, $imgSrc, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // write to disk $destination = '../mattPealingDesign/uploadImg/uploadedImage.jpg'; if (imagejpeg ($imgDest, $destination, 100)) { echo 'Upload Successful'; } else { echo 'error'; } } I should also point out that Im calling the function in another script with: PHP Code: <?php include "../script/php/imgResize.php"; imgResize ('portImg', '800', '1000'); ?> __________________ Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 Well does it echo 'Upload Successful' or 'error' at the end? Also, have you tried enabling errors and seeing if this can shed any light on your problem? Quote Link to comment Share on other sites More sharing options...
rollOrDie Posted June 29, 2008 Author Share Posted June 29, 2008 Nope, nothing is echoed and the image isn't transferred. Illl try changing the error reporting! Thanks. Quote Link to comment Share on other sites More sharing options...
rollOrDie Posted June 29, 2008 Author Share Posted June 29, 2008 I had several code issues which Ive fixed: <?php function imgResize ($fieldName, $width, $maxHeight) { // temporary file created by php $imgUpload = $_FILES[$fieldName]['tmp_name']; // create an image from it to be resized $imgSrc = imagecreatefromjpeg ($imgUpload); // Capture the original size of the uploaded image $size = array (getimagesize ($imgUpload)); $origWidth = $size[0]; $origHeight = $size[1]; // define new dimensions $newWidth = $width; $newHeight = ($origHeight / $origWidth) * $newWidth; // height must be no more than $maxHeight if($newHeight > $maxHeight) { $newHeight = $maxHeight; $newWidth = ($origWidth / $origHeight) * $newHeight; } // create true color image echo '<p>' . $newWidth . ' ' . $newHeight . '</p>'; $imgDest = imagecreatetruecolor ($newWidth, $newHeight); // resize $imgSrc and turn into $imgDest imagecopyresampled ($imgDest, $imgSrc, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight); // write to disk $destination = '../uploadImg/uploadedImage.jpg'; if (imagejpeg ($imgDest, $destination, 100)) { echo 'Upload Successful'; } else { echo 'error'; } } ?> But it still doesn't work! Here is the error generated: Notice: Undefined offset: 1 in C:\xampp\htdocs\xampp\mattPealingDesign\script\php\imgResize.php on line 14 Fatal error: Unsupported operand types in C:\xampp\htdocs\xampp\mattPealingDesign\script\php\imgResize.php on line 18 Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 To be honest I'm not too sure. The error is on the line where you perform a division and a mulitplication so all I can assume is that one of those must contain an erroneous value. To counter act this I've added some type casting a nd a small check to ensure you're not dividing by zero (although imagine if you were this would produce an error)! I can't promise this code will work but give it a try! <?php function imgResize ($fieldName, $width, $maxHeight) { // temporary file created by php $imgUpload = $_FILES[$fieldName]['tmp_name']; // create an image from it to be resized $imgSrc = imagecreatefromjpeg ($imgUpload); // Capture the original size of the uploaded image $size = array (getimagesize ($imgUpload)); $origWidth = (int)$size[0]; $origHeight = (int)$size[1]; if ($origHeight > 0 && $origWidth > 0) { // define new dimensions $newWidth = (int)$width; $newHeight = (($origHeight / $origWidth) * $newWidth); // height must be no more than $maxHeight if($newHeight > $maxHeight) { $newHeight = $maxHeight; $newWidth = ($origWidth / $origHeight) * $newHeight; } // create true color image echo '<p>' . $newWidth . ' ' . $newHeight . '</p>'; $imgDest = imagecreatetruecolor ($newWidth, $newHeight); // resize $imgSrc and turn into $imgDest imagecopyresampled ($imgDest, $imgSrc, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight); // write to disk $destination = '../uploadImg/uploadedImage.jpg'; if (imagejpeg ($imgDest, $destination, 100)) { echo 'Upload Successful'; } else { echo 'error'; } } } ?> Alternatively, here's a createThumbnail function I wrote myself a while back. I haven't used it in a while but if all else fails it'll be worth a go! <?php function createThumb($ipath, $tpath, $fname) { $info = pathinfo($ipath.$fname); $ext = strtolower($info['extension']); list($maxh, $maxw) = explode(":", _iSizeProds); if ($ext == 'jpg' || $ext == 'gif' ) { if ($ext == 'jpg') $img = imagecreatefromjpeg( "{$ipath}{$fname}" ); else $img = imagecreatefromgif( "{$ipath}{$fname}" ); $w = $width = imagesx( $img ); $h = $height = imagesy( $img ); $df = 1; if ($maxh < $width || $maxw < $width && $width > 0 && $height > 0) { while ($height > $maxh || $width > $maxw) { $width = $width - floor(((((($width/2)/2)/2)/2)/2)/2); $height = $height - floor(((((($height/2)/2)/2)/2)/2)/2); } $width = floor($width); $height = floor($height); } $tmp_img = imagecreatetruecolor($width, $height); imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $width, $height, $w, $h); if ($ext == 'gif') imagegif($tmp_img,"{$tpath}{$fname}"); elseif ($ext == 'jpg') imagejpeg( $tmp_img, "{$tpath}{$fname}" ); } } ?> Quote Link to comment Share on other sites More sharing options...
rollOrDie Posted June 29, 2008 Author Share Posted June 29, 2008 Thanks a lot ill give it a shot. Im really tired right now though so ill try tomorrow and get back to ya! lol Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 Yeah no worries. Let us know how it works out. Quote Link to comment Share on other sites More sharing options...
rollOrDie Posted June 30, 2008 Author Share Posted June 30, 2008 Hey, I managed to get it working in the end. The bit of code that used the array function shouldn't have been there! I have modified my pages to the following: Process.php <?php error_reporting (E_ALL); $script = $_POST['script']; include '../script/php/project/' . $script . '.php'; echo $resultMessage; ?> imgProcess.php <?php include "../script/php/imgResize.php"; global $fileName; global $destination; $imgResize = imgResize ('portImg', '200', '600'); if ($imgResize) { $resultMessage = '<p>' . $fileName . ' uploaded Successfully to "' . $destination . '"</p> <h2>' . $fileName . '</h2> <p><img src="' . $destination . $fileName . '" /></p>'; } else { $resultMessage = '<p>' . $fileName . ' could not be uploaded.</p>'; } ?> imgResize.php: <?php function imgResize ($fieldName, $width, $maxHeight) { // temporary file created by php $imgUpload = $_FILES[$fieldName]['tmp_name']; // create an image from it to be resized $imgSrc = imagecreatefromjpeg ($imgUpload); // capture the original size of the uploaded image $size = getimagesize ($imgUpload); $origWidth = $size[0]; $origHeight = $size[1]; // define new dimensions $newWidth = $width; $newHeight = round (($origHeight / $origWidth) * $newWidth); // height must be no more than $maxHeight if ($newHeight > $maxHeight) { $newHeight = $maxHeight; $newWidth = round (($origWidth / $origHeight) * $newHeight); } // create true color image from $imgSrc $imgDest = imagecreatetruecolor ($newWidth, $newHeight); // resize $imgSrc and turn into $imgDest imagecopyresampled ($imgDest, $imgSrc, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight); // write to disk $destination = '../uploadImg/'; $fileName = 'uploadedImage2.jpg'; imagejpeg ($imgDest, $destination . $fileName, 100); } ?> Everything seems to work fine, except the wrong result message is displayed! Although the image is resized and transferred successfully, the page seems to be echoing this part of the code: $resultMessage = '<p>' . $fileName . ' could not be uploaded.</p>'; only the $fileName variable is just blank; so Im presented with: could not be uploaded. Is this because Im trying to access variables from an included script? Can I not do that from making them 'global' as I have done in imgProcess.php? Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 30, 2008 Share Posted June 30, 2008 Your problem is you don't return the result from imgResize() so even though it succeeds, the $imgResize variables always holds NULL. Try this at the end of imgResize() return imagejpeg ($imgDest, $destination . $fileName, 100); I think the $fileName variable is always going to be empty because when the imgResize() function finished, all internal variables are destroyed. If you want the filename trying returning $fileName rather than what I said above. Also, I would modify the following portion of code to add some security checks. <?php error_reporting (E_ALL); $script = basename($_POST['script']); include '../script/php/project/' . $script . '.php'; echo $resultMessage; ?> While I've added a small security measure (and it's better than none) you should read up on PHP Upload security. Quote Link to comment Share on other sites More sharing options...
rollOrDie Posted June 30, 2008 Author Share Posted June 30, 2008 Thanks a lot for the help, Ill read up on it! Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 30, 2008 Share Posted June 30, 2008 No worries! Did it work then? If so, could you click 'Topic Solved' in the bottom left corner please? Quote Link to comment Share on other sites More sharing options...
rollOrDie Posted July 1, 2008 Author Share Posted July 1, 2008 hmmmm Ive modified my code like so: imagejpeg ($imgDest, $destination . $fileName, 100); return $fileName; return $destination; aswell as editing 'imageProcess.php' with: global $fileName; global $destination; But now I get this: Performing actions... uploaded Successfully to "" It seems to bring up the success message, but it still cant access what is in the $fileName and $destination variables? I'll get round to adding some security features later! Quote Link to comment Share on other sites More sharing options...
br0ken Posted July 2, 2008 Share Posted July 2, 2008 You can't return two values from one function. The only way you could accomplish that is like this: <?php // Last line of imgResize() return $fileName.",".$destination; // Calling imgResize() list($fileName,$destination) = explode(",", imgResize()); ?> Quote Link to comment Share on other sites More sharing options...
rollOrDie Posted July 2, 2008 Author Share Posted July 2, 2008 Ahh thanks a lot! 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.