Jump to content

Image upload and resizing class


AdRock

Recommended Posts

I have had a go at creating a class becuase i want to learn how to do it and I have taken some old procedral code and put it into a class.

 

I know it's not going to work yet but hopefully haven't got far to fgo before it does.

 

I have worked with classes before (not written by me) and i understand how they work so that's why i'm a littl econfused with this.

 

I have made an image upload class using some old code i have and and not sure about is

$_FILES['image']['tmp_name'];

 

How do i pass that to my function so i can do the resizing etc and looking at my code have i got far to go before i can upload an image and resize it?

 

<?php
class Upload_image {
var $percent;
var $new_width;
var $new_height;
var $width;
var $height;

function Upload_Image($percent,$new_width,$new_height,$width,$height) {
	$this->percent = $percent;
	$his->new_width = $new_width;
	$this->new_height = $new_height;
	$this->width = $width;
	$this->height = $height;
}

function upload_image($percent, $new_width, $new_height, $width, $height) {
	$imagepath = $_FILES['image']['tmp_name']; 

	// Load image 
	$image = open_image($imagepath); 

	if ($image == false) { 
		return ('<strong>You uploaded an invalid image. Please go back and try again.</strong>'); 
	} 

	// Get original width and height 
	$width = imagesx($image); 
	$height = imagesy($image); 

	// Percentage? 
	if (!empty($percent)) { 
		$percent = floatval($percent); 
		$percent = $percent/100; 

		$new_width = $width * $percent; 
		$new_height = $height * $percent; 

	// New width? Calculate new height 
	} elseif (!empty($new_width)) { 
		$new_width = floatval($new_width); 
		$new_height = $height * ($new_width/$width); 

	// New height? Calculate new width 
	} elseif (!empty($new_height)) { 
		$new_height = floatval($new_height); 
		$new_width = $width * ($new_height/$height); 

	// New height and new width 
	} elseif (!empty($height) AND !empty($width)) { 
		$new_height = floatval($_POST['height']); 
		$new_width = floatval($_POST['width']); 
	}     

	// Resample 
	$image_resized = imagecreatetruecolor($new_width, $new_height); 
	imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

	// Display resized image  
	imagejpeg($image_resized,$th_file_name,100); 
}

function open_image($file) { 
	# JPEG: 
	$im = @imagecreatefromjpeg($file); 
	if ($im !== false) { return $im; } 

	# GIF: 
	$im = @imagecreatefromgif($file); 
	if ($im !== false) { return $im; } 

	# PNG: 
	$im = @imagecreatefrompng($file); 
	if ($im !== false) { return $im; } 

	# GD File: 
	$im = @imagecreatefromgd($file); 
	if ($im !== false) { return $im; } 

	# GD2 File: 
	$im = @imagecreatefromgd2($file); 
	if ($im !== false) { return $im; } 

	# WBMP: 
	$im = @imagecreatefromwbmp($file); 
	if ($im !== false) { return $im; } 

	# XBM: 
	$im = @imagecreatefromxbm($file); 
	if ($im !== false) { return $im; } 

	# XPM: 
	$im = @imagecreatefromxpm($file); 
	if ($im !== false) { return $im; } 

	# Try and load from string: 
	$im = @imagecreatefromstring(file_get_contents($file)); 
	if ($im !== false) { return $im; } 

	return false; 
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/125678-image-upload-and-resizing-class/
Share on other sites

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.