All pastes #981468 Raw Edit

Miscellany

public php v1 · immutable
·published 2008-04-11 20:22 UTC
rendered paste body
	/*	 * Resizes an image and return the image's data	 * @param $imgfile Path to the current image file	 * @param $name Filename of the current image file (used for format detection)	 * @param $maxwidth Maximum width the image can have after resize	 */	public static function ImageResize($imgfile, $name, $maxwidth)	{        $format = strtoupper(eregi_replace(".*\.(.*)$","\\1", $name));        $image = null;        switch($format)        {                case "JPG":                case "JPEG":                        $image = imagecreatefromjpeg($imgfile);                        break;                case "PNG":                        $image = imagecreatefrompng($imgfile);                        break;                case "GIF":                        $image = imagecreatefromgif($imgfile);                        break;                case "WBMP":                        $image = imagecreatefromwbmp($imgfile);                        break;        }		if ($image != null)		{			 $x = imagesx($image);             $y = imagesy($image);             if ($x < $maxwidth)             {                     // No resize needed, copy original and shut up                     return file_get_contents($imgfile);             }             else             {                     // Calculate proportions based on $x2 = 1024;                     $x2 = $maxwidth;                     $y2 = $y * ($x2 / $x);                     // Create image and do actual resize                     $image2 = imagecreatetruecolor($x2, $y2);                     imagecopyresized($image2, $image, 0, 0, 0, 0, $x2, $y2, $x, $y);                     // Then save file in the same format we opened it with					 ob_start();                     switch($format)                     {                             case "JPG":                             case "JPEG":                                     imagejpeg($image2);                                     break;                             case "PNG":                                     imagepng($image2);                                     break;                             case "GIF":                                     imagegif($image2);                                     break;                             case "WBMP":                                     imagewbmp($image2);                                     break;                     }					$data = ob_get_contents();					ob_end_clean();					return $data;             }		} 		else die("Image open failed");	}