You are here: Re: upload and resize image script « PHP « IT news, forums, messages
Re: upload and resize image script

Posted by Cristian Lavaque on 03/04/05 22:06

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация