All pastes #564600 Raw Edit

Someone

public text v1 · immutable
·published 2007-06-13 17:36 UTC
rendered paste body
	$max_w = '400';
	$max_h = '400';
	
	$sTempFileName = $directory.'/'.$filename.'.'.$fileext;
	
	$dest_thumbnail = $directory.'/'.$filename.'_thumbnail.'.$fileext;
	
	$oTempFile = fopen($sTempFileName, "r");
	$sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
	
	$oSourceImage = imagecreatefromstring($sBinaryPhoto);
	$nWidth = imagesx($oSourceImage); // get original source image width
	$nHeight = imagesy($oSourceImage); // and height

	$height = $nHeight;
	$width = $nWidth;
	
	if($height < $width) $ratio=$height/$width;
	else $ratio=$width/$height;
	
        // resize image?
        if ($width > $max_w || $height > $max_h) {

				if($height > $max_h)
				{
              	$rs_h = $max_h;
                $rs_w = $rs_h * $ratio;
                }
       			if($width > $max_w)
				{
              	$rs_w = $max_w;
                $rs_h = $rs_w * $ratio;
                }
           
        }
        // image requires no resizing
        else {
            $rs_w = $width;
            $rs_h = $height;
        }
        
	$nDestinationWidth = $rs_w;
	$nDestinationHeight = $rs_h;
	
	
	
	$oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
	imagecopyresampled( $oDestinationImage, $oSourceImage, 0, 0, 0, 0, $nDestinationWidth, $nDestinationHeight, $nWidth, $nHeight); // resize the image


switch($fileext)
{
	case 'jpg':
		imageJPEG($oDestinationImage,$dest_thumbnail,100);
	break;
	case 'jpeg':
		imageJPEG($oDestinationImage,$dest_thumbnail,100);
	break;
	case 'gif':
		imageGIF($oDestinationImage,$dest_thumbnail,100);
	break;
	case 'png':
		imagePNG($oDestinationImage,$dest_thumbnail,100);
	break;
}