龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

裁切it糗事百科 图片 还原

时间:2014-07-22 14:49来源: 作者: 点击:
分享到:
你懂的 lol
你懂的  lol  
<?php
/**
 * 裁切图片 高度冲裁
 * @param unknown $src_file   源文件
 * @param unknown $dst_file   新文件名
 * @param number $height      裁切掉的高度
 */
function my_image_resize($src_file, $dst_file, $height = 30) {
	if (! file_exists ( $src_file )) {
		echo $src_file . " is not exists !";
		exit ();
	}
	$type = exif_imagetype ( $src_file );
	$support_type = array (
			IMAGETYPE_JPEG,
			IMAGETYPE_PNG,
			IMAGETYPE_GIF 
	);
	if (! in_array ( $type, $support_type, true )) {
		echo "this type of image does not support! only support jpg , gif or png";
		exit ();
	}
	switch ($type) {
		case IMAGETYPE_JPEG :
			$src_img = imagecreatefromjpeg ( $src_file );
			break;
		case IMAGETYPE_PNG :
			$src_img = imagecreatefrompng ( $src_file );
			break;
		case IMAGETYPE_GIF :
			$src_img = imagecreatefromgif ( $src_file );
			break;
		default :
			echo "Load image error!";
			exit ();
	}
	$w = imagesx ( $src_img );
	$h = imagesy ( $src_img );
	$ratio_w = $w;
	$ratio_h = $h - $height;
	
	$inter_img = imagecreatetruecolor ( $w, $h );
	
	imagecopyresampled ( $inter_img, $src_img, 0, 0, 0, 0, $w, $h, $w, $h );
	$new_img = imagecreatetruecolor ( $ratio_w, $ratio_h );
	imagecopy ( $new_img, $inter_img, 0, 0, 0, 0, $ratio_w, $ratio_h );
	switch ($type) {
		case IMAGETYPE_JPEG :
			imagejpeg ( $new_img, $dst_file, 100 );
			break;
		case IMAGETYPE_PNG :
			imagepng ( $new_img, $dst_file, 100 );
			break;
		case IMAGETYPE_GIF :
			imagegif ( $new_img, $dst_file, 100 );
			break;
		default :
			break;
	}
}
精彩图集

赞助商链接