881128824e981cd48a3fcfea136e8897

I'm building a personal library of file/image handling functions partially to organize my bits and partially for php-object practice. This looks so crazy to me, I think there must be a nicer way to do it.

<?php
private function sanitizeImageSizeInput(&$dimN, $dimO){
		if(strpos($dimN, '%') !== FALSE) {
			$perc = str_replace('%', '', $dimN);
			$dimN = $dimO * ($perc/100);
		} else if (strpos($dimN, 'px') !== FALSE) {
			$dimN = str_replace('px', '', $dimN);
		} else if (!is_numeric($dimN))
			$dimN = 0;
	}
	
	/**
	 * resize the image that was just uploaded, or one specified. $nw/$nh can be specified numerically (which will be used a pixel value), a string with % or px, or as NULL to cause that value to be set proportionately to the other.
	 * @param numeric or string $nw
	 * @param numeric or string $nh
	 * @param string $source
	 * @param string $copyTo
	 * @return boolean
	 */
	public function resizeImage ($nw,$nh,$source = NULL, $copyTo = NULL) { 
		// GENERATES SLIGHT BLURINESS IN THE IMAGE. Tried imageconvolution with a sharp filter, but didnt seem to help much.

		if($source == NULL)
			$source = $this->uploadedfile;
			
		$iData = getimagesize($source);
		$mime = $iData['mime'];
		list($w,$h) = $iData;

		if($mime != 'image/jpeg' && $mime != 'image/png' && $mime != 'image/bmp' && $mime != 'image/gif') {
			error('Allowed filetypes: jpeg, png, bmp or gif');
			return false;
		}
		
		if($nh == NULL) {
			// only manip W
			sanitizeImageSizeInput($nw,$w);
			$nh = ($nw/$w) * $h;
			
		} else if ($nw == NULL) {
			// only manip H
			sanitizeImageSizeInput($nh,$h);
			$nw = ($nh/$h) * $w;
			
		} else { // both values are specified
			//TODO beware, this will kill the proportions. should we build in a "sanity check"?
			sanitizeImageSizeInput($nw,$w);
			sanitizeImageSizeInput($nh,$h);
		}
		
		$dimg = imagecreatetruecolor($nw, $nh);

		if($copyTo == NULL)
			$product = $source;
		else
			$product = $copyTo;
		
			
		switch ($mime){
			case 'image/jpeg' : 
				$old = imagecreatefromjpeg($source);
				$commit = function () {
					imagejpeg($dimg,$product,100);
					};
				break;
			case 'image/png' : 
				$old = imagecreatefrompng($source);
				$commit = function () {
					imagepng($dimg,$product);
					};
				break;
			case 'image/gif' : 
				$old = imagecreatefromgif($source);
				$commit = function () {
					imagegif($dimg,$product);
					};
				break;
			case 'image/bmp' : 
				$old = imagecreatefrombmp($source);
				$commit = function () {
					// we will change bmp to png
					imagepng($dimg,$product);
					};
				break;
		}
		
		imagecopyresampled($dimg,$old,0,0,0,0,$nw,$nh,$w,$h);
		imagedestroy($old);
		
		if($commit){
			imagedestroy($dimg);
			return TRUE; 
		} else {
			imagedestroy($dimg);
			return FALSE;
		}
	}

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

magna

February 12, 2011, February 12, 2011 16:01, permalink

No rating. Login to rate!
<?php

private function sanitizeImageSizeInput(&$dimN, $dimO) {
	
	if (strpos($dimN, '%') !== false) {
		$perc = str_replace('%', '', $dimN);
		$dimN = $dimO * ($perc / 100);
	} else if (strpos($dimN, 'px') !== false) {
		$dimN = str_replace('px', '', $dimN);
	} else if (!is_numeric($dimN)) {
		$dimN = 0;
	}

} 

/**
 * Resize the image that was just uploaded, or one specified.
 * $nw/$nh can be specified numerically (which will be used a pixel value),
 * a string with % or px, or as NULL to cause that value to be set
 * proportionately to the other.
 * 
 * @param numeric $ or string $nw
 * @param numeric $ or string $nh
 * @param string $source 
 * @param string $copyTo 
 * @return boolean 
 */
public function resizeImage($nw, $nh, $source=null, $copyTo=null) {

	// GENERATES SLIGHT BLURINESS IN THE IMAGE.
	// Tried imageconvolution with a sharp filter, but didnt seem to help much.
	if ($source == null)
		$source = $this->uploadedfile;

	$iData = getimagesize($source);
	$mime = $iData['mime'];
	list($w, $h, $t) = $iData;
	
 	// what about image/pjpeg or image/x-windows-bmp?
 	// wouldn't it be nicer to use $t instead?
	$valid_mimes = array('image/jpeg', 'image/png', 'image/bmp', 'image/gif');
	if (!in_array($mime, $valid_mimes)) {
		error('Allowed filetypes: jpeg, png, bmp or gif');
		return false;
	} 

	if ($nw == null && $nh == null) {
		// don't forget this case!
		error('neither with nor height were given');
		return false;
	} else if ($nw != null && $nh == null) {
		// only manip W
		sanitizeImageSizeInput($nw, $w);
		$nh = ($nw / $w) * $h;
	} else if ($nw == null && $nh != null) {
		// only manip H
		sanitizeImageSizeInput($nh, $h);
		$nw = ($nh / $h) * $w;
	} else if ($nw != null && $nh != null) {
		// both values are specified
		// TODO beware, this will kill the proportions.
		// should we build in a "sanity check"?
		sanitizeImageSizeInput($nw, $w);
		sanitizeImageSizeInput($nh, $h);
	} 

	$dimg = imagecreatetruecolor($nw, $nh);
	$product = null == $copyTo ? $source : $copyTo;

	switch ($mime) {
		case 'image/jpeg':
			$old = imagecreatefromjpeg($source);
			$commit = function () {
				imagejpeg($dimg, $product, 100);
			};
			break;
		case 'image/png':
			$old = imagecreatefrompng($source);
			$commit = function () {
				imagepng($dimg, $product);
			};
			break;
		case 'image/gif':
			$old = imagecreatefromgif($source);
			$commit = function () {
				imagegif($dimg, $product);
			};
			break;
		case 'image/bmp':
			$old = imagecreatefrombmp($source);
			$commit = function () {
				// we will change bmp to png
				imagepng($dimg, $product);
			};
			break;
		default:
			error('Allowed filetypes: jpeg, png, bmp or gif');
			return false;
	}
	// what happens to $commit?

	imagecopyresampled($dimg, $old, 0, 0, 0, 0, $nw, $nh, $w, $h);
	imagedestroy($old);

	if ($commit) {
		imagedestroy($dimg);
		return true;
	} else {
		imagedestroy($dimg);
		return false;
	}
	
	// why not just
	// imagedestroy($dimg);
	// return $commit ? true : false;

}

?>

Your refactoring





Format Copy from initial code

or Cancel