Home > php教程 > PHP源码 > body text

无损裁剪图片

PHP中文网
Release: 2016-05-25 17:14:55
Original
1020 people have browsed it

php代码:

<?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);
?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template