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
https://forums.phpfreaks.com/topic/232388-issue-with-saving-uploaded-image-twice/
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.

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.

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

}	
}

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.