Jump to content

Issue with saving uploaded image twice


wwfc_barmy_army

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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() {

}	
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.