PHP:图片缩放函数

图片缩放函数


**

<?php
$img="a.jpg";

function thumb($img,$dstx,$dsty,$pre){
    // 得到图片信息
    $arr=getimagesize($img);
    echo '<pre>';
    print_r($arr);
    echo '</pre>';

    //原图资源
    $srcimg=imagecreatefromjpeg($img);
    
    //目标资源
    $dstimg=imagecreatetruecolor($dstx,$dsty);
    
    
	//图片缩放
    imagecopyresampled($dstimg,$srcimg,0,0,0,0,$dstx,$dsty,$srcx,$srcy);	
    
  
   
}
thumb($img,200,200,'t_');

?>

结果是

Array
(
    [0] => 500
    [1] => 300
    [2] => 2
    [3] => width="500" height="300"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

图片缩放函数1

<?php
$img="a.jpg";

function thumb($img,$dstx,$dsty,$pre){
    // 得到图片信息
    $arr=getimagesize($img);
    $srcx=$arr[0];
	$srcy=$arr[1];
	$srctype=$arr[2];

    //原图资源
    $srcimg=imagecreatefromjpeg($img);
    
    //目标资源
    $dstimg=imagecreatetruecolor($dstx,$dsty);
    
    
	//图片缩放
    imagecopyresampled($dstimg,$srcimg,0,0,0,0,$dstx,$dsty,$srcx,$srcy);	
    
  //保存路径
	$dstfile="t_".$img;

	//保存图片
	imagejpeg($dstimg,$dstfile);

	//关闭资源
	imagedestroy($srcimg);
	imagedestroy($dstimg);
   
}
thumb($img,200,200,'t_');

?>

图片缩放函数5

<?php
$img="a.jpg";

function thumb($img,$dstx,$dsty,$pre){
    // 得到图片信息
    $arr=getimagesize($img);
    $srcx=$arr[0];
	$srcy=$arr[1];
    $srctype=$arr[2];
    
    //图片类型
	//变量函数 
	switch($srctype){
		case 1:
			$imgfrom='imagecreatefromgif';	
			$imgout='imagegif';
			break;	
		case 2:
			$imgfrom='imagecreatefromjpeg';	
			$imgout='imagejpeg';
			break;	
		case 3:
			$imgfrom='imagecreatefrompng';	
			$imgout='imagepng';
			break;	
    }
    //等比例算法
    // 取最大值
    $scale=max($srcx/$dstx,$srcy/$dsty);
    // floor — 舍去法取整
	$dstx=floor($srcx/$scale);
    $dsty=floor($srcy/$scale);
    
    //原图资源
    $srcimg=imagecreatefromjpeg($img);
    
    //目标资源
    $dstimg=imagecreatetruecolor($dstx,$dsty);
    
    
	//图片缩放
    imagecopyresampled($dstimg,$srcimg,0,0,0,0,$dstx,$dsty,$srcx,$srcy);	
    
    //保存路径
    $dir=dirname($img);
    $file=basename($img);
    $dstfile=$dir."/".$pre.$file;

	//保存图片
	imagejpeg($dstimg,$dstfile);

	//关闭资源
	imagedestroy($srcimg);
	imagedestroy($dstimg);
   
}
thumb($img,1000,1000,'t_1000_');
thumb($img,500,500,'t_500_');
thumb($img,200,200,'t_200_');
thumb($img,100,100,'t_100_');

?>


**


**


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值