Re: upload and resize image script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



P80 wrote:
hey all
I'm looking for a simple script that would upload an image and resize it to thumbnail using a form to upload the picture. anyone has a good script for that?


thanx in advance

Pat

Hi Pat,

I wrote this function to resize pictures to the max width/height, preserving proportions:

function resizeImage($src_file, $dst_file, $max_width, $max_height)
{
	// Formats we will handle.
	$formats = array('1' => 'gif', '2' => 'jpeg', '3' => 'png');

	// Get info on the image.
	if (!$sizes = getimagesize($src_file))
		return false;

// A known and supported format?
if (!isset($formats[$sizes[2]]) || !function_exists('imagecreatefrom' . $formats[$sizes[2]]))
return false;


	// Create image from file.
	$imagecreatefrom = 'imagecreatefrom' . $formats[$sizes[2]];
	if (!$src_img = $imagecreatefrom($src_file))
		return false;

	// Calculate the new size.
	$max = array('width' => $max_width, 'height' => $max_height);
	$dst['width'] = $src['width'] = $sizes[0];
	$dst['height'] = $src['height'] = $sizes[1];

	foreach (array('width' => 'height', 'height' => 'width') as $k => $v)
		if (!empty($max[$k]) && $dst[$k] > $max[$k])
		{
			$dst[$k] = $max[$k];
			$dst[$v] = floor($src[$v] * $max[$k] / $src[$k]);
		}

	// Create true color image.
	$dst_img = imagecreatetruecolor($dst['width'], $dst['height']);

// Do the resize.
if(!imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst['width'], $dst['height'], $src['width'], $src['height']))
return false;


	// Save image to file.
	imagejpeg($dst_img, $dst_file);

	// Free the memory.
	imagedestroy($src_img);
	imagedestroy($dst_img);

	return true;
}

It may not be exactly what you need, but I hope you find it helpful.

Regards,
Cristian

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux