Php Image Resize

image resize:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
function my_image_resize( $src_file , $dst_file , $dst_width = 32 , $dst_height = 32 ) {
if ( $dst_width < 1 || $dst_height < 1 ) {
echo " params width or height error ! " ;
exit ();
}
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 ();
}
$src_w = imagesx( $src_img );
$src_h = imagesy( $src_img );
$ratio_w = 1.0 * $dst_width / $src_w ;
$ratio_h = 1.0 * $dst_height / $src_h ;
if ( $src_w <= $dst_width && $src_h <= $dst_height ) {
$x = ( $dst_width - $src_w ) / 2 ;
$y = ( $dst_height - $src_h ) / 2 ;
$new_img = imagecreatetruecolor( $dst_width , $dst_height );
imagecopy(
$new_img , $src_img , $x , $y , 0 , 0 , $dst_width , $dst_height );
switch ( $type ) {
case IMAGETYPE_JPEG :
imagejpeg(
$new_img , $dst_file , 100 );
break ;
case IMAGETYPE_PNG :
imagepng(
$new_img , $dst_file );
break ;
case IMAGETYPE_GIF :
imagegif(
$new_img , $dst_file );
break ;
default :
break ;
}
}
else {
$dstwh = $dst_width / $dst_height ;
$srcwh = $src_w / $src_h ;
if ( $ratio_w <= $ratio_h ) {
$zoom_w = $dst_width ;
$zoom_h = $zoom_w * ( $src_h / $src_w );
}
else {
$zoom_h = $dst_height ;
$zoom_w = $zoom_h * ( $src_w / $src_h );
}

$zoom_img = imagecreatetruecolor( $zoom_w , $zoom_h );
imagecopyresampled(
$zoom_img , $src_img , 0 , 0 , 0 , 0 , $zoom_w , $zoom_h , $src_w , $src_h );
$new_img = imagecreatetruecolor( $dst_width , $dst_height );
$x = ( $dst_width - $zoom_w ) / 2 ;
$y = ( $dst_height - $zoom_h ) / 2 + 1 ;
imagecopy(
$new_img , $zoom_img , $x , $y , 0 , 0 , $dst_width , $dst_height );
switch ( $type ) {
case IMAGETYPE_JPEG :
imagejpeg(
$new_img , $dst_file , 100 );
break ;
case IMAGETYPE_PNG :
imagepng(
$new_img , $dst_file );
break ;
case IMAGETYPE_GIF :
imagegif(
$new_img , $dst_file );
break ;
default :
break ;
}
}
}

 

转载于:https://www.cnblogs.com/Jekhn/archive/2011/01/16/1936608.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值