<?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;
}
}