Jump to content

Help with cleaning up coding


acctman

Recommended Posts

Hi I need assistants cleaning up the coding within the commented section in the middle. This is an Upload.php once a user uploads the file is checked, then if the file is not a .jpg then it is converted to .jpg and then processed as a thumbnail

 

<?php

if (isset($_POST["PHPSESSID"])) {
	session_id($_POST["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 File Extension
preg_match('/\.([^\.]+)$/',$_FILES["Filedata"]["tmp_name"],$out);
$ext = $out[1];  

// Convert Image to .jpg 
if($ext == "jpg" || $ext == "jpeg" || $ext == "pjpeg") {
          
          $image = imagecreatefromjpeg($picture); 
          
            } elseif($ext == "gif") {
            
          $image = imagecreatefromgif($picture); 

// Save converted image
header ("Content-type : image/jpg");
ImageJpeg($image);
ImageJpeg($image, "image.jpg");  // this needs to be $_FILES["Filedata"]["tmp_name"] . jpg
ImageDestroy($image);
//////////////////////////////////////////////////////////////////////////////////

// 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);

if (!$width || !$height) {
	header("HTTP/1.1 500 Internal Server Error");
	echo "Invalid width or height";
	exit(0);
}

// Build the thumbnail
$target_width = 100;
$target_height = 100;
$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(100, 100);
if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0)) {	// Fill the image black
	header("HTTP/1.1 500 Internal Server Error");
	echo "Could not fill new image";
	exit(0);
}

if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 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

?>

Link to comment
https://forums.phpfreaks.com/topic/102817-help-with-cleaning-up-coding/
Share on other sites

Is there anything wrong with it?  Or do you just want it to be more concise?

 

this section below needs to be cleaned up. the image save part i need for it to resave the changed image with the tmp_name.jpg

 

////////////////////////////////////////////////////////////////////////////////////// 
// Get File Extension
preg_match('/\.([^\.]+)$/',$_FILES["Filedata"]["tmp_name"],$out);
$ext = $out[1];  

// Convert Image to .jpg 
if($ext == "jpg" || $ext == "jpeg" || $ext == "pjpeg") {
          
          $image = imagecreatefromjpeg($picture); 
          
            } elseif($ext == "gif") {
            
          $image = imagecreatefromgif($picture); 

// Save converted image
header ("Content-type : image/jpg");
ImageJpeg($image);
ImageJpeg($image, "image.jpg");  // this needs to be $_FILES["Filedata"]["tmp_name"] . jpg
ImageDestroy($image);
//////////////////////////////////////////////////////////////////////////////////

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.