无损裁剪图片
上传图片的时候, 经常是不确定比例, 显示的时候又得统一, 这个方案可以解决
上传图片的时候, 经常是不确定比例, 显示的时候又得统一, 这个方案可以解决
<?php
$image = "jiequ.jpg"; // 原图
$imgstream = file_get_contents($image);
$im = imagecreatefromstring($imgstream);
$x = imagesx($im);//获取图片的宽
$y = imagesy($im);//获取图片的高
// 缩略后的大小
$xx = 140;
$yy = 200;
if($x>$y){
//图片宽大于高
$sx = abs(($y-$x)/2);
$sy = 0;
$thumbw = $y;
$thumbh = $y;
} else {
//图片高大于等于宽
$sy = abs(($x-$y)/2.5);
$sx = 0;
$thumbw = $x;
$thumbh = $x;
}
if(function_exists("imagecreatetruecolor")) {
$dim = imagecreatetruecolor($yy, $xx); // 创建目标图gd2
} else {
$dim = imagecreate($yy, $xx); // 创建目标图gd1
}
imageCopyreSampled ($dim,$im,0,0,$sx,$sy,$yy,$xx,$thumbw,$thumbh);
header ("Content-type: image/jpeg");
imagejpeg ($dim, false, 100);
?>
- 上一篇:无限分类[增强版]
- 下一篇:采集中国代理服务器网
精彩图集
精彩文章






