Jump to content

gd2resize script


zohab

Recommended Posts

Here's a useful class I wrote:

 

class Image
{
private $image, $ext, $file;

const CENTER = 0, CENTER_LEFT = 1, CENTER_RIGHT = 2, TOP_CENTER = 3, TOP_LEFT = 4, TOP_RIGHT = 5, BOTTOM_CENTER = 6, BOTTOM_LEFT = 7, BOTTOM_RIGHT = 8;

public function __construct($im=NULL)
{	
	if(empty($im)) return true;
	$info = @getimagesize($im);
	$backtrace = debug_backtrace();
	if(!$info)
		return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$ex = explode('/', $info['mime']);
	$this->ext = $ex[1];
	$func = 'imagecreatefrom' . $this->ext;
	$this->image = @$func($im);
	$this->file = $im;
	return true;
}
public function setImage($im)
{
	$info = @getimagesize($this->file);
	$backtrace = debug_backtrace();
	if(!$info)
		return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$ex = explode('/', $info['mime']);
	$this->ext = $ex[1];
	$func = 'imagecreatefrom' . $this->ext;
	$this->image = @$func($im);
	$this->file = $im;
	return true;
}
public function resize($width, $height, $porportional=FALSE, $percent=NULL, $max=NULL)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->resize, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$info = Array(imagesx($this->image), imagesy($this->image));
	if($porportional)
	{
		if(empty($max) && empty($percent)) return false;
		if(empty($max))
		{
			$new_width = $info[0] * ($percent/100);
			$new_height = $info[1] * ($percent/100);
		}
		else
		{
			if($info[0] < $max && $info[1] < $max) return false;
			$new_width = ($info[0] > $info[1]) ? $max : ($info[0]/$info[1]) * $max;
			$new_height = ($info[0] > $info[1]) ? ($info[1]/$info[0]) * $max : $max;
		}
	}
	else
	{
		$new_width = $width;
		$new_height = $height;
	}
	$new_image = imagecreatetruecolor($new_width, $new_height);
	imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);
	$this->image = $new_image;
	return true;
}
public function crop($width, $height, $x, $y, $position=NULL)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->crop, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$info = Array(imagesx($this->image), imagesy($this->image));
	if($width > $info[0] || $height > $info[1]) return false;
	if(!empty($position) && $position >= 0 && $position <= 
	{
		switch($position)
		{
			case CENTER:
				$x = ($info[0] - $width)/2;
				$y = ($info[1] - $height)/2;
			break;
			case CENTER_LEFT:
				$x = 0;
				$y = ($info[1] - $height)/2;
			break;
			case CENTER_RIGHT:
				$x = ($info[0] - $height);
				$y = ($info[1] - $height)/2;
			break;
			case TOP_CENTER:
				$x = ($info[0] - $width)/2;
				$y = 0;
			break;
			case TOP_LEFT:
				$x = 0;
				$y = 0;
			break;
			case TOP_RIGHT:
				$x = ($info[0] - $width);
				$y = 0;
			break;
			case BOTTOM_CENTER:
				$x = ($info[0] - $width)/2;
				$y = ($info[1] - $height);
			break;
			case BOTTOM_LEFT:
				$x = 0;
				$y = ($info[1] - $height);
			break;
			case BOTTOM_RIGHT:
				$x = ($info[0] - $width);
				$y = ($info[1] - $height);
			break;
			default:
				return false;
			break;
		}
	}
	$new_image = imagecreatetruecolor($width, $height);
	imagecopyresampled($new_image, $this->image, 0, 0, $x, $y, $width, $height, $width, $height);
	$this->image = $new_image;
	return true;
}
public function save($loc, $compression=100)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->save, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$func = 'image' . $this->ext;
	return $func($this->image, $loc, $compression);
}
public function output()
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->output, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	header('Content-type: image/' . $this->ext);
	$func = 'image' . $this->ext;
	$func($this->image);
}
}

 

My class can do a lot of different things, so you'd need to be specific for me to help you on exactly what you need to image to be resized to (proportional, max width, max height, whatever..). Unless you can look at the resize function and figure it out yourself.

 

Here's an example of something you can do:

 

//include my class
$image = new Image($_FILES['file']['tmp_name']);// $_FILES['file']['tmp_name'] is the file being uploaded
$image->resize(width, height);
$image->save('path/to/save/location');

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.