Heim  >  Artikel  >  Backend-Entwicklung  >  使用PHP缩略图跟剪切图

使用PHP缩略图跟剪切图

WBOY
WBOYOriginal
2016-06-13 12:32:23812Durchsuche

使用PHP缩略图和剪切图
API:
resource imagecreatetruecolor ( 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 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 的位置上。

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

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



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

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);


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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn