使用PHP缩略图和剪切图

API:
resource [color=darkblue]imagecreatetruecolor [/color]( int $width , int $height )
magecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。

是否定义了本函数取决于 PHP 和 GD 的版本。从 PHP 4.0.6 到 4.1.x 只要加载了 GD 模块本函数一直存在,但是在没有安装 GD2 的时候调用,PHP 将发出致命错误并退出。在 PHP 4.2.x 中此行为改为发出警告而不是错误。其它版本只在安装了正确的 GD 版本时定义了本函数。

bool [color=darkblue]imagecopyresampled [/color]( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
$dst_image:新建的图片
$src_image:需要载入的图片
$dst_x:设定需要载入的图片在新图中的x坐标
$dst_y:设定需要载入的图片在新图中的y坐标
$src_x:设定载入图片要载入的区域x坐标
$src_y:设定载入图片要载入的区域y坐标
$dst_w:设定载入的原图的宽度(在此设置缩放)
$dst_h:设定载入的原图的高度(在此设置缩放)
$src_w:原图要载入的宽度
$src_h:原图要载入的高度

bool [color=darkblue]imagecopy [/color]( 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 的位置上。

bool [color=darkblue]imagejpeg[/color] ( resource $image [, string $filename [, int $quality ]] )
imagejpeg() 从 image 图像以 filename 为文件名创建一个 JPEG 图像。

ool [color=darkblue]imagedestroy [/color]( resource $image )
imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,例如 imagecreatetruecolor()。


[url]http://www.cnblogs.com/xiaomia/archive/2010/11/13/1876191.html[/url]
[color=red]一开始采用了 imagecopyresized 方法进行图像等比缩小,实际操作后发现,图像缩小后燥点非常严重。后再换用 imagecopysampled 方法,该方法会对图像进行重新采样,对缩小的图像进行平滑处理,使清晰度得到很大提高 [/color]
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);


[color=darkblue][b]imagecopyresampled[/b][/color]
public static function short($src_from, $destFile, $scale = 0.5)
{
$image=imagecreatefromjpeg($src_from);
list($src_w, $src_h) = getimagesize($src_from);

$new_w = intval($src_w * $scale);
$new_h = intval($src_h * $scale);
$target = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($target, $image, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);

imagejpeg($target, $destFile);
imagedestroy($target);
return $destFile;
}



[url]http://www.cnblogs.com/analyzer/articles/1267017.html[/url]
[color=blue][b]先说说缩略图[/b][/color],它用得比较多,代码如下:
<?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);
?>

我自己ckaephp的例子:
public static function doThumbnail($filePath,$newWidth=273, $newHeight=156){
$fileType=CommonUtils::getFileType($filePath);

$lastIndexOf = strrpos($filePath,".");
$target=substr($filePath,0,$lastIndexOf)."_thumbnail".substr($filePath,$lastIndexOf);
$arr = getimagesize($filePath);
if(strcasecmp("png",$fileType)===0){
$iOut = imagecreatetruecolor($newWidth, $newHeight);
imagealphablending($iOut, false);
imagesavealpha($iOut, true);
$source = imagecreatefrompng($filePath);
imagealphablending($source, true);
imagecopyresampled($iOut, $source, 0, 0, 0, 0, $newWidth, $newHeight, $arr[0],$arr[1]);
imagepng($iOut,$target);

}else{
$image=imagecreatefromjpeg($filePath);
$iOut = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($iOut,$image,0,0,0,0,$newWidth, $newHeight,$arr[0],$arr[1]);
imagejpeg($iOut, $target);
imagedestroy($iOut);
}
return $target;
}



[color=blue][b]再说说剪切图[/b][/color],就是不缩放,而是从原图中剪切出一块小图,比较个性。代码如下:
<?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");
?>

我的例子:
//图片路径
$link = "/mnt/D/work_documents/htdocs/storage_file/2014_11_02/p195ncv6ub1h0f1icf11hpahc16rq8.jpg";
$img = imagecreatefromjpeg($link);
list($width, $height, $type, $attr) = getimagesize($link);
$iOut = imagecreatetruecolor(intval($this->request->data['width']), intval($this->request->data['height']));//给宽度和高度
/*
* 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 的位置上。
*/
imagecopy($iOut, $img, 0, 0, $this->request->data['x1'], intval($this->request->data['y1']), intval($this->request->data['width']), intval($this->request->data['height'])); //复制图片的一部分
imagejpeg($iOut, "/mnt/D/work_documents/htdocs/storage_file/2014_11_02/bak/aa.jpg"); //输出成0_0.jpg,0_1.jpg这样的格式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值