日期:2014-05-16 浏览次数:20616 次
list($src_w,$src_h)=getimagesize($src_img); // 获取原图尺寸
$dst_scale = $dst_h/$dst_w; //目标图像长宽比
$src_scale = $src_h/$src_w; // 原图长宽比
if($src_scale>=$dst_scale){ // 过高
$w = intval($src_w);
$h = intval($dst_scale*$w);
$x = 0;
$y = ($src_h - $h)/3;
}
else{ // 过宽
$h = intval($src_h);
$w = intval($h/$dst_scale);
$x = ($src_w - $w)/2;
$y = 0;
}
// 剪裁
$source=imagecreatefromjpeg($src_img);
$croped=imagecreatetruecolor($w, $h);
imagecopy($croped,$source,0,0,$x,$y,$src_w,$src_h);
// 缩放
$scale = $dst_w/$w;
$target = imagecreatetruecolor($dst_w, $dst_h);
$final_w = intval($w*$scale);
$final_h = intval($h*$scale);
imagecopyresampled($target,$croped,0,0,0,0,$final_w,$final_h,$w,$h);
// 保存
$timestamp = time();
imagejpeg($target, "$timestamp.jpg");
imagedestroy($target);<?php
header("Content-type: image/png");
//原图
$filename='source.jpg';
//缩放比例 新图/原图
$percent = '0.5';
list($width,$height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagepng($thumb);
?> <?php
$maxW=300;
$maxH=300;
//图片路径
$link= "big.jpg";
$img = imagecreatefromjpeg($link);
list($width, $height, $type, $attr) = getimagesize($link);
$widthnum=ceil($width/$maxW);
$heightnum=ceil($height/$maxH);
$iOut = imagecreatetruecolor ($maxW,$maxH);
//bool imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h )
//将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
//整图循环切割
for ($i=0;$i < $heightnum;$i++) {
for ($j=0;$j < $widthnum;$j++) {
imagecopy($iOut,$img,0,0,($j*$maxW),($i*$maxH),$maxW,$maxH);//复制图片的一部分
imagejpeg($iOut,"images/".$i."_".$j.".jpg"); //输出成0_0.jpg,0_1.jpg这样的格式
}
}
//只剪切一个开始部位的小图.复制图片的一部分
imagecopy($iOut,$img,0,0,0,0,$maxW,$maxH);
imagejpeg($iOut,"images/sm.jpg");
?>