Jump to content

thumbnail resizing


wendu

Recommended Posts

hey

 

I'm trying to make a thumbnail of an image and I already have the code working to create the thumbnail

 

however I need a way to calculate values so that respecting the aspect ratio I can make the thumbnail either 150 pixels wide or high but neither can go over 150, and the aspect ratio needs to stay the same

 

thanks!! <33

Link to comment
Share on other sites

$maxWidth = 150;
$maxHeight = 150;

list($width, $height) = getimagesize('path/to/image');

$ratio = 1;
if ($width > $maxWidth) {
    $ratio = $maxWidth / $width;
} else if ($height > $maxHeight) {
    $ratio = $maxHeight / $height;
}

$newWidth = $width * $ratio;
$newHeight = $height * $ratio;

 

yes you've given me this piece of code however it does not work if both the width and the hight are the same

Link to comment
Share on other sites

An Image class that I wrote quickly has the ability to do exactly what you want (and a lot more).

 

here's the class:

 

class Image
{
var $image, $ext, $file;
public function __construct($im=NULL)
{
	define('CENTER', 0, true);
	define('CENTER_LEFT', 1, true);
	define('CENTER_RIGHT', 2, true);
	define('TOP_CENTER', 3, true);
	define('TOP_LEFT', 4, true);
	define('TOP_RIGHT', 5, true);
	define('BOTTOM_CENTER', 6, true);
	define('BOTTOM_LEFT', 7, true);
	define('BOTTOM_RIGHT', 8, true);

	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=0)
{
	$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);
}
}

 

You're gonna wanna do something like this:

 

//Include my class
$image = new Image('path/to/image');
$image->resize(null, null, true, null, 150);
$image->save('path/to/new/location.ext');

Link to comment
Share on other sites

Alex the class you specified is wrong everytime you create a new instance of the class you'd get an error.

 

new Image('bla');
new Image('bla');// cannot redefine CENTER already defined in ..

 

Plus if you are writing for PHP4 (altough since 5.3.0 most hosts are now switching to PHP5) then keep it PHP4 or if you want to make use of all the OOP features PHP5 gives you then change your class for PHP5.

 

Thus either:

 

define('CENTER', 0, true);
..

class Image {
    var $image, $ext, $file;
    function Image($im = null) {
    ..

 

or:

 

class Image {
    const CENTER = 0;
    ..
    
    public $image, $ext, $file;//altough you should consider private instead of public
    public function __construct($im = null) {
    ..

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.