首页 / PHP / php中图片的缩放比例
php中图片的缩放比例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php中图片的缩放比例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1767字,纯文字阅读大概需要3分钟。
内容图文
在图片保存中,我们不必在ps或其他工具来修改图片,我们也可以用代码来实现。下面举两例子,效果相同:
$sourcefile="images/1.jpg";//旧图路径
$dstfile="images/1_small.jpg";//新图路径
$arr=getimagesize($sourcefile);//获取图片缩放比例,这样我们不必再在目录下去查找图片属性的宽高了。
//$src_width="144";
$src_height="200";也可自定义缩放比例但过大图片会失真
$scle=0.5;//比例系数
$dst_width=ceil($arr[0]*$scle);
$dst_width=ceil($arr[1]*$sclie);//新图宽高
$dst_img=imagecreatetruecolor($dst_width,$dst_height);//载入新图
$src_img=imagecreatefromjpeg($sourcefile);//载入旧图
imagecopyresampled($dst_img,$src_img,0,0,0,0$dst_width,$dst_height,$src_width,$src_height);//输出图像
imagejpeg($dst_img,$dstfile);//新图,路径
imagedestroy($dst_img);
imagedestroy($src_img);//销毁图片
?>
class Zoom{
private $srcImg;//原图地址
private $scle;//缩放系数
public function __construct($a $b){
$this->srcImg=$a;
$this->scle=$b;
list($src_w $src_h)=getimagesize($this->srcImg);
$dst_w=ceil($src_w*$this->scle);
$dst_h=ceil($src_h*$this->scle);
$arr=explode("."$this->srcImg);
$ext=end($arr);
if($ext=="jpg"){
$src_img=imagecreatefromjpeg($this->srcImg);
}else if($ext=="gif"){
$src_img=imagecreatefromgif($this->srcImg);
}else if($ext=="png"){
$src_img=imagecreatefrompng($this->srcImg);
}else{
echo "请选择jpg、png、gif图片格式";
exit;
}
$dstImg=$arr[0].$this->scle."_small".".".$ext;
$dst_img=imagecreatetruecolor($dst_w$dst_h);
imagecopyresampled($dst_img$src_img0000$dst_w$dst_h$src_w$src_h);
if($ext=="jpg"){
imagejpeg($dst_img$dstImg);
}else if($ext=="gif"){
imagegif($dst_img$dstImg);
}else if($ext=="png"){
imagepng($dst_img$dstImg);
}
imagedestroy($src_img);
imagedestroy($dst_img);
}
}
$image="imgs/a.jpg";
$b=1;
$i=new Zoom($image$b);
?>
内容总结
以上是互联网集市为您收集整理的php中图片的缩放比例全部内容,希望文章能够帮你解决php中图片的缩放比例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。