wwfc_barmy_army Posted April 1, 2011 Share Posted April 1, 2011 Hello. I'm using an Amazon S3 class to uploaded to S3. I have 2 upload boxes - The first uploads once and the second needs to upload twice - 1 full size, 1 thumb. The issue i'm having is that the 2nd image (the thumb) seems to be failing, although if I don't save the first full sized image I am able to upload the thumb. So I think the issue is with using the temp file twice? This is my code: //retreive post variables $fileName = $randomString . "_" . $_FILES['theFile']['name']; $fileTempName = $_FILES['theFile']['tmp_name']; $fileName2 = $randomString . "_" . $_FILES['theFile2']['name']; $fileTempName2 = $_FILES['theFile2']['tmp_name']; //move the file if ($s3->putObjectFile($fileTempName, "containerhere", $fileName, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } //move the file if ($s3->putObjectFile($fileTempName2, "containerhere", $fileName2, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } include('simpleImage.php'); $image = new SimpleImage(); $image->load($_FILES['theFile2']['tmp_name']); $image->resizeToWidth(100); $image->save($_FILES['theFile2']['tmp_name']); $fileName3 = $randomString . "_" . $_FILES['theFile2']['name']; $fileTempName3 = $_FILES['theFile2']['tmp_name']; //move the file if ($s3->putObjectFile($fileTempName3, "containerhere", "thumbs/" . $fileName3, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } Can anyone offer any advice? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/ Share on other sites More sharing options...
j9sjam3 Posted April 1, 2011 Share Posted April 1, 2011 Wouldn't it be easier, more user friendly and faster to upload the full size image, and resize it in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195439 Share on other sites More sharing options...
wwfc_barmy_army Posted April 1, 2011 Author Share Posted April 1, 2011 Sorry I should have explained better. The second image is resized. The First upload ($_FILES['theFile']['tmp_name']) is unrelated. Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195440 Share on other sites More sharing options...
j9sjam3 Posted April 1, 2011 Share Posted April 1, 2011 Did you get any error messages? Could you also include include('simpleImage.php'); Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195442 Share on other sites More sharing options...
wwfc_barmy_army Posted April 1, 2011 Author Share Posted April 1, 2011 Did you get any error messages? Could you also include include('simpleImage.php'); It's over there - http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php The error I get is: Warning: S3::putObject(): [RequestTimeout] Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed. in pathtofile.php on line 358 Something went wrong while uploading your file... sorry. The original (none resized image) uploads in seconds. Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195443 Share on other sites More sharing options...
j9sjam3 Posted April 1, 2011 Share Posted April 1, 2011 What file is "pathtofile.php"? Are you accidentally trying to save to that file? Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195444 Share on other sites More sharing options...
wwfc_barmy_army Posted April 1, 2011 Author Share Posted April 1, 2011 What file is "pathtofile.php"? Are you accidentally trying to save to that file? Thats just the file that the script is running from. Just didn't want to post my full path to the file thats all Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195447 Share on other sites More sharing options...
wwfc_barmy_army Posted April 1, 2011 Author Share Posted April 1, 2011 I think is issue is thats it's trying to use the temp file twice. After the first use does the temp image file get removed? I've put in another upload box for the same image and it works, but ideally I want to do it from 1 upload box. Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195450 Share on other sites More sharing options...
j9sjam3 Posted April 1, 2011 Share Posted April 1, 2011 Instead of: include('simpleImage.php'); $image = new SimpleImage(); $image->load($_FILES['theFile2']['tmp_name']); $image->resizeToWidth(100); $image->save($_FILES['theFile2']['tmp_name']); Use: include("resize_image.php"); $image = new resizeImage;' $thumbnail = $image->resize($file, 100); if ($s3->putObjectFile($thumbnail, "containerhere", "thumbs/" . $fileName3, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } resize_image.php <?php class resizeImage { protected $image, $toWidth, $toHeight; public function __construct($original_image, $toWidth = 200, $toHeight = 200) { $this->image = $original_image; $this->toHeight = $toHeight; $this->toWidth = $toWidth; } public function resize() { list($width, $height) = getimagesize($this->image); $xscale = $width / $this->toWidth; $yscale = $height / $this->toHeight; if($yscale > $xscale){ $new_width = round($width * (1 / $yscale)); $new_height = round($height * (1 / $yscale)); } else { $new_width = round($width * (1 / $xscale)); $new_height = round($height * (1 / $xscale)); } $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $imageResized; } public function __destruct() { } } Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195455 Share on other sites More sharing options...
wwfc_barmy_army Posted April 1, 2011 Author Share Posted April 1, 2011 Lots of errors: Warning: Missing argument 1 for resizeImage::__construct(), called in C:\xampp\htdocs\test\index.php on line 155 and defined in C:\xampp\htdocs\test\resize_image.php on line 4 Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in C:\xampp\htdocs\test\resize_image.php on line 11 Warning: Division by zero in C:\xampp\htdocs\test\resize_image.php on line 18 Warning: Division by zero in C:\xampp\htdocs\test\resize_image.php on line 19 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\xampp\htdocs\test\resize_image.php on line 21 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in C:\xampp\htdocs\test\resize_image.php on line 22 Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\resize_image.php on line 23 Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195458 Share on other sites More sharing options...
j9sjam3 Posted April 1, 2011 Share Posted April 1, 2011 Resize_image.php <?php class resizeImage { protected $image, $toWidth, $toHeight; public function __construct($original_image, $toWidth = 200, $toHeight = 200, $autoresize = true) { if(is_null($original_image)) { die("Missing first argument"); } $this->image = $original_image; $this->toHeight = $toHeight; $this->toWidth = $toWidth; $autoresize == true ? $this->resize() : false; } public function resize() { list($width, $height) = getimagesize($this->image); $xscale = $width / $this->toWidth; $yscale = $height / $this->toHeight; if($yscale > $xscale){ $new_width = round($width * (1 / $yscale)); $new_height = round($height * (1 / $yscale)); } else { $new_width = round($width * (1 / $xscale)); $new_height = round($height * (1 / $xscale)); } $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $imageResized; } public function __destruct() { } } <?php // stuff before include("resize_image.php"); $image = new resizeImage($file, 100); if ($s3->putObjectFile($thumbnail, "containerhere", "thumbs/" . $fileName3, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } Quote Link to comment https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/#findComment-1195461 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.