Re: upload and resize image script

From: Date: Fri, 04 Mar 2005 20:06:01 +0000
Subject: Re: upload and resize image script
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-210033@lists.php.net to get a copy of this message
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

« previous php.general (#210033) next »