Jump to content

Recommended Posts

Hey! Well I have a little dilema, hoping I could find some guidance.

 

I have a CMS and on the actual website I am building I have a nice little slideshow script that runs through all the images in a certain directory. The dimensions of the slideshow are 900x250. (Obviously not all images come in this size straight from the camera so I need a little resize crop and save script to do this for the user)

 

I would like the 'admin' to be able to upload an image (or more than one at the same time, if it's not much more hard work) and then PHP simply takes over from there. At which point the images would be resized to 900px in width (keeping the aspect ratio) and then cropped to take the middle 250px out. then this image is saved in "images/slideshow" as a 900x250 pixel image? The format it is saved in isn't that much of a problem.

 

Thanks a lot in advance! Any guidance is appreciated!

 

For example, this image:

http://thewalkingtree.files.wordpress.com/2009/07/children-1.jpg

 

Would become:

http://img503.imageshack.us/img503/4315/kidsl.png

Link to comment
https://forums.phpfreaks.com/topic/179727-php-images-resize-crop-save/
Share on other sites

There are tons of image resize/crop scripts out there so I won't post any code for that.

 

The best time to resize & crop any images are as soon as they're uploaded onto the server by the user. Once stored the site can then just link to them instead of resizing them each time they're displayed as this will increase server load.

 

As for renaming the files as they're uploaded there are various ways you can achieve this...

 

1. Ask the user for a new filename - disadvantage is that there's a large possibility the user can choose a filename that already exists.

 

2. Have the script generate a unique filename - you could link the uploaded images to a database which stores information about the image (dimensions, file type, etc.) and as they're viewed use a unique identifier from the database to generate the filename.

 

Personally I'd opt for the second option.

Only Actionscript can resize images.

 

Oops sorry i meant rotate lol

Do you mean like a slideshow? Because you can rotate an image resource by using imagerotate(). Either way I don't think creating the slideshow is the problem here.

 

Anyway, I created an Image Manipulation Class that can do what you want easily. I never really 'finished' it though, I just add stuff here and there.

 

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);
	if(!$info)
		$this->throw_error("'$im' is not a valid image resource");
	$this->_ext = end(explode('/', $info['mime']));
	$func = 'imagecreatefrom' . $this->_ext;
	$this->_image = @$func($im);
	return $this->_file = $im;
}

public function set($im)
{
	$info = @getimagesize($this->_file);
	if(!$info)
		$this->throw_error("'$im' is not a valid image resource");
	$this->_ext = end(explode('/', $info['mime']));
	$func = 'imagecreatefrom' . $this->_ext;
	$this->_image = @$func($im);
	return $this->_file = $im;
}

public function resize($width, $height, $porportional=FALSE, $percent=NULL, $max=NULL)
{
	if(empty($this->_image)) 
		$this->throw_error('Invalid call to Image()->resize, no image is set');
	$info = Array(imagesx($this->_image), imagesy($this->_image));
	if($porportional)
	{
		if(!empty($percent))
		{
			$new_width = $info[0] * ($percent/100);
			$new_height = $info[1] * ($percent/100);
		}
		else if(!empty($max))
		{
			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
		{
			if(!empty($width) && !empty($height) || empty($width) && empty($height))
				return false;
			$new_width = (!empty($width)) ? $width : $height/$info[1]*$info[0];
			$new_height = (!empty($height)) ? $height : $width/$info[0]*$info[1];
		}
	}
	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]);
	return $this->_image = $new_image;
}

public function crop($width, $height, $x, $y, $position=NULL)
{
	if(empty($this->_image))
		$this->throw_error('Invalid call to Image()->crop, no image is set');
	$info = Array(imagesx($this->_image), imagesy($this->_image));
	if($width > $info[0] || $height > $info[1])
		return false;
	switch($position)
	{
		case Image::CENTER:
			$x = ($info[0] - $width)/2;
			$y = ($info[1] - $height)/2;
		break;
		case Image::CENTER_LEFT:
			$x = 0;
			$y = ($info[1] - $height)/2;
		break;
		case Image::CENTER_RIGHT:
			$x = ($info[0] - $height);
			$y = ($info[1] - $height)/2;
		break;
		case Image::TOP_CENTER:
			$x = ($info[0] - $width)/2;
			$y = 0;
		break;
		case Image::TOP_LEFT:
			$x = 0;
			$y = 0;
		break;
		case Image::TOP_RIGHT:
			$x = ($info[0] - $width);
			$y = 0;
		break;
		case Image::BOTTOM_CENTER:
			$x = ($info[0] - $width)/2;
			$y = ($info[1] - $height);
		break;
		case Image::BOTTOM_LEFT:
			$x = 0;
			$y = ($info[1] - $height);
		break;
		case Image::BOTTOM_RIGHT:
			$x = ($info[0] - $width);
			$y = ($info[1] - $height);
		break;
		default:
		break;
	}
	$new_image = imagecreatetruecolor($width, $height);
	imagecopyresampled($new_image, $this->_image, 0, 0, $x, $y, $width, $height, $width, $height);
	return $this->_image = $new_image;
}

public function watermark($wm, $position=NULL, $alpha=50)
{
	if(empty($this->_image))
		$this->throw_error('Invalid call to Image()->crop, no image is set');
	$info = @getimagesize($wm);
	if(!$info)
		$this->throw_error("'$wm' (watermark) is not a valid image resource");
	elseif(end(explode('/', $info['mime'])) != $this->_ext)
		$this->throw_error("The watermark must have the same mime type as the base image");
	$func = 'imagecreatefrom' . end(explode('/', $info['mime']));
	$wmr = $func($wm);
	$info = Array(imagesx($this->_image), imagesy($this->_image));
	$wm_info = Array(imagesx($wmr), imagesy($wmr));
	switch($position)
	{
		case Image::CENTER:
			$x = ($info[0] - $wm_info[0]) / 2;
			$y = ($info[1] - $wm_info[1]) / 2;
		break;
		case Image::CENTER_LEFT:
			$x = 0;
			$y = ($info[1] - $wm_info[1]) / 2;
		break;
		case Image::CENTER_RIGHT:
			$x = $info[0] - $wm_info[1];
			$y = ($info[1] - $wm_info[1]) / 2;
		break;
		case Image::TOP_CENTER:
			$x = ($info[0] - $wm_info[0]) / 2;
			$y = 0;
		break;
		case Image::TOP_LEFT:
			$x = 0;
			$y = 0;
		break;
		case Image::TOP_RIGHT:
			$x = ($info[0] - $wm_info[0]);
			$y = 0;
		break;
		case Image::BOTTOM_CENTER:
			$x = ($info[0] - $wm_info[0]) / 2;
			$y = ($info[1] - $wm_info[1]);
		break;
		case Image::BOTTOM_LEFT:
			$x = 0;
			$y = ($info[1] - $wm_info[1]);
		break;
		case Image::BOTTOM_RIGHT:
			$x = ($info[0] - $wm_info[0]);
			$y = ($info[1] - $wm_info[1]);
		break;
		default:
			$x = ($info[0] - $wm_info[0]);
			$y = ($info[1] - $wm_info[1]);
		break;
	}
	return imagecopymerge($this->_image, $wmr, $x, $y, 0, 0, $wm_info[0], $wm_info[1], $alpha);
}

public function save($loc, $compression=100)
{
	if(empty($this->_image))
		$this->throw_error('Invalid call to Image()->save, no image is set');
	$func = 'image' . $this->_ext;
	return $func($this->_image, $loc, $compression);
}

public function output()
{
	if(empty($this->_image)) 
		$this->throw_error('Invalid call to Image()->output, no image is set');
	header('Content-type: image/' . $this->_ext);
	$func = 'image' . $this->_ext;
	$func($this->_image);
}

private function throw_error($err)
{
	$backtrace = debug_backtrace();
		die('<b>Fatal error:</b> ' . $err . ' <b>on line ' . $backtrace[1]['line'] . ' [' . $backtrace[1]['file'] . ']</b>');
}
}

 

To do what you want here's an example:

 

<?php
require_once 'image.class.php';

$image = new Image('http://thewalkingtree.files.wordpress.com/2009/07/children-1.jpg');
$image->resize(900, null, true);
$image->crop(900, 250, null, null, Image::CENTER);
$image->output();
//$image->save('path/to/new/location');
?>

 

If you test this you'll notice it outputs exactly what you posted in your example.

AlexWD Thank you so much!!!

 

However, your script doesn't work completely because a fatal error is returned.

 

Fatal error: 'http://thewalkingtree.files.wordpress.com/2009/07/children-1.jpg' is not a valid image resource on line 11 [/home/www/byrom.freehostia.com/smclc/image.php]

 

line 11 is:

 

$image = new Image('http://thewalkingtree.files.wordpress.com/2009/07/children-1.jpg');

I meant like a JAvascript and a PHP combination of let's say

 

Holding down shift key, then using mouse to rotate image to the right or left as far as u want, like circular, then save it with php, using javascript. but I don't think javascript supports anything like that, like Actionscript does

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.