Jump to content

Image upload process & saving


acctman

Recommended Posts

The image is processed and being held in buffer $imagevariable, how do set it so the last process saves the buffered image to a file after displaying it

 

<?php

// Get the session Id passed from Upload. 
if (isset($_POST["PHPSESSID"])) {
	session_id($_POST["PHPSESSID"]);
} else if (isset($_GET["PHPSESSID"])) {
	session_id($_GET["PHPSESSID"]);
}

session_start();

// Check the upload
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
	header("HTTP/1.1 500 Internal Server Error");
	echo "invalid upload";
	exit(0);
}

// Get the image and create a thumbnail
$img = @imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
if (!$img) {
	header("HTTP/1.1 500 Internal Server Error");
	echo "could not create image handle";
	exit(0);
}

$width = imageSX($img);
$height = imageSY($img);

// Check if image is too small
if ($width < 160 || $height < 120 ) {
	header("HTTP/1.1 200 Internal Server Error");
	echo "Invalid width or height. Your image is way to small.";
	exit(0);
}

// Resize if image is bigger than 640x480		
	if ($width > 640 || $height >= 480 ) {

// Build the thumbnail
$target_width = 640;
$target_height = 800;
$target_ratio = $target_width / $target_height;

$img_ratio = $width / $height;

if ($target_ratio > $img_ratio) {
	$new_height = $target_height;
	$new_width = $img_ratio * $target_height;
} else {
	$new_height = $target_width / $img_ratio;
	$new_width = $target_width;
}

if ($new_height > $target_height) {
	$new_height = $target_height;
}
if ($new_width > $target_width) {
	$new_height = $target_width;
}

$new_img = ImageCreateTrueColor($new_width, $new_height);

if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/0, ($target_height-$new_height)/0, 0, 0, $new_width, $new_height, $width, $height)) {
	header("HTTP/1.0 500 Internal Server Error");
	echo "Could not resize image";
	exit(0);
}

if (!isset($_SESSION["file_info"])) {
	$_SESSION["file_info"] = array();
}

// Use a output buffering to load the image into a variable
ob_start();
imagejpeg($new_img);
$imagevariable = ob_get_contents();
ob_end_clean();

$file_id = md5($_FILES["Filedata"]["tmp_name"] + rand()*100000);

$_SESSION["file_info"][$file_id] = $imagevariable;

echo $file_id;	// Return the file id to the script

// Resize if image is bigger than 640x480
} elseif ($width <= 640 and $width >= 160 and $height <= 480 and $height >= 120) {

	if (!isset($_SESSION["file_info"])) {
	$_SESSION["file_info"] = array();
}

// Use a output buffering to load the image into a variable
ob_start();
imagejpeg($img);
$imagevariable = ob_get_contents();
ob_end_clean();

$file_id = md5($_FILES["Filedata"]["tmp_name"] + rand()*100000);

$_SESSION["file_info"][$file_id] = $imagevariable;


echo $file_id;	// Return the file id to the script


}	


?>

Link to comment
https://forums.phpfreaks.com/topic/119735-image-upload-process-saving/
Share on other sites

would anyone like to work for a small fee? I have a few minor errors and coding questions on a site i've been working on for almost 2yrs. its taking me took long (weeks) to figure out simple coding errors. so if anyone would like to assist me please pm me. I can offer $10 per hour/per fix, anything that needs fixing shouldn't take more than a few mins to fix though

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.