PHP Resim Boyutlandırma

Sunucu sağlığı veya tasarımsal gerekçelerle yüklenen resmin yeniden boyutlandırılmasına ihtiyaç duyabiliyoruz.

Bu gibi ihtiyaçlarda kullandığım ufak tefek eklemelerle standart PHP resim boyutlandırmasına ilave bir kaç değişikle fonksiyon haline getirdiğim
kod aşağıdaki gibidir.

function imgResize($filename, $source_dir, $new_source = '', $dest_width = '', $dest_height = '',$auto = true){	
	$full_path = $source_dir . $filename;
	
	$basefilename = preg_replace("/(.*)\.([^.]+)$/","\\1", $filename);
	$ext = preg_replace("/.*\.([^.]+)$/","\\1", $filename);
	$ext = strtolower($ext);
	
	switch ($ext) {
		case 'png':
			$image = imagecreatefrompng($full_path);
		break;
		
		case 'jpg':
			$image = imagecreatefromjpeg($full_path);
		break;
		
		case 'jpeg':
			$image = imagecreatefromjpeg($full_path);
		break;
		
		default:
			echo 'GD işlem için geçerli olmayan dosya formatı : '.$ext;
			return false;
		break;
	}

	$image_width = imagesx($image);
	$image_height = imagesy($image);
	
	if ( $auto ){
		if ( $image_width >= $image_height ){
			if ( $dest_width ){
				if ( $dest_width > $image_width )
					$dest_width = $image_width;    					
				$dest_height = (int) ( $image_height / ( (real)($image_width / $dest_width) ) );		
			} else {
				if ( $dest_height > $image_height )
					$dest_height = $image_height;    					
				$dest_width = (int) ( $image_width / ( (real)($image_height / $dest_height) ) );
			}
		} else {    			    			
			if ( $dest_height ){
				if ( $dest_height > $image_height )
					$dest_height = $image_height;    					
				$dest_width = (int) ( $image_width / ( (real)($image_height / $dest_height) ) );		
			} else {
				if ( $dest_width > $image_width )
					$dest_width = $image_width;    					
				$dest_height = (int) ( $image_height / ( (real)($image_width / $dest_width) ) );
			}    			
		}
	}
	
	
	
	
	if ('gd2' == checkGd()) { 
		$img_id = imagecreatetruecolor($dest_width, $dest_height);
		
		imagealphablending($img_id, false);
		imagesavealpha($img_id,true);
		$transparent = imagecolorallocatealpha($img_id, 255, 255, 255, 127);
		imagefilledrectangle($img_id, 0, 0, $image_width, $image_height, $transparent);
		
		imagecopyresampled($img_id, $image, 0, 0, 0, 0, $dest_width, $dest_height, $image_width, $image_height);
		
	} else {
		$img_id = imagecreate($dest_width, $dest_height);
		
		imagealphablending($img_id, false);
		imagesavealpha($img_id,true);
		$transparent = imagecolorallocatealpha($img_id, 255, 255, 255, 127);
		imagefilledrectangle($img_id, 0, 0, $image_width, $image_height, $transparent);
		
		imagecopyresized($img_id, $image, 0, 0, 0, 0, $dest_width, $dest_height, $image_width, $image_height);
	}
	
	$new_source = ( $new_source ) ? $new_source : $source_dir . $filename; 
	
	
	switch ($ext) {
		case 'png':
			imagepng($img_id, $new_source,100);
			break;
		
		case 'jpg':
			imagejpeg($img_id, $new_source,100);
			break;
		
		case 'jpeg':
			imagejpeg($img_id, $new_source,100);
			break;
	}
	
	imagedestroy($img_id);
	
	return true;
}

function checkGd(){
	$gd_content = get_extension_funcs('gd');
	if ( !$gd_content ) {
		return false; 
	} else {
		ob_start();
		phpinfo(8);
		$buffer = ob_get_contents();
		ob_end_clean(); 
		if (strpos($buffer, '2.0')) {
			return 'gd2';
		} else {
			return 'gd';
		}
	}
}

imgResize fonksiyonunun özelliklerini anlatmak gerekirse

imgResize(
$filename : ‘Mevcut resim adı’
$source_dir : ‘Mevcut resmin bulunduğu klasör’
$new_source : ‘İsteniyorsa yeni klasör ya da yeni resmin adı’
$dest_width : ‘Değiştirilecek genişlik’
$dest_height : ‘Değiştirilecek yükseklik’
$auto = true : ‘Genişlik veya yükseklik değerleri baz alınarak resmin boyutlandırılması’
)

Aşağıda kullanımıyla alakalı bir kaç örnek vermek istiyorum.

Upload klasörünün altındaki Penguenler resmini upload/ klasörü altında kucukPenguenler resmi oluşturuluyor.

 imgResize( 'Penguenler.jpg', 'upload/', 'kucukPenguenler.jpg', 100, 75, auto ); 

Upload klasörünün altındaki Penguenler resmini upload/ klasörü altında kucukPenguenler resmi oluşturuluyor.

 imgResize( 'upload/Penguenler.jpg', '', 'kucukPenguenler.jpg', 100, 75, auto ); 

Upload klasörünün altındaki Penguenler resmini kucuk/ klasörü altında kucukPenguenler resmi oluşturuluyor.

 imgResize( 'upload/Penguenler.jpg', '', 'kucuk/Penguenler.jpg', 100, 75, false ); 

Uygulama Örnek Sayfası

PHP ile Resim Boyutlandırma Uygulamasını İndir

Paylaş: